GetMostRecentEcsAmi

nicolaw 17th June 2019 at 1:31am
AWS awscli ECS Terraform
$ aws ec2 describe-images \
    --owner amazon \
    --filters "Name=root-device-type,Values=ebs" \
              "Name=virtualization-type,Values=hvm" \
              "Name=architecture,Values=x86_64" \
    --query 'Images[?Name!=`null`]|[?contains(Name, `ecs-optimized`) == `true`]|reverse(sort_by(@,&CreationDate))[0].[Name,ImageId,CreationDate,Description]' \
    --output json
[
    "amzn-ami-2018.03.t-amazon-ecs-optimized",
    "ami-036cea62390485c0b",
    "2019-06-07T21:33:15.000Z",
    "Amazon Linux AMI 2018.03.t x86_64 ECS HVM GP2"
]
data "aws_ami" "ecs" {
  most_recent = true
  owners      = ["amazon"]

  filter {
    name   = "name"
    values = ["ecs-optimized"]
  }

  filter {
    name   = "root-device-type"
    values = ["ebs"]
  }

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

  filter {
    name   = "architecture"
    values = ["x86_64"]
  }
}