Skip to content

Commit

Permalink
asg_node_group enhancements
Browse files Browse the repository at this point in the history
* Add the ability to add custom AWS tags to node resources
* Make asg_termination policies configurable
* Disable terraform to wait for ASG capacity
* Enable configuring asg metrics collection
  • Loading branch information
errm committed May 7, 2020
1 parent e4446f0 commit 2615f00
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 7 deletions.
4 changes: 4 additions & 0 deletions examples/cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ module "cluster" {
groups = ["system:masters"]
}
]

tags = {
Project = "terraform-aws-eks"
}
}

module "node_group" {
Expand Down
35 changes: 30 additions & 5 deletions modules/asg_node_group/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ locals {
max_size = floor(var.max_size / length(local.asg_subnets))
min_size = ceil(var.min_size / length(local.asg_subnets))
root_device_mappings = tolist(data.aws_ami.image.block_device_mappings)[0]
tags = merge(var.cluster_config.tags, var.tags, { "kubernetes.io/cluster/${var.cluster_config.name}" = "owned" })
}

data "aws_ssm_parameter" "image_id" {
Expand Down Expand Up @@ -84,16 +85,31 @@ resource "aws_launch_template" "config" {
}
}

tag_specifications {
resource_type = "instance"
tags = local.tags
}

tag_specifications {
resource_type = "volume"
tags = local.tags
}

tags = local.tags

key_name = var.key_name
}

resource "aws_autoscaling_group" "nodes" {
for_each = local.asg_subnets

name = "${local.name_prefix}-${each.key}"
min_size = local.min_size
max_size = local.max_size
vpc_zone_identifier = each.value
name = "${local.name_prefix}-${each.key}"
min_size = local.min_size
max_size = local.max_size
vpc_zone_identifier = each.value
termination_policies = var.termination_policies
enabled_metrics = var.enabled_metrics
wait_for_capacity_timeout = 0

mixed_instances_policy {
launch_template {
Expand Down Expand Up @@ -133,7 +149,7 @@ resource "aws_autoscaling_group" "nodes" {
tag {
key = "kubernetes.io/cluster/${var.cluster_config.name}"
value = "owned"
propagate_at_launch = true
propagate_at_launch = false
}

tag {
Expand All @@ -160,5 +176,14 @@ resource "aws_autoscaling_group" "nodes" {
}
}

dynamic "tag" {
for_each = local.tags
content {
key = tag.key
value = tag.value
propagate_at_launch = false
}
}

depends_on = [aws_launch_template.config]
}
33 changes: 33 additions & 0 deletions modules/asg_node_group/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ variable "cluster_config" {
private_subnet_ids = map(string)
node_security_group = string
node_instance_profile = string
tags = map(string)
})
}

Expand Down Expand Up @@ -91,6 +92,12 @@ variable "taints" {
description = "taints that will be added to the kubernetes node"
}

variable "tags" {
type = map(string)
default = {}
description = "A map of additional tags to apply to this groups AWS resources"
}

variable "instance_types" {
type = list(string)
description = <<EOF
Expand All @@ -112,3 +119,29 @@ variable "security_groups" {
default = []
description = "Additional security groups for the nodes"
}

variable "termination_policies" {
type = list(string)
default = ["OldestLaunchTemplate", "OldestInstance"]
description = "A list of policies to decide how the instances in the auto scale group should be terminated."
}

variable "enabled_metrics" {
type = list(string)
default = [
"GroupDesiredCapacity",
"GroupInServiceInstances",
"GroupInServiceCapacity",
"GroupMaxSize",
"GroupMinSize",
"GroupPendingInstances",
"GroupPendingCapacity",
"GroupStandbyInstances",
"GroupStandbyCapacity",
"GroupTerminatingInstances",
"GroupTerminatingCapacity",
"GroupTotalInstances",
"GroupTotalCapacity",
]
description = "A list of metrics to collect."
}
1 change: 1 addition & 0 deletions modules/cluster/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ locals {
private_subnet_ids = var.vpc_config.private_subnet_ids
node_security_group = aws_eks_cluster.control_plane.vpc_config.0.cluster_security_group_id
node_instance_profile = var.iam_config.node_role
tags = var.tags
}
}

Expand Down
4 changes: 2 additions & 2 deletions modules/cluster/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ variable "legacy_security_groups" {
}

variable "tags" {
type = map
type = map(string)
default = {}
description = "A map of tags to assign to the cluster and cloudwatch log group resources"
description = "A map of tags to assign to cluster AWS resources"
}

0 comments on commit 2615f00

Please sign in to comment.