diff --git a/.travis.yml b/.travis.yml index 2214281..c5fb533 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,7 +23,7 @@ install: - bundle install before_script: -- export TERRAFORM_VERSION=0.11.14 +- export TERRAFORM_VERSION=0.12.2 - curl --silent --output terraform.zip "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip" - unzip terraform.zip ; rm -f terraform.zip; chmod +x terraform - mkdir -p ${HOME}/bin ; export PATH=${PATH}:${HOME}/bin; mv terraform ${HOME}/bin/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 37e4829..a1829f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ project adheres to [Semantic Versioning](http://semver.org/). ## Next release -## [[v4.?.?](https://github.com/terraform-aws-modules/terraform-aws-eks/compare/v4.0.0...HEAD)] - 2019-06-??] +## [[v5.0.0](https://github.com/terraform-aws-modules/terraform-aws-eks/compare/v4.0.2...HEAD)] - 2019-06-??] ### Added @@ -18,6 +18,11 @@ project adheres to [Semantic Versioning](http://semver.org/). ### Changed +- Finally, Terraform 0.12 support, [Upgrade Guide](https://github.com/terraform-aws-modules/terraform-aws-eks/pull/394) (by @alex-goncharov @nauxliu @timboven) +- All the xx_count variables have been removed (by @nauxliu on behalf of RightCapital) +- Use actual lists in the workers group maps instead of strings with commas (by @nauxliu on behalf of RightCapital) +- Move variable `worker_group_tags` to workers group's attribute `tags` (by @nauxliu on behalf of RightCapital) +- Change override instance_types to list (by @nauxliu on behalf of RightCapital) - Fix toggle for IAM instance profile creation for mixed launch templates (by @jnozo) # History diff --git a/README.md b/README.md index 6de9293..3d15e26 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,11 @@ module "my-cluster" { { instance_type = "m4.large" asg_max_size = 5 + tags = { + key = "foo" + value = "bar" + propagate_at_launch = true + } } ] @@ -130,11 +135,8 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a | manage\_cluster\_iam\_resources | Whether to let the module manage cluster IAM resources. If set to false, cluster_iam_role_name must be specified. | string | `"true"` | no | | manage\_worker\_iam\_resources | Whether to let the module manage worker IAM resources. If set to false, iam_instance_profile_name must be specified for workers. | string | `"true"` | no | | map\_accounts | Additional AWS account numbers to add to the aws-auth configmap. See examples/basic/variables.tf for example format. | list | `[]` | no | -| map\_accounts\_count | The count of accounts in the map_accounts list. | string | `"0"` | no | | map\_roles | Additional IAM roles to add to the aws-auth configmap. See examples/basic/variables.tf for example format. | list | `[]` | no | -| map\_roles\_count | The count of roles in the map_roles list. | string | `"0"` | no | | map\_users | Additional IAM users to add to the aws-auth configmap. See examples/basic/variables.tf for example format. | list | `[]` | no | -| map\_users\_count | The count of roles in the map_users list. | string | `"0"` | no | | permissions\_boundary | If provided, all IAM roles will be created with this permissions boundary attached. | string | `""` | no | | subnets | A list of subnets to place the EKS cluster and workers within. | list | n/a | yes | | tags | A map of tags to add to all resources. | map | `{}` | no | @@ -142,17 +144,12 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a | worker\_additional\_security\_group\_ids | A list of additional security group ids to attach to worker instances | list | `[]` | no | | worker\_ami\_name\_filter | Additional name filter for AWS EKS worker AMI. Default behaviour will get latest for the cluster_version but could be set to a release from amazon-eks-ami, e.g. "v20190220" | string | `"v*"` | no | | 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 | -| worker\_group\_count | The number of maps contained within the worker_groups list. | string | `"1"` | no | -| worker\_group\_launch\_template\_count | The number of maps contained within the worker_groups_launch_template list. | string | `"0"` | no | -| worker\_group\_launch\_template\_mixed\_count | The number of maps contained within the worker_groups_launch_template_mixed list. | string | `"0"` | no | -| worker\_group\_tags | A map defining extra tags to be applied to the worker group ASG. | map | `{ "default": [] }` | no | | 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 | | 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 | | worker\_groups\_launch\_template\_mixed | 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 | | worker\_security\_group\_id | 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. | string | `""` | no | | worker\_sg\_ingress\_from\_port | Minimum port number from which pods will accept communication. Must be changed to a lower value if some pods in your cluster will expose a port lower than 1025 (e.g. 22, 80, or 443). | string | `"1025"` | no | | workers\_additional\_policies | Additional policies to be added to workers | list | `[]` | no | -| workers\_additional\_policies\_count | | string | `"0"` | no | | workers\_group\_defaults | Override default values for target groups. See workers_group_defaults_defaults in local.tf for valid keys. | map | `{}` | no | | write\_aws\_auth\_config | Whether to write the aws-auth configmap file. | string | `"true"` | no | | write\_kubeconfig | Whether to write a Kubectl config file containing the cluster configuration. Saved to `config_output_path`. | string | `"true"` | no | diff --git a/aws_auth.tf b/aws_auth.tf index abb6e02..c5cb532 100644 --- a/aws_auth.tf +++ b/aws_auth.tf @@ -1,103 +1,140 @@ resource "local_file" "config_map_aws_auth" { - count = "${var.write_aws_auth_config ? 1 : 0}" - content = "${data.template_file.config_map_aws_auth.rendered}" + count = var.write_aws_auth_config ? 1 : 0 + content = data.template_file.config_map_aws_auth.rendered filename = "${var.config_output_path}config-map-aws-auth_${var.cluster_name}.yaml" } resource "null_resource" "update_config_map_aws_auth" { - count = "${var.manage_aws_auth ? 1 : 0}" - depends_on = ["aws_eks_cluster.this"] + count = var.manage_aws_auth ? 1 : 0 + depends_on = [aws_eks_cluster.this] provisioner "local-exec" { - working_dir = "${path.module}" + working_dir = path.module command = < kube_config.yaml & \ -echo "${null_resource.update_config_map_aws_auth.triggers.config_map_rendered}" > aws_auth_configmap.yaml & \ +echo "${null_resource.update_config_map_aws_auth[0].triggers.kube_config_map_rendered}" > kube_config.yaml & \ +echo "${null_resource.update_config_map_aws_auth[0].triggers.config_map_rendered}" > aws_auth_configmap.yaml & \ kubectl apply -f aws_auth_configmap.yaml --kubeconfig kube_config.yaml && break || \ sleep 10; \ done; \ rm aws_auth_configmap.yaml kube_config.yaml; EOS - interpreter = ["${var.local_exec_interpreter}"] + + interpreter = var.local_exec_interpreter } - triggers { - kube_config_map_rendered = "${data.template_file.kubeconfig.rendered}" - config_map_rendered = "${data.template_file.config_map_aws_auth.rendered}" - endpoint = "${aws_eks_cluster.this.endpoint}" + triggers = { + kube_config_map_rendered = data.template_file.kubeconfig.rendered + config_map_rendered = data.template_file.config_map_aws_auth.rendered + endpoint = aws_eks_cluster.this.endpoint } } -data "aws_caller_identity" "current" {} +data "aws_caller_identity" "current" { +} data "template_file" "launch_template_mixed_worker_role_arns" { - count = "${var.worker_group_launch_template_mixed_count}" - template = "${file("${path.module}/templates/worker-role.tpl")}" + count = local.worker_group_launch_template_mixed_count + template = file("${path.module}/templates/worker-role.tpl") - vars { - worker_role_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/${element(coalescelist(aws_iam_instance_profile.workers_launch_template_mixed.*.role, data.aws_iam_instance_profile.custom_worker_group_launch_template_mixed_iam_instance_profile.*.role_name), count.index)}" + vars = { + worker_role_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/${element( + coalescelist( + aws_iam_instance_profile.workers_launch_template_mixed.*.role, + data.aws_iam_instance_profile.custom_worker_group_launch_template_mixed_iam_instance_profile.*.role_name, + ), + count.index, + )}" } } data "template_file" "launch_template_worker_role_arns" { - count = "${var.worker_group_launch_template_count}" - template = "${file("${path.module}/templates/worker-role.tpl")}" + count = local.worker_group_launch_template_count + template = file("${path.module}/templates/worker-role.tpl") - vars { - worker_role_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/${element(coalescelist(aws_iam_instance_profile.workers_launch_template.*.role, data.aws_iam_instance_profile.custom_worker_group_launch_template_iam_instance_profile.*.role_name), count.index)}" + vars = { + worker_role_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/${element( + coalescelist( + aws_iam_instance_profile.workers_launch_template.*.role, + data.aws_iam_instance_profile.custom_worker_group_launch_template_iam_instance_profile.*.role_name, + ), + count.index, + )}" } } data "template_file" "worker_role_arns" { - count = "${var.worker_group_count}" - template = "${file("${path.module}/templates/worker-role.tpl")}" + count = local.worker_group_count + template = file("${path.module}/templates/worker-role.tpl") - vars { - worker_role_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/${element(coalescelist(aws_iam_instance_profile.workers.*.role, data.aws_iam_instance_profile.custom_worker_group_iam_instance_profile.*.role_name), count.index)}" + vars = { + worker_role_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/${element( + coalescelist( + aws_iam_instance_profile.workers.*.role, + data.aws_iam_instance_profile.custom_worker_group_iam_instance_profile.*.role_name, + [""] + ), + count.index, + )}" } } data "template_file" "config_map_aws_auth" { - template = "${file("${path.module}/templates/config-map-aws-auth.yaml.tpl")}" + template = file("${path.module}/templates/config-map-aws-auth.yaml.tpl") - vars { - worker_role_arn = "${join("", distinct(concat(data.template_file.launch_template_worker_role_arns.*.rendered, data.template_file.worker_role_arns.*.rendered, data.template_file.launch_template_mixed_worker_role_arns.*.rendered)))}" - map_users = "${join("", data.template_file.map_users.*.rendered)}" - map_roles = "${join("", data.template_file.map_roles.*.rendered)}" - map_accounts = "${join("", data.template_file.map_accounts.*.rendered)}" + vars = { + worker_role_arn = join( + "", + distinct( + concat( + data.template_file.launch_template_worker_role_arns.*.rendered, + data.template_file.worker_role_arns.*.rendered, + data.template_file.launch_template_mixed_worker_role_arns.*.rendered, + ), + ), + ) + map_users = join("", data.template_file.map_users.*.rendered) + map_roles = join("", data.template_file.map_roles.*.rendered) + map_accounts = join("", data.template_file.map_accounts.*.rendered) } } data "template_file" "map_users" { - count = "${var.map_users_count}" - template = "${file("${path.module}/templates/config-map-aws-auth-map_users.yaml.tpl")}" + count = length(var.map_users) + template = file( + "${path.module}/templates/config-map-aws-auth-map_users.yaml.tpl", + ) - vars { - user_arn = "${lookup(var.map_users[count.index], "user_arn")}" - username = "${lookup(var.map_users[count.index], "username")}" - group = "${lookup(var.map_users[count.index], "group")}" + vars = { + user_arn = var.map_users[count.index]["user_arn"] + username = var.map_users[count.index]["username"] + group = var.map_users[count.index]["group"] } } data "template_file" "map_roles" { - count = "${var.map_roles_count}" - template = "${file("${path.module}/templates/config-map-aws-auth-map_roles.yaml.tpl")}" + count = length(var.map_roles) + template = file( + "${path.module}/templates/config-map-aws-auth-map_roles.yaml.tpl", + ) - vars { - role_arn = "${lookup(var.map_roles[count.index], "role_arn")}" - username = "${lookup(var.map_roles[count.index], "username")}" - group = "${lookup(var.map_roles[count.index], "group")}" + vars = { + role_arn = var.map_roles[count.index]["role_arn"] + username = var.map_roles[count.index]["username"] + group = var.map_roles[count.index]["group"] } } data "template_file" "map_accounts" { - count = "${var.map_accounts_count}" - template = "${file("${path.module}/templates/config-map-aws-auth-map_accounts.yaml.tpl")}" + count = length(var.map_accounts) + template = file( + "${path.module}/templates/config-map-aws-auth-map_accounts.yaml.tpl", + ) - vars { - account_number = "${element(var.map_accounts, count.index)}" + vars = { + account_number = var.map_accounts[count.index] } } + diff --git a/cluster.tf b/cluster.tf index 50b883d..e39eaf5 100644 --- a/cluster.tf +++ b/cluster.tf @@ -1,48 +1,53 @@ resource "aws_cloudwatch_log_group" "this" { name = "/aws/eks/${var.cluster_name}/cluster" - retention_in_days = "${var.cluster_log_retention_in_days}" + retention_in_days = var.cluster_log_retention_in_days - count = "${length(var.cluster_enabled_log_types) > 0 ? 1 : 0}" + count = length(var.cluster_enabled_log_types) > 0 ? 1 : 0 } resource "aws_eks_cluster" "this" { - name = "${var.cluster_name}" - enabled_cluster_log_types = "${var.cluster_enabled_log_types}" - role_arn = "${local.cluster_iam_role_arn}" - version = "${var.cluster_version}" + name = var.cluster_name + enabled_cluster_log_types = var.cluster_enabled_log_types + role_arn = local.cluster_iam_role_arn + version = var.cluster_version vpc_config { - security_group_ids = ["${local.cluster_security_group_id}"] - subnet_ids = ["${var.subnets}"] - endpoint_private_access = "${var.cluster_endpoint_private_access}" - endpoint_public_access = "${var.cluster_endpoint_public_access}" + security_group_ids = [local.cluster_security_group_id] + subnet_ids = var.subnets + endpoint_private_access = var.cluster_endpoint_private_access + endpoint_public_access = var.cluster_endpoint_public_access } timeouts { - create = "${var.cluster_create_timeout}" - delete = "${var.cluster_delete_timeout}" + create = var.cluster_create_timeout + delete = var.cluster_delete_timeout } depends_on = [ - "aws_iam_role_policy_attachment.cluster_AmazonEKSClusterPolicy", - "aws_iam_role_policy_attachment.cluster_AmazonEKSServicePolicy", - "aws_cloudwatch_log_group.this", + aws_iam_role_policy_attachment.cluster_AmazonEKSClusterPolicy, + aws_iam_role_policy_attachment.cluster_AmazonEKSServicePolicy, + aws_cloudwatch_log_group.this ] } resource "aws_security_group" "cluster" { - count = "${var.cluster_create_security_group ? 1 : 0}" - name_prefix = "${var.cluster_name}" + count = var.cluster_create_security_group ? 1 : 0 + name_prefix = var.cluster_name description = "EKS cluster security group." - vpc_id = "${var.vpc_id}" - tags = "${merge(var.tags, map("Name", "${var.cluster_name}-eks_cluster_sg"))}" + vpc_id = var.vpc_id + tags = merge( + var.tags, + { + "Name" = "${var.cluster_name}-eks_cluster_sg" + }, + ) } resource "aws_security_group_rule" "cluster_egress_internet" { - count = "${var.cluster_create_security_group ? 1 : 0}" + count = var.cluster_create_security_group ? 1 : 0 description = "Allow cluster egress access to the Internet." protocol = "-1" - security_group_id = "${aws_security_group.cluster.id}" + security_group_id = aws_security_group.cluster[0].id cidr_blocks = ["0.0.0.0/0"] from_port = 0 to_port = 0 @@ -50,34 +55,35 @@ resource "aws_security_group_rule" "cluster_egress_internet" { } resource "aws_security_group_rule" "cluster_https_worker_ingress" { - count = "${var.cluster_create_security_group ? 1 : 0}" + count = var.cluster_create_security_group ? 1 : 0 description = "Allow pods to communicate with the EKS cluster API." protocol = "tcp" - security_group_id = "${aws_security_group.cluster.id}" - source_security_group_id = "${local.worker_security_group_id}" + security_group_id = aws_security_group.cluster[0].id + source_security_group_id = local.worker_security_group_id from_port = 443 to_port = 443 type = "ingress" } resource "aws_iam_role" "cluster" { - count = "${var.manage_cluster_iam_resources ? 1 : 0}" - name_prefix = "${var.cluster_name}" - assume_role_policy = "${data.aws_iam_policy_document.cluster_assume_role_policy.json}" - permissions_boundary = "${var.permissions_boundary}" - path = "${var.iam_path}" + count = var.manage_cluster_iam_resources ? 1 : 0 + name_prefix = var.cluster_name + assume_role_policy = data.aws_iam_policy_document.cluster_assume_role_policy.json + permissions_boundary = var.permissions_boundary + path = var.iam_path force_detach_policies = true - tags = "${var.tags}" + tags = var.tags } resource "aws_iam_role_policy_attachment" "cluster_AmazonEKSClusterPolicy" { - count = "${var.manage_cluster_iam_resources ? 1 : 0}" + count = var.manage_cluster_iam_resources ? 1 : 0 policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy" - role = "${aws_iam_role.cluster.name}" + role = aws_iam_role.cluster[0].name } resource "aws_iam_role_policy_attachment" "cluster_AmazonEKSServicePolicy" { - count = "${var.manage_cluster_iam_resources ? 1 : 0}" + count = var.manage_cluster_iam_resources ? 1 : 0 policy_arn = "arn:aws:iam::aws:policy/AmazonEKSServicePolicy" - role = "${aws_iam_role.cluster.name}" + role = aws_iam_role.cluster[0].name } + diff --git a/data.tf b/data.tf index a103b8c..f7f7a33 100644 --- a/data.tf +++ b/data.tf @@ -1,4 +1,5 @@ -data "aws_region" "current" {} +data "aws_region" "current" { +} data "aws_iam_policy_document" "workers_assume_role_policy" { statement { @@ -43,95 +44,169 @@ data "aws_iam_policy_document" "cluster_assume_role_policy" { } data "template_file" "kubeconfig" { - template = "${file("${path.module}/templates/kubeconfig.tpl")}" + template = file("${path.module}/templates/kubeconfig.tpl") - vars { - kubeconfig_name = "${local.kubeconfig_name}" - endpoint = "${aws_eks_cluster.this.endpoint}" - region = "${data.aws_region.current.name}" - cluster_auth_base64 = "${aws_eks_cluster.this.certificate_authority.0.data}" - aws_authenticator_command = "${var.kubeconfig_aws_authenticator_command}" - aws_authenticator_command_args = "${length(var.kubeconfig_aws_authenticator_command_args) > 0 ? " - ${join("\n - ", var.kubeconfig_aws_authenticator_command_args)}" : " - ${join("\n - ", formatlist("\"%s\"", list("token", "-i", aws_eks_cluster.this.name)))}"}" - aws_authenticator_additional_args = "${length(var.kubeconfig_aws_authenticator_additional_args) > 0 ? " - ${join("\n - ", var.kubeconfig_aws_authenticator_additional_args)}" : ""}" - aws_authenticator_env_variables = "${length(var.kubeconfig_aws_authenticator_env_variables) > 0 ? " env:\n${join("\n", data.template_file.aws_authenticator_env_variables.*.rendered)}" : ""}" + vars = { + kubeconfig_name = local.kubeconfig_name + endpoint = aws_eks_cluster.this.endpoint + region = data.aws_region.current.name + cluster_auth_base64 = aws_eks_cluster.this.certificate_authority[0].data + aws_authenticator_command = var.kubeconfig_aws_authenticator_command + aws_authenticator_command_args = length(var.kubeconfig_aws_authenticator_command_args) > 0 ? " - ${join( + "\n - ", + var.kubeconfig_aws_authenticator_command_args, + )}" : " - ${join( + "\n - ", + formatlist("\"%s\"", ["token", "-i", aws_eks_cluster.this.name]), + )}" + aws_authenticator_additional_args = length(var.kubeconfig_aws_authenticator_additional_args) > 0 ? " - ${join( + "\n - ", + var.kubeconfig_aws_authenticator_additional_args, + )}" : "" + aws_authenticator_env_variables = length(var.kubeconfig_aws_authenticator_env_variables) > 0 ? " env:\n${join( + "\n", + data.template_file.aws_authenticator_env_variables.*.rendered, + )}" : "" } } data "template_file" "aws_authenticator_env_variables" { - count = "${length(var.kubeconfig_aws_authenticator_env_variables)}" + count = length(var.kubeconfig_aws_authenticator_env_variables) template = < 10250 ? 1 : 0 : 0 description = "Allow workers Kubelets to receive communication from the cluster control plane." protocol = "tcp" - security_group_id = "${aws_security_group.workers.id}" - source_security_group_id = "${local.cluster_security_group_id}" + security_group_id = aws_security_group.workers[0].id + source_security_group_id = local.cluster_security_group_id from_port = 10250 to_port = 10250 type = "ingress" } resource "aws_security_group_rule" "workers_ingress_cluster_https" { - count = "${var.worker_create_security_group ? 1 : 0}" + count = var.worker_create_security_group ? 1 : 0 description = "Allow pods running extension API servers on port 443 to receive communication from cluster control plane." protocol = "tcp" - security_group_id = "${aws_security_group.workers.id}" - source_security_group_id = "${local.cluster_security_group_id}" + security_group_id = aws_security_group.workers[0].id + source_security_group_id = local.cluster_security_group_id from_port = 443 to_port = 443 type = "ingress" } resource "aws_iam_role" "workers" { - count = "${var.manage_worker_iam_resources ? 1 : 0}" - name_prefix = "${aws_eks_cluster.this.name}" - assume_role_policy = "${data.aws_iam_policy_document.workers_assume_role_policy.json}" - permissions_boundary = "${var.permissions_boundary}" - path = "${var.iam_path}" + count = var.manage_worker_iam_resources ? 1 : 0 + name_prefix = aws_eks_cluster.this.name + assume_role_policy = data.aws_iam_policy_document.workers_assume_role_policy.json + permissions_boundary = var.permissions_boundary + path = var.iam_path force_detach_policies = true } resource "aws_iam_instance_profile" "workers" { - count = "${var.manage_worker_iam_resources ? var.worker_group_count : 0}" - name_prefix = "${aws_eks_cluster.this.name}" - role = "${lookup(var.worker_groups[count.index], "iam_role_id", lookup(local.workers_group_defaults, "iam_role_id"))}" + count = var.manage_worker_iam_resources ? local.worker_group_count : 0 + name_prefix = aws_eks_cluster.this.name + role = lookup( + var.worker_groups[count.index], + "iam_role_id", + local.workers_group_defaults["iam_role_id"], + ) - path = "${var.iam_path}" + path = var.iam_path } resource "aws_iam_role_policy_attachment" "workers_AmazonEKSWorkerNodePolicy" { - count = "${var.manage_worker_iam_resources ? 1 : 0}" + count = var.manage_worker_iam_resources ? 1 : 0 policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy" - role = "${aws_iam_role.workers.name}" + role = aws_iam_role.workers[0].name } resource "aws_iam_role_policy_attachment" "workers_AmazonEKS_CNI_Policy" { - count = "${var.manage_worker_iam_resources ? 1 : 0}" + count = var.manage_worker_iam_resources ? 1 : 0 policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy" - role = "${aws_iam_role.workers.name}" + role = aws_iam_role.workers[0].name } resource "aws_iam_role_policy_attachment" "workers_AmazonEC2ContainerRegistryReadOnly" { - count = "${var.manage_worker_iam_resources ? 1 : 0}" + count = var.manage_worker_iam_resources ? 1 : 0 policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly" - role = "${aws_iam_role.workers.name}" + role = aws_iam_role.workers[0].name } resource "aws_iam_role_policy_attachment" "workers_additional_policies" { - count = "${var.manage_worker_iam_resources ? var.workers_additional_policies_count : 0}" - role = "${aws_iam_role.workers.name}" - policy_arn = "${var.workers_additional_policies[count.index]}" + count = var.manage_worker_iam_resources ? length(var.workers_additional_policies) : 0 + role = aws_iam_role.workers[0].name + policy_arn = var.workers_additional_policies[count.index] } resource "null_resource" "tags_as_list_of_maps" { - count = "${length(keys(var.tags))}" + count = length(keys(var.tags)) triggers = { - key = "${element(keys(var.tags), count.index)}" - value = "${element(values(var.tags), count.index)}" + key = keys(var.tags)[count.index] + value = values(var.tags)[count.index] propagate_at_launch = "true" } } resource "aws_iam_role_policy_attachment" "workers_autoscaling" { - count = "${var.manage_worker_iam_resources ? 1 : 0}" - policy_arn = "${aws_iam_policy.worker_autoscaling.arn}" - role = "${aws_iam_role.workers.name}" + count = var.manage_worker_iam_resources ? 1 : 0 + policy_arn = aws_iam_policy.worker_autoscaling[0].arn + role = aws_iam_role.workers[0].name } resource "aws_iam_policy" "worker_autoscaling" { - count = "${var.manage_worker_iam_resources ? 1 : 0}" + count = var.manage_worker_iam_resources ? 1 : 0 name_prefix = "eks-worker-autoscaling-${aws_eks_cluster.this.name}" description = "EKS worker node autoscaling policy for cluster ${aws_eks_cluster.this.name}" - policy = "${data.aws_iam_policy_document.worker_autoscaling.json}" - path = "${var.iam_path}" + policy = data.aws_iam_policy_document.worker_autoscaling.json + path = var.iam_path } data "aws_iam_policy_document" "worker_autoscaling" { @@ -232,3 +384,4 @@ data "aws_iam_policy_document" "worker_autoscaling" { } } } + diff --git a/workers_launch_template.tf b/workers_launch_template.tf index 61d0442..931c18c 100644 --- a/workers_launch_template.tf +++ b/workers_launch_template.tf @@ -1,92 +1,264 @@ # Worker Groups using Launch Templates resource "aws_autoscaling_group" "workers_launch_template" { - count = "${var.worker_group_launch_template_count}" - name_prefix = "${aws_eks_cluster.this.name}-${lookup(var.worker_groups_launch_template[count.index], "name", count.index)}" - desired_capacity = "${lookup(var.worker_groups_launch_template[count.index], "asg_desired_capacity", local.workers_group_defaults["asg_desired_capacity"])}" - max_size = "${lookup(var.worker_groups_launch_template[count.index], "asg_max_size", local.workers_group_defaults["asg_max_size"])}" - min_size = "${lookup(var.worker_groups_launch_template[count.index], "asg_min_size", local.workers_group_defaults["asg_min_size"])}" - force_delete = "${lookup(var.worker_groups_launch_template[count.index], "asg_force_delete", local.workers_group_defaults["asg_force_delete"])}" - target_group_arns = ["${compact(split(",", coalesce(lookup(var.worker_groups_launch_template[count.index], "target_group_arns", ""), local.workers_group_defaults["target_group_arns"])))}"] - service_linked_role_arn = "${lookup(var.worker_groups_launch_template[count.index], "service_linked_role_arn", local.workers_group_defaults["service_linked_role_arn"])}" - vpc_zone_identifier = ["${split(",", coalesce(lookup(var.worker_groups_launch_template[count.index], "subnets", ""), local.workers_group_defaults["subnets"]))}"] - protect_from_scale_in = "${lookup(var.worker_groups_launch_template[count.index], "protect_from_scale_in", local.workers_group_defaults["protect_from_scale_in"])}" - suspended_processes = ["${compact(split(",", coalesce(lookup(var.worker_groups_launch_template[count.index], "suspended_processes", ""), local.workers_group_defaults["suspended_processes"])))}"] - enabled_metrics = ["${compact(split(",", coalesce(lookup(var.worker_groups_launch_template[count.index], "enabled_metrics", ""), local.workers_group_defaults["enabled_metrics"])))}"] - placement_group = "${lookup(var.worker_groups_launch_template[count.index], "placement_group", local.workers_group_defaults["placement_group"])}" - termination_policies = ["${compact(split(",", coalesce(lookup(var.worker_groups_launch_template[count.index], "termination_policies", ""), local.workers_group_defaults["termination_policies"])))}"] + count = local.worker_group_launch_template_count + name_prefix = "${aws_eks_cluster.this.name}-${lookup( + var.worker_groups_launch_template[count.index], + "name", + count.index, + )}" + desired_capacity = lookup( + var.worker_groups_launch_template[count.index], + "asg_desired_capacity", + local.workers_group_defaults["asg_desired_capacity"], + ) + max_size = lookup( + var.worker_groups_launch_template[count.index], + "asg_max_size", + local.workers_group_defaults["asg_max_size"], + ) + min_size = lookup( + var.worker_groups_launch_template[count.index], + "asg_min_size", + local.workers_group_defaults["asg_min_size"], + ) + force_delete = lookup( + var.worker_groups_launch_template[count.index], + "asg_force_delete", + local.workers_group_defaults["asg_force_delete"], + ) + target_group_arns = lookup( + var.worker_groups_launch_template[count.index], + "target_group_arns", + local.workers_group_defaults["target_group_arns"] + ) + service_linked_role_arn = lookup( + var.worker_groups_launch_template[count.index], + "service_linked_role_arn", + local.workers_group_defaults["service_linked_role_arn"], + ) + vpc_zone_identifier = lookup( + var.worker_groups_launch_template[count.index], + "subnets", + local.workers_group_defaults["subnets"] + ) + protect_from_scale_in = lookup( + var.worker_groups_launch_template[count.index], + "protect_from_scale_in", + local.workers_group_defaults["protect_from_scale_in"], + ) + suspended_processes = lookup( + var.worker_groups_launch_template[count.index], + "suspended_processes", + local.workers_group_defaults["suspended_processes"] + ) + enabled_metrics = lookup( + var.worker_groups_launch_template[count.index], + "enabled_metrics", + local.workers_group_defaults["enabled_metrics"] + ) + placement_group = lookup( + var.worker_groups_launch_template[count.index], + "placement_group", + local.workers_group_defaults["placement_group"], + ) + termination_policies = lookup( + var.worker_groups_launch_template[count.index], + "termination_policies", + local.workers_group_defaults["termination_policies"] + ) launch_template { - id = "${element(aws_launch_template.workers_launch_template.*.id, count.index)}" - version = "${lookup(var.worker_groups_launch_template[count.index], "launch_template_version", local.workers_group_defaults["launch_template_version"])}" + id = aws_launch_template.workers_launch_template.*.id[count.index] + version = lookup( + var.worker_groups_launch_template[count.index], + "launch_template_version", + local.workers_group_defaults["launch_template_version"], + ) } - tags = ["${concat( - list( - map("key", "Name", "value", "${aws_eks_cluster.this.name}-${lookup(var.worker_groups_launch_template[count.index], "name", count.index)}-eks_asg", "propagate_at_launch", true), - map("key", "kubernetes.io/cluster/${aws_eks_cluster.this.name}", "value", "owned", "propagate_at_launch", true), - map("key", "k8s.io/cluster-autoscaler/${lookup(var.worker_groups_launch_template[count.index], "autoscaling_enabled", local.workers_group_defaults["autoscaling_enabled"]) == 1 ? "enabled" : "disabled"}", "value", "true", "propagate_at_launch", false), - map("key", "k8s.io/cluster-autoscaler/${aws_eks_cluster.this.name}", "value", "", "propagate_at_launch", false), - map("key", "k8s.io/cluster-autoscaler/node-template/resources/ephemeral-storage", "value", "${lookup(var.worker_groups_launch_template[count.index], "root_volume_size", local.workers_group_defaults["root_volume_size"])}Gi", "propagate_at_launch", false) - ), + tags = concat( + [ + { + "key" = "Name" + "value" = "${aws_eks_cluster.this.name}-${lookup( + var.worker_groups_launch_template[count.index], + "name", + count.index, + )}-eks_asg" + "propagate_at_launch" = true + }, + { + "key" = "kubernetes.io/cluster/${aws_eks_cluster.this.name}" + "value" = "owned" + "propagate_at_launch" = true + }, + { + "key" = "k8s.io/cluster-autoscaler/${lookup( + var.worker_groups_launch_template[count.index], + "autoscaling_enabled", + local.workers_group_defaults["autoscaling_enabled"], + ) ? "enabled" : "disabled"}" + "value" = "true" + "propagate_at_launch" = false + }, + { + "key" = "k8s.io/cluster-autoscaler/${aws_eks_cluster.this.name}" + "value" = aws_eks_cluster.this.name + "propagate_at_launch" = false + }, + { + "key" = "k8s.io/cluster-autoscaler/node-template/resources/ephemeral-storage" + "value" = "${lookup( + var.worker_groups_launch_template[count.index], + "root_volume_size", + local.workers_group_defaults["root_volume_size"], + )}Gi" + "propagate_at_launch" = false + }, + ], local.asg_tags, - var.worker_group_tags[contains(keys(var.worker_group_tags), "${lookup(var.worker_groups_launch_template[count.index], "name", count.index)}") ? "${lookup(var.worker_groups_launch_template[count.index], "name", count.index)}" : "default"]) - }"] + lookup( + var.worker_groups_launch_template[count.index], + "tags", + local.workers_group_defaults["tags"] + ) + ) lifecycle { create_before_destroy = true - ignore_changes = ["desired_capacity"] + ignore_changes = [desired_capacity] } } resource "aws_launch_template" "workers_launch_template" { - count = "${var.worker_group_launch_template_count}" - name_prefix = "${aws_eks_cluster.this.name}-${lookup(var.worker_groups_launch_template[count.index], "name", count.index)}" + count = local.worker_group_launch_template_count + name_prefix = "${aws_eks_cluster.this.name}-${lookup( + var.worker_groups_launch_template[count.index], + "name", + count.index, + )}" network_interfaces { - associate_public_ip_address = "${lookup(var.worker_groups_launch_template[count.index], "public_ip", local.workers_group_defaults["public_ip"])}" - delete_on_termination = "${lookup(var.worker_groups_launch_template[count.index], "eni_delete", local.workers_group_defaults["eni_delete"])}" - - security_groups = [ - "${local.worker_security_group_id}", - "${var.worker_additional_security_group_ids}", - "${compact(split(",", lookup(var.worker_groups_launch_template[count.index], "additional_security_group_ids", local.workers_group_defaults["additional_security_group_ids"])))}", - ] + associate_public_ip_address = lookup( + var.worker_groups_launch_template[count.index], + "public_ip", + local.workers_group_defaults["public_ip"], + ) + delete_on_termination = lookup( + var.worker_groups_launch_template[count.index], + "eni_delete", + local.workers_group_defaults["eni_delete"], + ) + security_groups = flatten([ + local.worker_security_group_id, + var.worker_additional_security_group_ids, + lookup( + var.worker_groups_launch_template[count.index], + "additional_security_group_ids", + local.workers_group_defaults["additional_security_group_ids"], + ), + ]) } iam_instance_profile { - name = "${element(coalescelist(aws_iam_instance_profile.workers_launch_template.*.name, data.aws_iam_instance_profile.custom_worker_group_launch_template_iam_instance_profile.*.name), count.index)}" + name = coalescelist( + aws_iam_instance_profile.workers_launch_template.*.name, + data.aws_iam_instance_profile.custom_worker_group_launch_template_iam_instance_profile.*.name, + )[count.index] } - image_id = "${lookup(var.worker_groups_launch_template[count.index], "ami_id", local.workers_group_defaults["ami_id"])}" - instance_type = "${lookup(var.worker_groups_launch_template[count.index], "instance_type", local.workers_group_defaults["instance_type"])}" - key_name = "${lookup(var.worker_groups_launch_template[count.index], "key_name", local.workers_group_defaults["key_name"])}" - user_data = "${base64encode(element(data.template_file.launch_template_userdata.*.rendered, count.index))}" - ebs_optimized = "${lookup(var.worker_groups_launch_template[count.index], "ebs_optimized", lookup(local.ebs_optimized, lookup(var.worker_groups_launch_template[count.index], "instance_type", local.workers_group_defaults["instance_type"]), false))}" + image_id = lookup( + var.worker_groups_launch_template[count.index], + "ami_id", + local.workers_group_defaults["ami_id"], + ) + instance_type = lookup( + var.worker_groups_launch_template[count.index], + "instance_type", + local.workers_group_defaults["instance_type"], + ) + key_name = lookup( + var.worker_groups_launch_template[count.index], + "key_name", + local.workers_group_defaults["key_name"], + ) + user_data = base64encode( + data.template_file.launch_template_userdata.*.rendered[count.index], + ) + ebs_optimized = lookup( + var.worker_groups_launch_template[count.index], + "ebs_optimized", + lookup( + local.ebs_optimized, + lookup( + var.worker_groups_launch_template[count.index], + "instance_type", + local.workers_group_defaults["instance_type"], + ), + false, + ), + ) monitoring { - enabled = "${lookup(var.worker_groups_launch_template[count.index], "enable_monitoring", local.workers_group_defaults["enable_monitoring"])}" + enabled = lookup( + var.worker_groups_launch_template[count.index], + "enable_monitoring", + local.workers_group_defaults["enable_monitoring"], + ) } placement { - tenancy = "${lookup(var.worker_groups_launch_template[count.index], "launch_template_placement_tenancy", local.workers_group_defaults["launch_template_placement_tenancy"])}" - group_name = "${lookup(var.worker_groups_launch_template[count.index], "launch_template_placement_group", local.workers_group_defaults["launch_template_placement_group"])}" + tenancy = lookup( + var.worker_groups_launch_template[count.index], + "launch_template_placement_tenancy", + local.workers_group_defaults["launch_template_placement_tenancy"], + ) + group_name = lookup( + var.worker_groups_launch_template[count.index], + "launch_template_placement_group", + local.workers_group_defaults["launch_template_placement_group"], + ) } block_device_mappings { - device_name = "${lookup(var.worker_groups_launch_template[count.index], "root_block_device_name", local.workers_group_defaults["root_block_device_name"])}" + device_name = lookup( + var.worker_groups_launch_template[count.index], + "root_block_device_name", + local.workers_group_defaults["root_block_device_name"], + ) ebs { - volume_size = "${lookup(var.worker_groups_launch_template[count.index], "root_volume_size", local.workers_group_defaults["root_volume_size"])}" - volume_type = "${lookup(var.worker_groups_launch_template[count.index], "root_volume_type", local.workers_group_defaults["root_volume_type"])}" - iops = "${lookup(var.worker_groups_launch_template[count.index], "root_iops", local.workers_group_defaults["root_iops"])}" - encrypted = "${lookup(var.worker_groups_launch_template[count.index], "root_encrypted", local.workers_group_defaults["root_encrypted"])}" - kms_key_id = "${lookup(var.worker_groups_launch_template[count.index], "root_kms_key_id", local.workers_group_defaults["root_kms_key_id"])}" + volume_size = lookup( + var.worker_groups_launch_template[count.index], + "root_volume_size", + local.workers_group_defaults["root_volume_size"], + ) + volume_type = lookup( + var.worker_groups_launch_template[count.index], + "root_volume_type", + local.workers_group_defaults["root_volume_type"], + ) + iops = lookup( + var.worker_groups_launch_template[count.index], + "root_iops", + local.workers_group_defaults["root_iops"], + ) + encrypted = lookup( + var.worker_groups_launch_template[count.index], + "root_encrypted", + local.workers_group_defaults["root_encrypted"], + ) + kms_key_id = lookup( + var.worker_groups_launch_template[count.index], + "root_kms_key_id", + local.workers_group_defaults["root_kms_key_id"], + ) delete_on_termination = true } } - tags = "${var.tags}" + tags = var.tags lifecycle { create_before_destroy = true @@ -94,8 +266,13 @@ resource "aws_launch_template" "workers_launch_template" { } resource "aws_iam_instance_profile" "workers_launch_template" { - count = "${var.manage_worker_iam_resources ? var.worker_group_launch_template_count : 0}" - name_prefix = "${aws_eks_cluster.this.name}" - role = "${lookup(var.worker_groups_launch_template[count.index], "iam_role_id", lookup(local.workers_group_defaults, "iam_role_id"))}" - path = "${var.iam_path}" + count = var.manage_worker_iam_resources ? local.worker_group_launch_template_count : 0 + name_prefix = aws_eks_cluster.this.name + role = lookup( + var.worker_groups_launch_template[count.index], + "iam_role_id", + local.workers_group_defaults["iam_role_id"], + ) + path = var.iam_path } + diff --git a/workers_launch_template_mixed.tf b/workers_launch_template_mixed.tf index 02d8ba2..7de2c9a 100644 --- a/workers_launch_template_mixed.tf +++ b/workers_launch_template_mixed.tf @@ -1,123 +1,328 @@ # Worker Groups using Launch Templates with mixed instances policy resource "aws_autoscaling_group" "workers_launch_template_mixed" { - count = "${var.worker_group_launch_template_mixed_count}" - name_prefix = "${aws_eks_cluster.this.name}-${lookup(var.worker_groups_launch_template_mixed[count.index], "name", count.index)}" - desired_capacity = "${lookup(var.worker_groups_launch_template_mixed[count.index], "asg_desired_capacity", local.workers_group_defaults["asg_desired_capacity"])}" - max_size = "${lookup(var.worker_groups_launch_template_mixed[count.index], "asg_max_size", local.workers_group_defaults["asg_max_size"])}" - min_size = "${lookup(var.worker_groups_launch_template_mixed[count.index], "asg_min_size", local.workers_group_defaults["asg_min_size"])}" - force_delete = "${lookup(var.worker_groups_launch_template_mixed[count.index], "asg_force_delete", local.workers_group_defaults["asg_force_delete"])}" - target_group_arns = ["${compact(split(",", coalesce(lookup(var.worker_groups_launch_template_mixed[count.index], "target_group_arns", ""), local.workers_group_defaults["target_group_arns"])))}"] - service_linked_role_arn = "${lookup(var.worker_groups_launch_template_mixed[count.index], "service_linked_role_arn", local.workers_group_defaults["service_linked_role_arn"])}" - vpc_zone_identifier = ["${split(",", coalesce(lookup(var.worker_groups_launch_template_mixed[count.index], "subnets", ""), local.workers_group_defaults["subnets"]))}"] - protect_from_scale_in = "${lookup(var.worker_groups_launch_template_mixed[count.index], "protect_from_scale_in", local.workers_group_defaults["protect_from_scale_in"])}" - suspended_processes = ["${compact(split(",", coalesce(lookup(var.worker_groups_launch_template_mixed[count.index], "suspended_processes", ""), local.workers_group_defaults["suspended_processes"])))}"] - enabled_metrics = ["${compact(split(",", coalesce(lookup(var.worker_groups_launch_template_mixed[count.index], "enabled_metrics", ""), local.workers_group_defaults["enabled_metrics"])))}"] - placement_group = "${lookup(var.worker_groups_launch_template_mixed[count.index], "placement_group", local.workers_group_defaults["placement_group"])}" - termination_policies = ["${compact(split(",", coalesce(lookup(var.worker_groups_launch_template_mixed[count.index], "termination_policies", ""), local.workers_group_defaults["termination_policies"])))}"] + count = local.worker_group_launch_template_mixed_count + name_prefix = "${aws_eks_cluster.this.name}-${lookup( + var.worker_groups_launch_template_mixed[count.index], + "name", + count.index, + )}" + desired_capacity = lookup( + var.worker_groups_launch_template_mixed[count.index], + "asg_desired_capacity", + local.workers_group_defaults["asg_desired_capacity"], + ) + max_size = lookup( + var.worker_groups_launch_template_mixed[count.index], + "asg_max_size", + local.workers_group_defaults["asg_max_size"], + ) + min_size = lookup( + var.worker_groups_launch_template_mixed[count.index], + "asg_min_size", + local.workers_group_defaults["asg_min_size"], + ) + force_delete = lookup( + var.worker_groups_launch_template_mixed[count.index], + "asg_force_delete", + local.workers_group_defaults["asg_force_delete"], + ) + target_group_arns = lookup( + var.worker_groups_launch_template_mixed[count.index], + "target_group_arns", + local.workers_group_defaults["target_group_arns"] + ) + service_linked_role_arn = lookup( + var.worker_groups_launch_template_mixed[count.index], + "service_linked_role_arn", + local.workers_group_defaults["service_linked_role_arn"], + ) + vpc_zone_identifier = lookup( + var.worker_groups_launch_template_mixed[count.index], + "subnets", + local.workers_group_defaults["subnets"] + ) + protect_from_scale_in = lookup( + var.worker_groups_launch_template_mixed[count.index], + "protect_from_scale_in", + local.workers_group_defaults["protect_from_scale_in"], + ) + suspended_processes = lookup( + var.worker_groups_launch_template_mixed[count.index], + "suspended_processes", + local.workers_group_defaults["suspended_processes"] + ) + enabled_metrics = lookup( + var.worker_groups_launch_template_mixed[count.index], + "enabled_metrics", + local.workers_group_defaults["enabled_metrics"] + ) + placement_group = lookup( + var.worker_groups_launch_template_mixed[count.index], + "placement_group", + local.workers_group_defaults["placement_group"], + ) + termination_policies = lookup( + var.worker_groups_launch_template_mixed[count.index], + "termination_policies", + local.workers_group_defaults["termination_policies"] + ) mixed_instances_policy { instances_distribution { - on_demand_allocation_strategy = "${lookup(var.worker_groups_launch_template_mixed[count.index], "on_demand_allocation_strategy", local.workers_group_defaults["on_demand_allocation_strategy"])}" - on_demand_base_capacity = "${lookup(var.worker_groups_launch_template_mixed[count.index], "on_demand_base_capacity", local.workers_group_defaults["on_demand_base_capacity"])}" - on_demand_percentage_above_base_capacity = "${lookup(var.worker_groups_launch_template_mixed[count.index], "on_demand_percentage_above_base_capacity", local.workers_group_defaults["on_demand_percentage_above_base_capacity"])}" - spot_allocation_strategy = "${lookup(var.worker_groups_launch_template_mixed[count.index], "spot_allocation_strategy", local.workers_group_defaults["spot_allocation_strategy"])}" - spot_instance_pools = "${lookup(var.worker_groups_launch_template_mixed[count.index], "spot_instance_pools", local.workers_group_defaults["spot_instance_pools"])}" - spot_max_price = "${lookup(var.worker_groups_launch_template_mixed[count.index], "spot_max_price", local.workers_group_defaults["spot_max_price"])}" + on_demand_allocation_strategy = lookup( + var.worker_groups_launch_template_mixed[count.index], + "on_demand_allocation_strategy", + local.workers_group_defaults["on_demand_allocation_strategy"], + ) + on_demand_base_capacity = lookup( + var.worker_groups_launch_template_mixed[count.index], + "on_demand_base_capacity", + local.workers_group_defaults["on_demand_base_capacity"], + ) + on_demand_percentage_above_base_capacity = lookup( + var.worker_groups_launch_template_mixed[count.index], + "on_demand_percentage_above_base_capacity", + local.workers_group_defaults["on_demand_percentage_above_base_capacity"], + ) + spot_allocation_strategy = lookup( + var.worker_groups_launch_template_mixed[count.index], + "spot_allocation_strategy", + local.workers_group_defaults["spot_allocation_strategy"], + ) + spot_instance_pools = lookup( + var.worker_groups_launch_template_mixed[count.index], + "spot_instance_pools", + local.workers_group_defaults["spot_instance_pools"], + ) + spot_max_price = lookup( + var.worker_groups_launch_template_mixed[count.index], + "spot_max_price", + local.workers_group_defaults["spot_max_price"], + ) } launch_template { launch_template_specification { - launch_template_id = "${element(aws_launch_template.workers_launch_template_mixed.*.id, count.index)}" - version = "${lookup(var.worker_groups_launch_template_mixed[count.index], "launch_template_version", local.workers_group_defaults["launch_template_version"])}" + launch_template_id = aws_launch_template.workers_launch_template_mixed.*.id[count.index] + version = lookup( + var.worker_groups_launch_template_mixed[count.index], + "launch_template_version", + local.workers_group_defaults["launch_template_version"], + ) } - override { - instance_type = "${lookup(var.worker_groups_launch_template_mixed[count.index], "override_instance_type_1", local.workers_group_defaults["override_instance_type_1"])}" + dynamic "override" { + for_each = lookup( + var.worker_groups_launch_template_mixed[count.index], + "override_instance_types", + local.workers_group_defaults["override_instance_types"] + ) + + content { + instance_type = override.value + } } - override { - instance_type = "${lookup(var.worker_groups_launch_template_mixed[count.index], "override_instance_type_2", local.workers_group_defaults["override_instance_type_2"])}" - } - - override { - instance_type = "${lookup(var.worker_groups_launch_template_mixed[count.index], "override_instance_type_3", local.workers_group_defaults["override_instance_type_3"])}" - } - - override { - instance_type = "${lookup(var.worker_groups_launch_template_mixed[count.index], "override_instance_type_4", local.workers_group_defaults["override_instance_type_4"])}" - } } } - tags = ["${concat( - list( - map("key", "Name", "value", "${aws_eks_cluster.this.name}-${lookup(var.worker_groups_launch_template_mixed[count.index], "name", count.index)}-eks_asg", "propagate_at_launch", true), - map("key", "kubernetes.io/cluster/${aws_eks_cluster.this.name}", "value", "owned", "propagate_at_launch", true), - map("key", "k8s.io/cluster-autoscaler/${lookup(var.worker_groups_launch_template_mixed[count.index], "autoscaling_enabled", local.workers_group_defaults["autoscaling_enabled"]) == 1 ? "enabled" : "disabled"}", "value", "true", "propagate_at_launch", false), - map("key", "k8s.io/cluster-autoscaler/${aws_eks_cluster.this.name}", "value", "", "propagate_at_launch", false), - map("key", "k8s.io/cluster-autoscaler/node-template/resources/ephemeral-storage", "value", "${lookup(var.worker_groups_launch_template_mixed[count.index], "root_volume_size", local.workers_group_defaults["root_volume_size"])}Gi", "propagate_at_launch", false) - ), + tags = concat( + [ + { + "key" = "Name" + "value" = "${aws_eks_cluster.this.name}-${lookup( + var.worker_groups_launch_template_mixed[count.index], + "name", + count.index, + )}-eks_asg" + "propagate_at_launch" = true + }, + { + "key" = "kubernetes.io/cluster/${aws_eks_cluster.this.name}" + "value" = "owned" + "propagate_at_launch" = true + }, + { + "key" = "k8s.io/cluster-autoscaler/${lookup( + var.worker_groups_launch_template_mixed[count.index], + "autoscaling_enabled", + local.workers_group_defaults["autoscaling_enabled"], + ) ? "enabled" : "disabled"}" + "value" = "true" + "propagate_at_launch" = false + }, + { + "key" = "k8s.io/cluster-autoscaler/${aws_eks_cluster.this.name}" + "value" = aws_eks_cluster.this.name + "propagate_at_launch" = false + }, + { + "key" = "k8s.io/cluster-autoscaler/node-template/resources/ephemeral-storage" + "value" = "${lookup( + var.worker_groups_launch_template_mixed[count.index], + "root_volume_size", + local.workers_group_defaults["root_volume_size"], + )}Gi" + "propagate_at_launch" = false + }, + ], local.asg_tags, - var.worker_group_tags[contains(keys(var.worker_group_tags), "${lookup(var.worker_groups_launch_template_mixed[count.index], "name", count.index)}") ? "${lookup(var.worker_groups_launch_template_mixed[count.index], "name", count.index)}" : "default"]) - }"] + lookup( + var.worker_groups_launch_template_mixed[count.index], + "tags", + local.workers_group_defaults["tags"] + ) + ) lifecycle { create_before_destroy = true - ignore_changes = ["desired_capacity"] + ignore_changes = [desired_capacity] } } resource "aws_launch_template" "workers_launch_template_mixed" { - count = "${var.worker_group_launch_template_mixed_count}" - name_prefix = "${aws_eks_cluster.this.name}-${lookup(var.worker_groups_launch_template_mixed[count.index], "name", count.index)}" + count = local.worker_group_launch_template_mixed_count + name_prefix = "${aws_eks_cluster.this.name}-${lookup( + var.worker_groups_launch_template_mixed[count.index], + "name", + count.index, + )}" network_interfaces { - associate_public_ip_address = "${lookup(var.worker_groups_launch_template_mixed[count.index], "public_ip", local.workers_group_defaults["public_ip"])}" - delete_on_termination = "${lookup(var.worker_groups_launch_template_mixed[count.index], "eni_delete", local.workers_group_defaults["eni_delete"])}" - security_groups = ["${local.worker_security_group_id}", "${var.worker_additional_security_group_ids}", "${compact(split(",", lookup(var.worker_groups_launch_template_mixed[count.index], "additional_security_group_ids", local.workers_group_defaults["additional_security_group_ids"])))}"] + associate_public_ip_address = lookup( + var.worker_groups_launch_template_mixed[count.index], + "public_ip", + local.workers_group_defaults["public_ip"], + ) + delete_on_termination = lookup( + var.worker_groups_launch_template_mixed[count.index], + "eni_delete", + local.workers_group_defaults["eni_delete"], + ) + security_groups = flatten([ + local.worker_security_group_id, + var.worker_additional_security_group_ids, + lookup( + var.worker_groups_launch_template_mixed[count.index], + "additional_security_group_ids", + local.workers_group_defaults["additional_security_group_ids"] + ) + ]) } iam_instance_profile { - name = "${element(coalescelist(aws_iam_instance_profile.workers_launch_template_mixed.*.name, data.aws_iam_instance_profile.custom_worker_group_launch_template_mixed_iam_instance_profile.*.name), count.index)}" + name = coalescelist( + aws_iam_instance_profile.workers_launch_template_mixed.*.name, + data.aws_iam_instance_profile.custom_worker_group_launch_template_mixed_iam_instance_profile.*.name, + )[count.index] } - image_id = "${lookup(var.worker_groups_launch_template_mixed[count.index], "ami_id", local.workers_group_defaults["ami_id"])}" - instance_type = "${lookup(var.worker_groups_launch_template_mixed[count.index], "instance_type", local.workers_group_defaults["instance_type"])}" - key_name = "${lookup(var.worker_groups_launch_template_mixed[count.index], "key_name", local.workers_group_defaults["key_name"])}" - user_data = "${base64encode(element(data.template_file.workers_launch_template_mixed.*.rendered, count.index))}" - ebs_optimized = "${lookup(var.worker_groups_launch_template_mixed[count.index], "ebs_optimized", lookup(local.ebs_optimized, lookup(var.worker_groups_launch_template_mixed[count.index], "instance_type", local.workers_group_defaults["instance_type"]), false))}" + image_id = lookup( + var.worker_groups_launch_template_mixed[count.index], + "ami_id", + local.workers_group_defaults["ami_id"], + ) + instance_type = lookup( + var.worker_groups_launch_template_mixed[count.index], + "instance_type", + local.workers_group_defaults["instance_type"], + ) + key_name = lookup( + var.worker_groups_launch_template_mixed[count.index], + "key_name", + local.workers_group_defaults["key_name"], + ) + user_data = base64encode( + data.template_file.workers_launch_template_mixed.*.rendered[count.index], + ) + ebs_optimized = lookup( + var.worker_groups_launch_template_mixed[count.index], + "ebs_optimized", + lookup( + local.ebs_optimized, + lookup( + var.worker_groups_launch_template_mixed[count.index], + "instance_type", + local.workers_group_defaults["instance_type"], + ), + false, + ), + ) monitoring { - enabled = "${lookup(var.worker_groups_launch_template_mixed[count.index], "enable_monitoring", local.workers_group_defaults["enable_monitoring"])}" + enabled = lookup( + var.worker_groups_launch_template_mixed[count.index], + "enable_monitoring", + local.workers_group_defaults["enable_monitoring"], + ) } placement { - tenancy = "${lookup(var.worker_groups_launch_template_mixed[count.index], "launch_template_placement_tenancy", local.workers_group_defaults["launch_template_placement_tenancy"])}" - group_name = "${lookup(var.worker_groups_launch_template_mixed[count.index], "launch_template_placement_group", local.workers_group_defaults["launch_template_placement_group"])}" + tenancy = lookup( + var.worker_groups_launch_template_mixed[count.index], + "launch_template_placement_tenancy", + local.workers_group_defaults["launch_template_placement_tenancy"], + ) + group_name = lookup( + var.worker_groups_launch_template_mixed[count.index], + "launch_template_placement_group", + local.workers_group_defaults["launch_template_placement_group"], + ) } block_device_mappings { - device_name = "${lookup(var.worker_groups_launch_template_mixed[count.index], "root_block_device_name", local.workers_group_defaults["root_block_device_name"])}" + device_name = lookup( + var.worker_groups_launch_template_mixed[count.index], + "root_block_device_name", + local.workers_group_defaults["root_block_device_name"], + ) ebs { - volume_size = "${lookup(var.worker_groups_launch_template_mixed[count.index], "root_volume_size", local.workers_group_defaults["root_volume_size"])}" - volume_type = "${lookup(var.worker_groups_launch_template_mixed[count.index], "root_volume_type", local.workers_group_defaults["root_volume_type"])}" - iops = "${lookup(var.worker_groups_launch_template_mixed[count.index], "root_iops", local.workers_group_defaults["root_iops"])}" - encrypted = "${lookup(var.worker_groups_launch_template_mixed[count.index], "root_encrypted", local.workers_group_defaults["root_encrypted"])}" - kms_key_id = "${lookup(var.worker_groups_launch_template_mixed[count.index], "root_kms_key_id", local.workers_group_defaults["root_kms_key_id"])}" + volume_size = lookup( + var.worker_groups_launch_template_mixed[count.index], + "root_volume_size", + local.workers_group_defaults["root_volume_size"], + ) + volume_type = lookup( + var.worker_groups_launch_template_mixed[count.index], + "root_volume_type", + local.workers_group_defaults["root_volume_type"], + ) + iops = lookup( + var.worker_groups_launch_template_mixed[count.index], + "root_iops", + local.workers_group_defaults["root_iops"], + ) + encrypted = lookup( + var.worker_groups_launch_template_mixed[count.index], + "root_encrypted", + local.workers_group_defaults["root_encrypted"], + ) + kms_key_id = lookup( + var.worker_groups_launch_template_mixed[count.index], + "root_kms_key_id", + local.workers_group_defaults["root_kms_key_id"], + ) delete_on_termination = true } } + tags = var.tags + lifecycle { create_before_destroy = true } } resource "aws_iam_instance_profile" "workers_launch_template_mixed" { - count = "${var.manage_worker_iam_resources ? var.worker_group_launch_template_mixed_count : 0}" - name_prefix = "${aws_eks_cluster.this.name}" - role = "${lookup(var.worker_groups_launch_template_mixed[count.index], "iam_role_id", lookup(local.workers_group_defaults, "iam_role_id"))}" - path = "${var.iam_path}" + count = var.manage_worker_iam_resources ? local.worker_group_launch_template_mixed_count : 0 + name_prefix = aws_eks_cluster.this.name + role = lookup( + var.worker_groups_launch_template_mixed[count.index], + "iam_role_id", + local.workers_group_defaults["iam_role_id"], + ) + path = var.iam_path } +