From c89da3772ebd2b1ffb79ef357891d7f34804bffb Mon Sep 17 00:00:00 2001 From: syst0m Date: Thu, 31 Oct 2019 16:27:03 +0000 Subject: [PATCH] Add destroy-time flag --- README.md | 5 +++-- aws_auth.tf | 4 ++-- cluster.tf | 15 ++++++++------- kubectl.tf | 3 +-- variables.tf | 6 ++++++ workers.tf | 34 +++++++++++++++++----------------- workers_launch_template.tf | 8 ++++---- 7 files changed, 41 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index edcc733c..64dbe186 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -# terraform-aws-eks +# terraform-aws-eks [![Lint Status](https://github.com/terraform-aws-modules/terraform-aws-eks/workflows/Lint/badge.svg)](https://github.com/terraform-aws-modules/terraform-aws-eks/actions) -[![LICENSE](https://img.shields.io/github/license/terraform-aws-modules/terraform-aws-eks)](https://github.com/terraform-aws-modules/terraform-aws-eks/blob/master/LICENSE) +[![LICENSE](https://img.shields.io/github/license/terraform-aws-modules/terraform-aws-eks)](https://github.com/terraform-aws-modules/terraform-aws-eks/blob/master/LICENSE) A terraform module to create a managed Kubernetes cluster on AWS EKS. Available through the [Terraform registry](https://registry.terraform.io/modules/terraform-aws-modules/eks/aws). @@ -123,6 +123,7 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a | cluster\_security\_group\_id | If provided, the EKS cluster will be attached to this security group. If not given, a security group will be created with necessary ingres/egress to work with the workers | string | `""` | no | | cluster\_version | Kubernetes version to use for the EKS cluster. | string | `"1.14"` | no | | config\_output\_path | Where to save the Kubectl config file (if `write_kubeconfig = true`). Assumed to be a directory if the value ends with a forward slash `/`. | string | `"./"` | no | +| enabled | Destroy-time flag. If set to "false", the count will be set to "0" on all resources. This allows destroy-time provisioners to run successfully. | bool | `"true"` | no | | iam\_path | If provided, all IAM roles will be created on this path. | string | `"/"` | no | | kubeconfig\_aws\_authenticator\_additional\_args | Any additional arguments to pass to the authenticator such as the role to assume. e.g. ["-r", "MyEksRole"]. | list(string) | `[]` | no | | kubeconfig\_aws\_authenticator\_command | Command to use to fetch AWS EKS credentials. | string | `"aws-iam-authenticator"` | no | diff --git a/aws_auth.tf b/aws_auth.tf index bd322119..c6b99a77 100644 --- a/aws_auth.tf +++ b/aws_auth.tf @@ -1,11 +1,11 @@ resource "local_file" "config_map_aws_auth" { - count = var.write_aws_auth_config ? 1 : 0 + count = var.write_aws_auth_config && local.enabled ? 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 + count = var.manage_aws_auth && local.enabled ? 1 : 0 depends_on = [aws_eks_cluster.this] provisioner "local-exec" { diff --git a/cluster.tf b/cluster.tf index 7b8a1c91..10d569a7 100644 --- a/cluster.tf +++ b/cluster.tf @@ -1,5 +1,5 @@ resource "aws_cloudwatch_log_group" "this" { - count = length(var.cluster_enabled_log_types) > 0 ? 1 : 0 + count = length(var.cluster_enabled_log_types) > 0 && local.enabled ? 1 : 0 name = "/aws/eks/${var.cluster_name}/cluster" retention_in_days = var.cluster_log_retention_in_days kms_key_id = var.cluster_log_kms_key_id @@ -7,6 +7,7 @@ resource "aws_cloudwatch_log_group" "this" { } resource "aws_eks_cluster" "this" { + count = local.enabled name = var.cluster_name enabled_cluster_log_types = var.cluster_enabled_log_types role_arn = local.cluster_iam_role_arn @@ -33,7 +34,7 @@ resource "aws_eks_cluster" "this" { } resource "aws_security_group" "cluster" { - count = var.cluster_create_security_group ? 1 : 0 + count = var.cluster_create_security_group && local.enabled ? 1 : 0 name_prefix = var.cluster_name description = "EKS cluster security group." vpc_id = var.vpc_id @@ -46,7 +47,7 @@ resource "aws_security_group" "cluster" { } resource "aws_security_group_rule" "cluster_egress_internet" { - count = var.cluster_create_security_group ? 1 : 0 + count = var.cluster_create_security_group && local.enabled ? 1 : 0 description = "Allow cluster egress access to the Internet." protocol = "-1" security_group_id = local.cluster_security_group_id @@ -57,7 +58,7 @@ 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 && local.enabled ? 1 : 0 description = "Allow pods to communicate with the EKS cluster API." protocol = "tcp" security_group_id = local.cluster_security_group_id @@ -68,7 +69,7 @@ resource "aws_security_group_rule" "cluster_https_worker_ingress" { } resource "aws_iam_role" "cluster" { - count = var.manage_cluster_iam_resources ? 1 : 0 + count = var.manage_cluster_iam_resources && local.enabled ? 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 @@ -78,13 +79,13 @@ resource "aws_iam_role" "cluster" { } resource "aws_iam_role_policy_attachment" "cluster_AmazonEKSClusterPolicy" { - count = var.manage_cluster_iam_resources ? 1 : 0 + count = var.manage_cluster_iam_resources && local.enabled ? 1 : 0 policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy" role = local.cluster_iam_role_name } resource "aws_iam_role_policy_attachment" "cluster_AmazonEKSServicePolicy" { - count = var.manage_cluster_iam_resources ? 1 : 0 + count = var.manage_cluster_iam_resources && local.enabled ? 1 : 0 policy_arn = "arn:aws:iam::aws:policy/AmazonEKSServicePolicy" role = local.cluster_iam_role_name } diff --git a/kubectl.tf b/kubectl.tf index 5a70828f..30fc0ed1 100644 --- a/kubectl.tf +++ b/kubectl.tf @@ -1,6 +1,5 @@ resource "local_file" "kubeconfig" { - count = var.write_kubeconfig ? 1 : 0 + count = var.write_kubeconfig && local.enabled ? 1 : 0 content = data.template_file.kubeconfig.rendered filename = "${substr(var.config_output_path, -1, 1) == "/" ? "${var.config_output_path}kubeconfig_${var.cluster_name}" : var.config_output_path}" } - diff --git a/variables.tf b/variables.tf index 91618563..eb6ea983 100644 --- a/variables.tf +++ b/variables.tf @@ -293,3 +293,9 @@ variable "attach_worker_cni_policy" { type = bool default = true } + +variable "enabled" { + description = "Destroy-time flag. If set to "false", the count will be set to "0" on all resources." + type = bool + default = true +} diff --git a/workers.tf b/workers.tf index e946e78e..240ee3e5 100644 --- a/workers.tf +++ b/workers.tf @@ -1,7 +1,7 @@ # Worker Groups using Launch Configurations resource "aws_autoscaling_group" "workers" { - count = local.worker_group_count + count = (local.worker_group_count * local.enabled) name_prefix = join( "-", compact( @@ -143,7 +143,7 @@ resource "aws_autoscaling_group" "workers" { } resource "aws_launch_configuration" "workers" { - count = local.worker_group_count + count = (local.worker_group_count * local.enabled) name_prefix = "${aws_eks_cluster.this.name}-${lookup(var.worker_groups[count.index], "name", count.index)}" associate_public_ip_address = lookup( var.worker_groups[count.index], @@ -232,7 +232,7 @@ resource "aws_launch_configuration" "workers" { } resource "random_pet" "workers" { - count = local.worker_group_count + count = (local.worker_group_count * local.enabled) separator = "-" length = 2 @@ -243,7 +243,7 @@ resource "random_pet" "workers" { } resource "aws_security_group" "workers" { - count = var.worker_create_security_group ? 1 : 0 + count = var.worker_create_security_group && local.enabled ? 1 : 0 name_prefix = aws_eks_cluster.this.name description = "Security group for all nodes in the cluster." vpc_id = var.vpc_id @@ -257,7 +257,7 @@ resource "aws_security_group" "workers" { } resource "aws_security_group_rule" "workers_egress_internet" { - count = var.worker_create_security_group ? 1 : 0 + count = var.worker_create_security_group && local.enabled ? 1 : 0 description = "Allow nodes all egress to the Internet." protocol = "-1" security_group_id = local.worker_security_group_id @@ -268,7 +268,7 @@ resource "aws_security_group_rule" "workers_egress_internet" { } resource "aws_security_group_rule" "workers_ingress_self" { - count = var.worker_create_security_group ? 1 : 0 + count = var.worker_create_security_group && local.enabled ? 1 : 0 description = "Allow node to communicate with each other." protocol = "-1" security_group_id = local.worker_security_group_id @@ -279,7 +279,7 @@ resource "aws_security_group_rule" "workers_ingress_self" { } resource "aws_security_group_rule" "workers_ingress_cluster" { - count = var.worker_create_security_group ? 1 : 0 + count = var.worker_create_security_group && local.enabled ? 1 : 0 description = "Allow workers pods to receive communication from the cluster control plane." protocol = "tcp" security_group_id = local.worker_security_group_id @@ -290,7 +290,7 @@ resource "aws_security_group_rule" "workers_ingress_cluster" { } resource "aws_security_group_rule" "workers_ingress_cluster_kubelet" { - count = var.worker_create_security_group ? var.worker_sg_ingress_from_port > 10250 ? 1 : 0 : 0 + count = var.worker_create_security_group && local.enabled ? var.worker_sg_ingress_from_port > 10250 ? 1 : 0 : 0 description = "Allow workers Kubelets to receive communication from the cluster control plane." protocol = "tcp" security_group_id = local.worker_security_group_id @@ -301,7 +301,7 @@ resource "aws_security_group_rule" "workers_ingress_cluster_kubelet" { } resource "aws_security_group_rule" "workers_ingress_cluster_https" { - count = var.worker_create_security_group ? 1 : 0 + count = var.worker_create_security_group && local.enabled ? 1 : 0 description = "Allow pods running extension API servers on port 443 to receive communication from cluster control plane." protocol = "tcp" security_group_id = local.worker_security_group_id @@ -312,7 +312,7 @@ resource "aws_security_group_rule" "workers_ingress_cluster_https" { } resource "aws_iam_role" "workers" { - count = var.manage_worker_iam_resources ? 1 : 0 + count = var.manage_worker_iam_resources && local.enabled ? 1 : 0 name_prefix = var.workers_role_name != "" ? null : aws_eks_cluster.this.name name = var.workers_role_name != "" ? var.workers_role_name : null assume_role_policy = data.aws_iam_policy_document.workers_assume_role_policy.json @@ -323,7 +323,7 @@ resource "aws_iam_role" "workers" { } resource "aws_iam_instance_profile" "workers" { - count = var.manage_worker_iam_resources ? local.worker_group_count : 0 + count = var.manage_worker_iam_resources && local.enabled ? local.worker_group_count : 0 name_prefix = aws_eks_cluster.this.name role = lookup( var.worker_groups[count.index], @@ -335,37 +335,37 @@ resource "aws_iam_instance_profile" "workers" { } resource "aws_iam_role_policy_attachment" "workers_AmazonEKSWorkerNodePolicy" { - count = var.manage_worker_iam_resources ? 1 : 0 + count = var.manage_worker_iam_resources && local.enabled ? 1 : 0 policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy" role = aws_iam_role.workers[0].name } resource "aws_iam_role_policy_attachment" "workers_AmazonEKS_CNI_Policy" { - count = var.manage_worker_iam_resources && var.attach_worker_cni_policy ? 1 : 0 + count = var.manage_worker_iam_resources && var.attach_worker_cni_policy && local.enabled ? 1 : 0 policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy" 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 && local.enabled ? 1 : 0 policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly" role = aws_iam_role.workers[0].name } resource "aws_iam_role_policy_attachment" "workers_additional_policies" { - count = var.manage_worker_iam_resources ? length(var.workers_additional_policies) : 0 + count = var.manage_worker_iam_resources && local.enabled ? length(var.workers_additional_policies) : 0 role = aws_iam_role.workers[0].name policy_arn = var.workers_additional_policies[count.index] } resource "aws_iam_role_policy_attachment" "workers_autoscaling" { - count = var.manage_worker_iam_resources && var.manage_worker_autoscaling_policy && var.attach_worker_autoscaling_policy ? 1 : 0 + count = var.manage_worker_iam_resources && var.manage_worker_autoscaling_policy && var.attach_worker_autoscaling_policy && local.enabled ? 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 && var.manage_worker_autoscaling_policy ? 1 : 0 + count = var.manage_worker_iam_resources && var.manage_worker_autoscaling_policy && local.enabled ? 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 diff --git a/workers_launch_template.tf b/workers_launch_template.tf index fefacd63..2bd83bfb 100644 --- a/workers_launch_template.tf +++ b/workers_launch_template.tf @@ -1,7 +1,7 @@ # Worker Groups using Launch Templates resource "aws_autoscaling_group" "workers_launch_template" { - count = local.worker_group_launch_template_count + count = (local.worker_group_launch_template_count * local.enabled) name_prefix = join( "-", compact( @@ -218,7 +218,7 @@ resource "aws_autoscaling_group" "workers_launch_template" { } resource "aws_launch_template" "workers_launch_template" { - count = local.worker_group_launch_template_count + count = (local.worker_group_launch_template_count * local.enabled) name_prefix = "${aws_eks_cluster.this.name}-${lookup( var.worker_groups_launch_template[count.index], "name", @@ -382,7 +382,7 @@ resource "aws_launch_template" "workers_launch_template" { } resource "random_pet" "workers_launch_template" { - count = local.worker_group_launch_template_count + count = (local.worker_group_launch_template_count * && local.enabled) separator = "-" length = 2 @@ -401,7 +401,7 @@ resource "random_pet" "workers_launch_template" { } resource "aws_iam_instance_profile" "workers_launch_template" { - count = var.manage_worker_iam_resources ? local.worker_group_launch_template_count : 0 + count = var.manage_worker_iam_resources ? (local.worker_group_launch_template_count * local.enabled) : 0 name_prefix = aws_eks_cluster.this.name role = lookup( var.worker_groups_launch_template[count.index],