Skip to content

Commit

Permalink
Moved to terraform module
Browse files Browse the repository at this point in the history
  • Loading branch information
garutilorenzo committed Feb 9, 2022
1 parent e762208 commit f9f0137
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 118 deletions.
58 changes: 18 additions & 40 deletions asg.tf → k3s_cluster/asg.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,20 @@ resource "aws_autoscaling_group" "k3s_servers_asg" {
version = "$Latest"
}

override {
instance_type = "t3.large"
weighted_capacity = "1"
dynamic "override" {
for_each = var.instance_types
content {
instance_type = override.value
weighted_capacity = "1"
}
}

override {
instance_type = "t2.large"
weighted_capacity = "1"
}

override {
instance_type = "m4.large"
weighted_capacity = "1"
}

override {
instance_type = "t3a.large"
weighted_capacity = "1"
}
}
}

desired_capacity = 3
min_size = 3
max_size = 4
desired_capacity = var.k3s_server_desired_capacity
min_size = var.k3s_server_min_capacity
max_size = var.k3s_server_max_capacity
health_check_grace_period = 300
health_check_type = "EC2"
force_delete = true
Expand Down Expand Up @@ -86,31 +75,20 @@ resource "aws_autoscaling_group" "k3s_workers_asg" {
version = "$Latest"
}

override {
instance_type = "t3.large"
weighted_capacity = "1"
dynamic "override" {
for_each = var.instance_types
content {
instance_type = override.value
weighted_capacity = "1"
}
}

override {
instance_type = "t2.large"
weighted_capacity = "1"
}

override {
instance_type = "m4.large"
weighted_capacity = "1"
}

override {
instance_type = "t3a.large"
weighted_capacity = "1"
}
}
}

desired_capacity = 3
min_size = 3
max_size = 4
desired_capacity = var.k3s_worker_desired_capacity
min_size = var.k3s_worker_min_capacity
max_size = var.k3s_worker_max_capacity
health_check_grace_period = 300
health_check_type = "EC2"
force_delete = true
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions launchtemplate.tf → k3s_cluster/launchtemplate.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
resource "aws_launch_template" "k3s_server" {
name_prefix = "k3s_server_tpl"
image_id = var.AMIS[var.AWS_REGION]
instance_type = "t3.large"
instance_type = var.default_instance_type
user_data = data.template_cloudinit_config.k3s_server.rendered

iam_instance_profile {
Expand All @@ -28,7 +28,7 @@ resource "aws_launch_template" "k3s_server" {
resource "aws_launch_template" "k3s_agent" {
name_prefix = "k3s_agent_tpl"
image_id = var.AMIS[var.AWS_REGION]
instance_type = "t3.large"
instance_type = var.default_instance_type
user_data = data.template_cloudinit_config.k3s_agent.rendered

iam_instance_profile {
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions securitygroup.tf → k3s_cluster/securitygroup.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ resource "aws_security_group" "allow-strict" {
}

ingress {
from_port = 6443
to_port = 6443
from_port = var.kube_api_port
to_port = var.kube_api_port
protocol = "tcp"
cidr_blocks = [var.vpc_subnet_cidr]
}
Expand Down
File renamed without changes.
118 changes: 118 additions & 0 deletions k3s_cluster/vars.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
variable "AWS_REGION" {
type = string
}

variable "AMIS" {
type = map(string)
default = {
us-east-1 = "ami-0ae74ae9c43584639"
us-west-2 = "ami-09f5b7791a4e85729"
eu-west-1 = "ami-081ff4b9aa4e81a08" # ami-0da36f7f059b7086e
}
}

variable "PATH_TO_PUBLIC_KEY" {
type = string
default = "~/.ssh/id_rsa.pub"
description = "Path to your public key"
}

variable "PATH_TO_PRIVATE_KEY" {
type = string
default = "~/.ssh/id_rsa"
description = "Path to your private key"
}

variable "vpc_id" {
type = string
description = "The vpc id"
}

variable "instance_profile_name" {
type = string
description = "The name of the instance profile to use"
}

variable "my_public_ip_cidr" {
type = string
description = "My public ip CIDR"
}

variable "vpc_subnet_cidr" {
type = string
description = "VPC subnet CIDR"
}

variable "vpc_subnets" {
type = list(any)
description = "The vpc subnets ids"
}

variable "default_instance_type" {
type = string
default = "t3.large"
description = "Instance type to be used"
}

variable "instance_types" {
description = "List of instance types to use"
type = map(string)
default = {
asg_instance_type_1 = "t3.large"
asg_instance_type_3 = "m4.large"
asg_instance_type_4 = "t3a.large"
}
}

variable "kube_api_port" {
type = number
default = 6443
description = "Kubeapi Port"
}

variable "k3s_server_desired_capacity" {
type = number
default = 3
description = "K3s server ASG desired capacity"
}

variable "k3s_server_min_capacity" {
type = number
default = 3
description = "K3s server ASG min capacity"
}

variable "k3s_server_max_capacity" {
type = number
default = 4
description = "K3s server ASG max capacity"
}

variable "k3s_worker_desired_capacity" {
type = number
default = 3
description = "K3s server ASG desired capacity"
}

variable "k3s_worker_min_capacity" {
type = number
default = 3
description = "K3s server ASG min capacity"
}

variable "k3s_worker_max_capacity" {
type = number
default = 4
description = "K3s server ASG max capacity"
}

variable "cluster_name" {
type = string
default = "k3s-cluster"
description = "Cluster name"
}

variable "k3s_token" {
type = string
description = "Override to set k3s cluster registration token"
}
25 changes: 25 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
variable "AWS_ACCESS_KEY" {

}

variable "AWS_SECRET_KEY" {

}

variable "AWS_REGION" {
type = string
default = "<change_me>"
description = "AWS Zone"
}

module "k3s_cluster" {
AWS_REGION = "<change_me>"
instance_profile_name = "AmazonEC2ReadOnlyAccess"
my_public_ip_cidr = "<change_me>"
vpc_id = "<change_me>"
vpc_subnets = ["<change_me>", "<change_me>", "<change_me>"]
vpc_subnet_cidr = "<change_me>"
cluster_name = "k3s-cluster"
k3s_token = "<change_me>"
source = "./k3s_cluster/"
}
74 changes: 0 additions & 74 deletions vars.tf

This file was deleted.

0 comments on commit f9f0137

Please sign in to comment.