terraform-aws-modules / terraform-aws-autoscaling

Terraform module to create AWS Auto Scaling resources 🇺🇦
https://registry.terraform.io/modules/terraform-aws-modules/autoscaling/aws
Apache License 2.0
287 stars 552 forks source link

Unable to define allowed_instance_types when defining a launch_template #265

Closed DisgruntledCrayon closed 1 month ago

DisgruntledCrayon commented 2 months ago

Description

Please provide a clear and concise description of the issue you are encountering, and a reproduction of your configuration (see the examples/* directory for references that you can copy+paste and tailor to match your configs if you are unable to copy your exact configuration). The reproduction MUST be executable by running terraform init && terraform apply without any further changes.

If your request is for a new feature, please use the Feature request template.

⚠️ Note

Before you submit an issue, please perform the following first:

  1. Remove the local .terraform directory (! ONLY if state is stored remotely, which hopefully you are following that best practice!): rm -rf .terraform/
  2. Re-initialize the project root to pull down modules: terraform init
  3. Re-attempt your terraform plan or apply and check if the issue still persists

Versions

Reproduction Code [Required]

module "asg" {
  source = "terraform-aws-modules/autoscaling/aws"
  version = "7.4.1"

  name                   = "example"
  autoscaling_group_tags = local.cluster_tags
  update_default_version = true

  create_launch_template = true
  image_id               = local.image_id
  launch_template_name   = local.cluster_name
  user_data              = base64encode(local.user_data)

  security_groups = local.security_groups
  vpc_zone_identifier = data.aws_subnets.private_subnets.ids

  default_cooldown      = 10
  max_size              = 1
  min_size              = 1

  health_check_type         = "EC2"
  health_check_grace_period = 600

  use_mixed_instances_policy = true
  instance_requirements = {
    allowed_instance_types = ["t3.micro", "t2.micro"]
    burstable_performance  = "included"

    memory_mib = {
      min = 256
      max = 8388608
    }

    vcpu_count = {
      min = 1
      max = 8096
    }
  }
}

Steps to reproduce the behavior:

terraform apply

Expected behavior

When running terraform apply my expectation is that the list provided for instance_requirements.allowed_instance_types gets populated within the EC2 launch template when it get created.

I expected it to be populated in the same way that it does when using mixed_instances_policy.override. However, when using this method, the allowed_instance_types get added to the autoscaling group instead of the launch template (this is expected).

Code like shown here:

  mixed_instances_policy = {
    override = [
      {
        instance_requirements = {
          allowed_instance_types = ["t3.micro", "t2.micro"]
        }
      }
    ]
  }

Actual behavior

The launch template is created but the list provided for instance_requirements.allowed_instance_types doesn't get populated into the created launch template.

Terminal Output Screenshot(s)

Additional context

I at the source and found that the problem is due to a missing line of code.

When looking at main.tf:769, I found that allowed_instance_types is utilized within the override block. However, I would expect to find the same logic on main.tf:182, but don't. I have not verified this theory, yet, but adding the following line at main.tf:182 should remedy the issue I'm experiencing:

allowed_instance_types = try(instance_requirements.value.allowed_instance_types, null)
DisgruntledCrayon commented 2 months ago

Testing revealed that my solution will work, but also that main.tf:194 will need to be changed to:

      excluded_instance_types = try(instance_requirements.value.excluded_instance_types, null)
baolsen commented 1 month ago

I came to the same conclusion, tested the proposed fix and it works. Will submit a PR shortly.

antonbabenko commented 1 month ago

This issue has been resolved in version 7.5.0 :tada:

github-actions[bot] commented 1 week ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.