amazon-web-servicesterraformamazon-ecs

How to implement instance_requirements from launch template in terraform?


I have a module to deploy ecs with ec2 spot, within this module I have several resources aws_autoscaling_group, aws_launch_template.

For aws_launch_template I have the instance_type attribute, but I need there to be several options for the instance types for example t3.micro, t3a.micro etc, various family types.

resource "aws_launch_template" "launch_template" {
  for_each = var.ecs_clusters

  name          = "${var.prefix}-${each.value.ecs_name}-lt"
  image_id      = each.value.launch_template.image_id
  instance_requirements = ["t3a.micro", "t3.micro"]
  ....... 
  other attributes.. 



I am trying to add the attribute instance requirements just to validate the plan, but terraform doesn't recognize it.

Unexpected attribute: An attribute named "instance_requirements" is not expected hereTerraform


Solution

  • Terraform syntax of your snippet is wrong.

    The correct way to create a launch template using instance_requirements is this:

    resource "aws_launch_template" "launch_template" {
      for_each = var.ecs_clusters
    
      name          = "${var.prefix}-${each.value.ecs_name}-lt"
      image_id      = each.value.launch_template.image_id
      instance_requirements {
        allowed_instance_types = ["t3.xlarge", "t3a.xlarge", ]
        memory_mib {
          min = 15000
        }
        vcpu_count {
          min = 3
        }
      }
      ....... 
      other attributes.. 
    

    And remember, as docs says:

    If present then instance_type cannot be present.