terraform-ecs
terraform-ecs copied to clipboard
Default image is not the recommended one
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"]
}
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"]
}
}