Skip to content

Commit 2439c25

Browse files
Cleaning up and deduplicating launch template related code (#370)
* cleaning up launch template code * update changelog * Remove variable worker_group_launch_template_tags * misc fixes * fixing and simplifying example
1 parent 959e533 commit 2439c25

File tree

8 files changed

+75
-154
lines changed

8 files changed

+75
-154
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
2121

2222
- Set default suspended processes for ASG to `AZRebalance` (by @max-rocket-internet)
2323
- 4 small changes to `aws_launch_template` resource (by @max-rocket-internet)
24+
- (Breaking Change) Rewritten and de-duplicated code related to Launch Templates (by @max-rocket-internet)
2425
- Add .prettierignore file (by @rothandrew)
2526
- Switch to https for the pre-commit repos (by @rothandrew)
2627
- Add instructions on how to enable the docker bridge network (by @rothandrew)

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a
148148
| worker\_create\_security\_group | Whether to create a security group for the workers or attach the workers to `worker_security_group_id`. | string | `"true"` | no |
149149
| worker\_group\_count | The number of maps contained within the worker_groups list. | string | `"1"` | no |
150150
| worker\_group\_launch\_template\_count | The number of maps contained within the worker_groups_launch_template list. | string | `"0"` | no |
151-
| worker\_group\_launch\_template\_tags | A map defining extra tags to be applied to the worker group template ASG. | map | `{ "default": [] }` | no |
152151
| worker\_group\_tags | A map defining extra tags to be applied to the worker group ASG. | map | `{ "default": [] }` | no |
153152
| worker\_groups | A list of maps defining worker group configurations to be defined using AWS Launch Configurations. See workers_group_defaults for valid keys. | list | `[ { "name": "default" } ]` | no |
154153
| worker\_groups\_launch\_template | A list of maps defining worker group configurations to be defined using AWS Launch Templates. See workers_group_defaults for valid keys. | list | `[ { "name": "default" } ]` | no |
@@ -157,14 +156,14 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a
157156
| workers\_additional\_policies | Additional policies to be added to workers | list | `[]` | no |
158157
| workers\_additional\_policies\_count | | string | `"0"` | no |
159158
| workers\_group\_defaults | Override default values for target groups. See workers_group_defaults_defaults in local.tf for valid keys. | map | `{}` | no |
160-
| workers\_group\_launch\_template\_defaults | Override default values for target groups. See workers_group_defaults_defaults in local.tf for valid keys. | map | `{}` | no |
161159
| write\_aws\_auth\_config | Whether to write the aws-auth configmap file. | string | `"true"` | no |
162160
| write\_kubeconfig | Whether to write a Kubectl config file containing the cluster configuration. Saved to `config_output_path`. | string | `"true"` | no |
163161
164162
## Outputs
165163
166164
| Name | Description |
167165
|------|-------------|
166+
| cluster\_arn | The Amazon Resource Name (ARN) of the cluster. |
168167
| cluster\_certificate\_authority\_data | Nested attribute containing certificate-authority-data for your cluster. This is the base64 encoded certificate data required to communicate with your cluster. |
169168
| cluster\_endpoint | The endpoint for your EKS Kubernetes API. |
170169
| cluster\_iam\_role\_arn | IAM role ARN of the EKS cluster. |
@@ -183,9 +182,9 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a
183182
| workers\_asg\_arns | IDs of the autoscaling groups containing workers. |
184183
| workers\_asg\_names | Names of the autoscaling groups containing workers. |
185184
| workers\_default\_ami\_id | ID of the default worker group AMI |
186-
| workers\_user\_data | User data of worker groups |
187185
| workers\_launch\_template\_arns | ARNs of the worker launch templates. |
188186
| workers\_launch\_template\_ids | IDs of the worker launch templates. |
189187
| workers\_launch\_template\_latest\_versions | Latest versions of the worker launch templates. |
188+
| workers\_user\_data | User data of worker groups |
190189
191190
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

data.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ data "template_file" "launch_template_userdata" {
9494
cluster_name = "${aws_eks_cluster.this.name}"
9595
endpoint = "${aws_eks_cluster.this.endpoint}"
9696
cluster_auth_base64 = "${aws_eks_cluster.this.certificate_authority.0.data}"
97-
pre_userdata = "${lookup(var.worker_groups_launch_template[count.index], "pre_userdata", local.workers_group_launch_template_defaults["pre_userdata"])}"
98-
additional_userdata = "${lookup(var.worker_groups_launch_template[count.index], "additional_userdata", local.workers_group_launch_template_defaults["additional_userdata"])}"
99-
bootstrap_extra_args = "${lookup(var.worker_groups_launch_template[count.index], "bootstrap_extra_args", local.workers_group_launch_template_defaults["bootstrap_extra_args"])}"
100-
kubelet_extra_args = "${lookup(var.worker_groups_launch_template[count.index], "kubelet_extra_args", local.workers_group_launch_template_defaults["kubelet_extra_args"])}"
97+
pre_userdata = "${lookup(var.worker_groups_launch_template[count.index], "pre_userdata", local.workers_group_defaults["pre_userdata"])}"
98+
additional_userdata = "${lookup(var.worker_groups_launch_template[count.index], "additional_userdata", local.workers_group_defaults["additional_userdata"])}"
99+
bootstrap_extra_args = "${lookup(var.worker_groups_launch_template[count.index], "bootstrap_extra_args", local.workers_group_defaults["bootstrap_extra_args"])}"
100+
kubelet_extra_args = "${lookup(var.worker_groups_launch_template[count.index], "kubelet_extra_args", local.workers_group_defaults["kubelet_extra_args"])}"
101101
}
102102
}
103103

