arminc / terraform-ecs

AWS ECS terraform module
MIT License
804 stars 374 forks source link

Default image is not the recommended one #47

Open WhyNotHugo opened 3 years ago

WhyNotHugo commented 3 years ago

The image used by default does not match the image recommended by AWS for ECS.

This returns amzn2-ami-ecs-gpu-hvm-2.0.20210331-x86_64-ebs:

# Get latest Linux 2 ECS-optimized AMI by Amazon
data "aws_ami" "latest_ecs_ami" {
  most_recent = true

  filter {
    name   = "name"
    values = ["amzn2-ami-ecs-*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["amazon"]
}

The recommended image, mentioned here, can be obtained with:

aws ssm get-parameters --names /aws/service/ecs/optimized-ami/amazon-linux-2/recommended | jq -r '.Parameters[0].Value' | jq .image_id

The image id is amzn2-ami-ecs-hvm-2.0.20210331-x86_64-ebs, and this can be fetched with terraform using:

data "aws_ssm_parameter" "ecs_optimised_ami" {
  name = "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended"
}

resource "aws_instance" "myinstance" {
  ami = jsondecode(data.aws_ssm_parameter.ecs_optimised_ami.value)["image_id"]
}
mrtaxi commented 1 year ago

you can simply change the block:

data "aws_ami" "latest_ecs_ami" {
  most_recent = true

  filter {
    name   = "name"
    values = ["amzn2-ami-ecs-*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["amazon"]
}

to this block:

data "aws_ami" "latest_ecs_ami" {
  most_recent = true
  owners      = ["amazon"]

  filter {
    name   = "name"
    values = ["amzn2-ami-ecs-hvm-*-x86_64-ebs"]
  }
}