Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #568 Terraform destroy fails when state is empty #1020

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aws_auth.tf
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ locals {

resource "kubernetes_config_map" "aws_auth" {
count = var.create_eks && var.manage_aws_auth ? 1 : 0
depends_on = [null_resource.wait_for_cluster[0]]
depends_on = [aws_eks_cluster.this[0], null_resource.wait_for_cluster[0]]

metadata {
name = "aws-auth"
Expand Down
6 changes: 3 additions & 3 deletions local.tf
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,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 = length(aws_eks_cluster.this) > 0 ? aws_eks_cluster.this[0].endpoint : ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the splat syntax instead.

coalescelist(aws_eks_cluster.this[*]. endpoint, [""])[0] This will return "" if the state is empty.

cluster_auth_base64 = length(aws_eks_cluster.this) > 0 ? aws_eks_cluster.this[0].certificate_authority[0].data : ""
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", var.cluster_name]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

coalescelist(aws_eks_cluster.this[*].name, [""])[0] This is true for all your change.

aws_authenticator_additional_args = var.kubeconfig_aws_authenticator_additional_args
aws_authenticator_env_variables = var.kubeconfig_aws_authenticator_env_variables
}) : ""
Expand Down
14 changes: 7 additions & 7 deletions workers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ resource "aws_autoscaling_group" "workers" {
"-",
compact(
[
aws_eks_cluster.this[0].name,
var.cluster_name,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the splat syntax instead : coalescelist(aws_eks_cluster.this[*].name, [""])[0] This is true for all your change.

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 : ""
]
Expand Down Expand Up @@ -107,16 +107,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" = "${var.cluster_name}-${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/${var.cluster_name}"
"value" = "owned"
"propagate_at_launch" = true
},
{
"key" = "k8s.io/cluster/${aws_eks_cluster.this[0].name}"
"key" = "k8s.io/cluster/${var.cluster_name}"
"value" = "owned"
"propagate_at_launch" = true
},
Expand All @@ -143,7 +143,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 = "${var.cluster_name}-${lookup(var.worker_groups[count.index], "name", count.index)}"
associate_public_ip_address = lookup(
var.worker_groups[count.index],
"public_ip",
Expand Down Expand Up @@ -385,7 +385,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 : var.cluster_name
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
Expand All @@ -396,7 +396,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 = var.cluster_name
role = lookup(
var.worker_groups[count.index],
"iam_role_id",
Expand Down