terraform-ecs icon indicating copy to clipboard operation
terraform-ecs copied to clipboard

Default image is not the recommended one

Open WhyNotHugo opened this issue 3 years ago • 1 comments

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"]
}

WhyNotHugo avatar Apr 08 '21 08:04 WhyNotHugo

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"]
  }
}

mrtaxi avatar Feb 23 '23 15:02 mrtaxi