@@ -112,6 +112,6 @@ data "aws_iam_instance_profile" "custom_worker_group_iam_instance_profile" {
112112
}
113113

114114
data "aws_iam_instance_profile" "custom_worker_group_launch_template_iam_instance_profile" {
115-
name = "${lookup(var.worker_groups_launch_template[count.index], "iam_instance_profile_name", local.workers_group_launch_template_defaults["iam_instance_profile_name"])}"
115+
name = "${lookup(var.worker_groups_launch_template[count.index], "iam_instance_profile_name", local.workers_group_defaults["iam_instance_profile_name"])}"
116116
count = "${var.manage_worker_iam_resources ? 0 : var.worker_group_launch_template_count}"
117117
}

examples/eks_test_fixture/main.tf

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,17 @@ locals {
6161

6262
worker_groups = [
6363
{
64-
# This will launch an autoscaling group with only On-Demand instances
6564
instance_type = "t2.small"
6665
additional_userdata = "echo foo bar"
67-
subnets = "${join(",", module.vpc.private_subnets)}"
68-
asg_desired_capacity = "2"
66+
asg_desired_capacity = 2
6967
},
7068
]
7169
worker_groups_launch_template = [
7270
{
73-
# This will launch an autoscaling group with only Spot Fleet instances
74-
instance_type = "t2.small"
75-
additional_userdata = "echo foo bar"
76-
subnets = "${join(",", module.vpc.private_subnets)}"
77-
additional_security_group_ids = "${aws_security_group.worker_group_mgmt_one.id},${aws_security_group.worker_group_mgmt_two.id}"
78-
override_instance_type = "t3.small"
79-
asg_desired_capacity = "2"
80-
spot_instance_pools = 10
81-
on_demand_percentage_above_base_capacity = "0"
71+
instance_type = "t2.small"
72+
additional_userdata = "echo foo bar"
73+
additional_security_group_ids = "${aws_security_group.worker_group_mgmt_one.id},${aws_security_group.worker_group_mgmt_two.id}"
74+
asg_desired_capacity = 2
8275
},
8376
]
8477
tags = {
@@ -163,8 +156,8 @@ module "eks" {
163156
vpc_id = "${module.vpc.vpc_id}"
164157
worker_groups = "${local.worker_groups}"
165158
worker_groups_launch_template = "${local.worker_groups_launch_template}"
166-
worker_group_count = "1"
167-
worker_group_launch_template_count = "1"
159+
worker_group_count = 1
160+
worker_group_launch_template_count = 1
168161
worker_additional_security_group_ids = ["${aws_security_group.all_worker_mgmt.id}"]
169162
map_roles = "${var.map_roles}"
170163
map_roles_count = "${var.map_roles_count}"

local.tf

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -43,56 +43,19 @@ locals {
4343
target_group_arns = "" # A comma delimited list of ALB target group ARNs to be associated to the ASG
4444
enabled_metrics = "" # A comma delimited list of metrics to be collected i.e. GroupMinSize,GroupMaxSize,GroupDesiredCapacity
4545
placement_group = "" # The name of the placement group into which to launch the instances, if any.
46-
}
47-
48-
workers_group_defaults = "${merge(local.workers_group_defaults_defaults, var.workers_group_defaults)}"
46+
service_linked_role_arn = "" # Arn of custom service linked role that Auto Scaling group will use. Useful when you have encrypted EBS
4947

50-
workers_group_launch_template_defaults_defaults = {
51-
name = "count.index" # Name of the worker group. Literal count.index will never be used but if name is not set, the count.index interpolation will be used.
52-
ami_id = "${data.aws_ami.eks_worker.id}" # AMI ID for the eks workers. If none is provided, Terraform will search for the latest version of their EKS optimized worker AMI.
53-
root_block_device_id = "${data.aws_ami.eks_worker.root_device_name}" # Root device name for workers. If non is provided, will assume default AMI was used.
54-
asg_desired_capacity = "1" # Desired worker capacity in the autoscaling group.
55-
asg_max_size = "3" # Maximum worker capacity in the autoscaling group.
56-
asg_min_size = "1" # Minimum worker capacity in the autoscaling group.
57-
asg_force_delete = false # Enable forced deletion for the autoscaling group.
58-
instance_type = "m4.large" # Size of the workers instances.
59-
override_instance_type = "t3.large" # Need to specify at least one additional instance type for mixed instances policy. The instance_type holds higher priority for on demand instances.
60-
on_demand_allocation_strategy = "prioritized" # Strategy to use when launching on-demand instances. Valid values: prioritized.
61-
on_demand_base_capacity = "0" # Absolute minimum amount of desired capacity that must be fulfilled by on-demand instances
62-
on_demand_percentage_above_base_capacity = "100" # Percentage split between on-demand and Spot instances above the base on-demand capacity
63-
spot_allocation_strategy = "lowest-price" # The only valid value is lowest-price, which is also the default value. The Auto Scaling group selects the cheapest Spot pools and evenly allocates your Spot capacity across the number of Spot pools that you specify.
64-
spot_instance_pools = 10 # "Number of Spot pools per availability zone to allocate capacity. EC2 Auto Scaling selects the cheapest Spot pools and evenly allocates Spot capacity across the number of Spot pools that you specify."
65-
spot_max_price = "" # Maximum price per unit hour that the user is willing to pay for the Spot instances. Default is the on-demand price
66-
spot_price = "" # Cost of spot instance.
67-
placement_tenancy = "default" # The tenancy of the instance. Valid values are "default" or "dedicated".
68-
root_volume_size = "100" # root volume size of workers instances.
69-
root_volume_type = "gp2" # root volume type of workers instances, can be 'standard', 'gp2', or 'io1'
70-
root_iops = "0" # The amount of provisioned IOPS. This must be set with a volume_type of "io1".
71-
root_encrypted = "" # root volume encryption for workers.
72-
kms_key_id = "" # KMS key ID used for encrypted block device. ASG must have access to this key. If not specified, the default KMS key will be used.
73-
key_name = "" # The key name that should be used for the instances in the autoscaling group
74-
pre_userdata = "" # userdata to pre-append to the default userdata.
75-
bootstrap_extra_args = "" # Extra arguments passed to the bootstrap.sh script from the EKS AMI.
76-
additional_userdata = "" # userdata to append to the default userdata.
77-
ebs_optimized = true # sets whether to use ebs optimization on supported types.
78-
enable_monitoring = true # Enables/disables detailed monitoring.
79-
public_ip = false # Associate a public ip address with a worker
80-
eni_delete = true # Delete the ENI on termination (if set to false you will have to manually delete before destroying)
81-
kubelet_extra_args = "" # This string is passed directly to kubelet if set. Useful for adding labels or taints.
82-
subnets = "${join(",", var.subnets)}" # A comma delimited string of subnets to place the worker nodes in. i.e. subnet-123,subnet-456,subnet-789
83-
autoscaling_enabled = false # Sets whether policy and matching tags will be added to allow autoscaling.
84-
additional_security_group_ids = "" # A comma delimited list of additional security group ids to include in worker launch config
85-
protect_from_scale_in = false # Prevent AWS from scaling in, so that cluster-autoscaler is solely responsible.
86-
iam_instance_profile_name = "" # A custom IAM instance profile name. Used when manage_worker_iam_resources is set to false. Incompatible with iam_role_id.
87-
iam_role_id = "${local.default_iam_role_id}" # A custom IAM role id. Incompatible with iam_instance_profile_name.
88-
suspended_processes = "AZRebalance" # A comma delimited string of processes to to suspend. i.e. AZRebalance,HealthCheck,ReplaceUnhealthy
89-
target_group_arns = "" # A comma delimited list of ALB target group ARNs to be associated to the ASG
90-
enabled_metrics = "" # A comma delimited list of metrics to be collected i.e. GroupMinSize,GroupMaxSize,GroupDesiredCapacity
91-
placement_group = "" # The name of the placement group into which to launch the instances, if any.
92-
service_linked_role_arn = "" # Arn of custom service linked role that Auto Scaling group will use. Useful when you have encrypted EBS
48+
# Settings for launch templates
49+
root_block_device_name = "${data.aws_ami.eks_worker.root_device_name}" # Root device name for workers. If non is provided, will assume default AMI was used.
50+
root_kms_key_id = "" # The KMS key to use when encrypting the root storage device
51+
launch_template_version = "$Latest" # The lastest version of the launch template to use in the autoscaling group
52+
launch_template_placement_tenancy = "default" # The placement tenancy for instances
53+
launch_template_placement_group = "" # The name of the placement group into which to launch the instances, if any.
54+
root_encrypted = "" # Whether the volume should be encrypted or not
55+
eni_delete = true # Delete the ENI on termination (if set to false you will have to manually delete before destroying)
9356
}
9457

95-
workers_group_launch_template_defaults = "${merge(local.workers_group_launch_template_defaults_defaults, var.workers_group_launch_template_defaults)}"
58+
workers_group_defaults = "${merge(local.workers_group_defaults_defaults, var.workers_group_defaults)}"
9659

9760
ebs_optimized = {
9861
"c1.medium" = false

variables.tf

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,21 +138,6 @@ variable "worker_group_launch_template_count" {
138138
default = "0"
139139
}
140140

141-
variable "workers_group_launch_template_defaults" {
142-
description = "Override default values for target groups. See workers_group_defaults_defaults in local.tf for valid keys."
143-
type = "map"
144-
default = {}
145-
}
146-
147-
variable "worker_group_launch_template_tags" {
148-
description = "A map defining extra tags to be applied to the worker group template ASG."
149-
type = "map"
150-
151-
default = {
152-
default = []
153-
}
154-
}
155-
156141
variable "worker_security_group_id" {
157142
description = "If provided, all workers will be attached to this security group. If not given, a security group will be created with necessary ingres/egress to work with the EKS cluster."
158143
default = ""

0 commit comments

Comments
 (0)