Search code examples
amazon-web-servicesamazon-ec2amazon-ami

How can I get the full ID of an ami before actually having to launch an ami?


I use Terraform which allows me to retrieve the ami ID from the ami "name" (actually I'm a little confused about what that ami "name" is versus the ami id).

Example:

data "aws_ami" "windowsimage" {
  owners = ["amazon"]
  most_recent = true
  filter {
    name = "name"
    values = ["Windows_Server-2016-English-Full-ECS_Optimized*"]
  }
}

This is awesome because it means I don't have to hard code the ami id. But in order to get that Windows_Server-2016-English-Full-ECS_Optimized... value I had to actually launch an instance and look at the AMI ID property.

There has to be a better way! I couldn't find that value anywhere for the ami in the marketplace page. Is there a aws cli command maybe I can run to find the full ami id string of amis?


Solution

  • Yes, there is an aws cli command to do this.

    https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-images.html

    You can also add an additional line

    most_recent = true
    

    should you only wish to pull the most recent ami for that filter match.