Home > Software engineering >  Terraform wants to change EKS node groups even if configuration is the same
Terraform wants to change EKS node groups even if configuration is the same

Time:11-15

I'm having issues with Terraform when wanting to apply changes. Namely, even though the configuration (for launch template and node group) stays the same Terraform wants to change node groups settings because it thinks launch template version has been change.

My node group config looks like this:

resource "aws_eks_node_group" "test" {
  depends_on = [aws_launch_template.test]

  ...other settings

  launch_template {
    name    = aws_launch_template.test.name
    version = aws_launch_template.test.latest_version
  }
}

Launch template:

resource "aws_launch_template" "test" {
  name          = "test"
  instance_type = var.instance
  image_id      = data.aws_ssm_parameter.eks-ami.value
  user_data     = base64encode(local.eks-node-userdata)

  tag_specifications {
    resource_type = "instance"
    tags = {
      Name = "test
    }
  }

  lifecycle {
    create_before_destroy = true
  }
}

The output from terraform apply:

  # aws_launch_template.test will be updated in-place
  ~ resource "aws_launch_template" "test" {
        id                                   = "xxx"
      ~ image_id                             = (sensitive)
      ~ latest_version                       = 3 -> (known after apply)
        name                                 = "test"
        tags                                 = {}
        # (16 unchanged attributes hidden)

        # (1 unchanged block hidden)
    }

  # aws_eks_node_group.test will be updated in-place
  ~ resource "aws_eks_node_group" "general" {
        ...other settings

      ~ launch_template {
            id      = "xxx"
            name    = "test"
          ~ version = "3" -> (known after apply)
        }
    }

Everytime I run terraform commands (plan or apply) it wants to update node group thinking launch template has new version and node group should be updated. But nothing was changes in configuration files. Why is that happening? And how can I fix that?

CodePudding user response:

Try to comment creation of resource "aws_launch_template". Seems like it create a new launch template version even if user_data, image etc still the same.

CodePudding user response:

I think the problem is because the resource aws_launch_template always create a new default version. Maybe you can solve this problem setting the argument update_default_version as false.

update_default_version - (Optional) Whether to update Default Version each update. Conflicts with default_version.

Try to define your resource like this:

resource "aws_launch_template" "test" {
  name          = "test"
  instance_type = var.instance
  image_id      = data.aws_ssm_parameter.eks-ami.value
  user_data     = base64encode(local.eks-node-userdata)
  update_default_version = false

  tag_specifications {
    resource_type = "instance"
    tags = {
      Name = "test
    }
  }

  lifecycle {
    create_before_destroy = true
  }
}
  • Related