From f619c76b1c3dbbb86a3b99c54d8472bf79c6abf0 Mon Sep 17 00:00:00 2001 From: "Thierno IB. BARRY" Date: Mon, 12 Oct 2020 21:59:29 +0200 Subject: [PATCH] fix: Use splat syntax to avoid errors during destroy with an empty state (#1041) --- aws_auth.tf | 3 +-- cluster.tf | 2 +- local.tf | 6 +++--- workers.tf | 15 +++++++-------- workers_launch_template.tf | 14 +++++++------- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/aws_auth.tf b/aws_auth.tf index 7dc479421d0..a1124324139 100644 --- a/aws_auth.tf +++ b/aws_auth.tf @@ -1,5 +1,4 @@ -data "aws_caller_identity" "current" { -} +data "aws_caller_identity" "current" {} locals { auth_launch_template_worker_roles = [ diff --git a/cluster.tf b/cluster.tf index 268de8013b9..fd1efb01fff 100644 --- a/cluster.tf +++ b/cluster.tf @@ -64,7 +64,7 @@ resource "null_resource" "wait_for_cluster" { count = var.create_eks && var.manage_aws_auth ? 1 : 0 depends_on = [ - aws_eks_cluster.this[0], + aws_eks_cluster.this, aws_security_group_rule.cluster_private_access, ] diff --git a/local.tf b/local.tf index 393dd7a8577..2554697dd04 100644 --- a/local.tf +++ b/local.tf @@ -146,10 +146,10 @@ locals { kubeconfig = var.create_eks ? templatefile("${path.module}/templates/kubeconfig.tpl", { kubeconfig_name = local.kubeconfig_name - endpoint = aws_eks_cluster.this[0].endpoint - cluster_auth_base64 = aws_eks_cluster.this[0].certificate_authority[0].data + endpoint = coalescelist(aws_eks_cluster.this[*].endpoint, [""])[0] + cluster_auth_base64 = coalescelist(aws_eks_cluster.this[*].certificate_authority[0].data, [""])[0] aws_authenticator_command = var.kubeconfig_aws_authenticator_command - aws_authenticator_command_args = length(var.kubeconfig_aws_authenticator_command_args) > 0 ? var.kubeconfig_aws_authenticator_command_args : ["token", "-i", aws_eks_cluster.this[0].name] + aws_authenticator_command_args = length(var.kubeconfig_aws_authenticator_command_args) > 0 ? var.kubeconfig_aws_authenticator_command_args : ["token", "-i", coalescelist(aws_eks_cluster.this[*].name, [""])[0]] aws_authenticator_additional_args = var.kubeconfig_aws_authenticator_additional_args aws_authenticator_env_variables = var.kubeconfig_aws_authenticator_env_variables }) : "" diff --git a/workers.tf b/workers.tf index a20b7d64865..5c05981415f 100644 --- a/workers.tf +++ b/workers.tf @@ -6,7 +6,7 @@ resource "aws_autoscaling_group" "workers" { "-", compact( [ - aws_eks_cluster.this[0].name, + coalescelist(aws_eks_cluster.this[*].name, [""])[0], lookup(var.worker_groups[count.index], "name", count.index), lookup(var.worker_groups[count.index], "asg_recreate_on_change", local.workers_group_defaults["asg_recreate_on_change"]) ? random_pet.workers[count.index].id : "" ] @@ -112,16 +112,16 @@ resource "aws_autoscaling_group" "workers" { [ { "key" = "Name" - "value" = "${aws_eks_cluster.this[0].name}-${lookup(var.worker_groups[count.index], "name", count.index)}-eks_asg" + "value" = "${coalescelist(aws_eks_cluster.this[*].name, [""])[0]}-${lookup(var.worker_groups[count.index], "name", count.index)}-eks_asg" "propagate_at_launch" = true }, { - "key" = "kubernetes.io/cluster/${aws_eks_cluster.this[0].name}" + "key" = "kubernetes.io/cluster/${coalescelist(aws_eks_cluster.this[*].name, [""])[0]}" "value" = "owned" "propagate_at_launch" = true }, { - "key" = "k8s.io/cluster/${aws_eks_cluster.this[0].name}" + "key" = "k8s.io/cluster/${coalescelist(aws_eks_cluster.this[*].name, [""])[0]}" "value" = "owned" "propagate_at_launch" = true }, @@ -148,7 +148,7 @@ resource "aws_autoscaling_group" "workers" { resource "aws_launch_configuration" "workers" { count = var.create_eks ? local.worker_group_count : 0 - name_prefix = "${aws_eks_cluster.this[0].name}-${lookup(var.worker_groups[count.index], "name", count.index)}" + name_prefix = "${coalescelist(aws_eks_cluster.this[*].name, [""])[0]}-${lookup(var.worker_groups[count.index], "name", count.index)}" associate_public_ip_address = lookup( var.worker_groups[count.index], "public_ip", @@ -262,7 +262,6 @@ resource "aws_launch_configuration" "workers" { ) delete_on_termination = lookup(ebs_block_device.value, "delete_on_termination", true) } - } lifecycle { @@ -394,7 +393,7 @@ resource "aws_security_group_rule" "cluster_primary_ingress_workers" { resource "aws_iam_role" "workers" { count = var.manage_worker_iam_resources && var.create_eks ? 1 : 0 - name_prefix = var.workers_role_name != "" ? null : aws_eks_cluster.this[0].name + name_prefix = var.workers_role_name != "" ? null : coalescelist(aws_eks_cluster.this[*].name, [""])[0] name = var.workers_role_name != "" ? var.workers_role_name : null assume_role_policy = data.aws_iam_policy_document.workers_assume_role_policy.json permissions_boundary = var.permissions_boundary @@ -405,7 +404,7 @@ resource "aws_iam_role" "workers" { resource "aws_iam_instance_profile" "workers" { count = var.manage_worker_iam_resources && var.create_eks ? local.worker_group_count : 0 - name_prefix = aws_eks_cluster.this[0].name + name_prefix = coalescelist(aws_eks_cluster.this[*].name, [""])[0] role = lookup( var.worker_groups[count.index], "iam_role_id", diff --git a/workers_launch_template.tf b/workers_launch_template.tf index 9d1ed28547e..89301246c02 100644 --- a/workers_launch_template.tf +++ b/workers_launch_template.tf @@ -6,7 +6,7 @@ resource "aws_autoscaling_group" "workers_launch_template" { "-", compact( [ - aws_eks_cluster.this[0].name, + coalescelist(aws_eks_cluster.this[*].name, [""])[0], lookup(var.worker_groups_launch_template[count.index], "name", count.index), lookup(var.worker_groups_launch_template[count.index], "asg_recreate_on_change", local.workers_group_defaults["asg_recreate_on_change"]) ? random_pet.workers_launch_template[count.index].id : "" ] @@ -189,7 +189,7 @@ resource "aws_autoscaling_group" "workers_launch_template" { [ { "key" = "Name" - "value" = "${aws_eks_cluster.this[0].name}-${lookup( + "value" = "${coalescelist(aws_eks_cluster.this[*].name, [""])[0]}-${lookup( var.worker_groups_launch_template[count.index], "name", count.index, @@ -197,7 +197,7 @@ resource "aws_autoscaling_group" "workers_launch_template" { "propagate_at_launch" = true }, { - "key" = "kubernetes.io/cluster/${aws_eks_cluster.this[0].name}" + "key" = "kubernetes.io/cluster/${coalescelist(aws_eks_cluster.this[*].name, [""])[0]}" "value" = "owned" "propagate_at_launch" = true }, @@ -224,7 +224,7 @@ resource "aws_autoscaling_group" "workers_launch_template" { resource "aws_launch_template" "workers_launch_template" { count = var.create_eks ? (local.worker_group_launch_template_count) : 0 - name_prefix = "${aws_eks_cluster.this[0].name}-${lookup( + name_prefix = "${coalescelist(aws_eks_cluster.this[*].name, [""])[0]}-${lookup( var.worker_groups_launch_template[count.index], "name", count.index, @@ -427,7 +427,7 @@ resource "aws_launch_template" "workers_launch_template" { tags = merge( { - "Name" = "${aws_eks_cluster.this[0].name}-${lookup( + "Name" = "${coalescelist(aws_eks_cluster.this[*].name, [""])[0]}-${lookup( var.worker_groups_launch_template[count.index], "name", count.index, @@ -442,7 +442,7 @@ resource "aws_launch_template" "workers_launch_template" { tags = merge( { - "Name" = "${aws_eks_cluster.this[0].name}-${lookup( + "Name" = "${coalescelist(aws_eks_cluster.this[*].name, [""])[0]}-${lookup( var.worker_groups_launch_template[count.index], "name", count.index, @@ -500,7 +500,7 @@ resource "random_pet" "workers_launch_template" { resource "aws_iam_instance_profile" "workers_launch_template" { count = var.manage_worker_iam_resources && var.create_eks ? local.worker_group_launch_template_count : 0 - name_prefix = aws_eks_cluster.this[0].name + name_prefix = coalescelist(aws_eks_cluster.this[*].name, [""])[0] role = lookup( var.worker_groups_launch_template[count.index], "iam_role_id",