30

I have been launching EC2 instances by logging in to the AWS site, hitting the "Launch" button and following the proscribed steps. Now I'd like to launch instance from an Ansible script, and to do this I (think I) need the AMI ID of the image I wish to launch.

The problem is that I am launching an image from the "Marketplace", and I cannot find the AMI ID. In particular I'm using the Centos 7 image. This is easy to find in the web interface, just go to the marketplace and search for "centos", the image I want is the first one found, but the information provided about the image doesn't seem to include the AMI ID that I need to launch it from a script. The workaround is to manually launch an image, and then when inspecting the running image, the AMI ID is given. But is there an easier way to find it?

Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
bandjalong
  • 483
  • 2
  • 6
  • 9
  • Is it in the marketplace, under "Manage Your Software Subscriptions"? – Michael - sqlbot Nov 28 '16 at 02:00
  • 2
    It doesn't seem to show there either - except in that it shows my running instances, which will show the ami id. But I want to find it prior to running one. – bandjalong Nov 28 '16 at 02:22
  • 3
    This was a very frustrating issue to deal with - I can't believe there's no way to view the AMI ID straight in the AWS website before launching an instance. – John Humphreys Jul 11 '19 at 20:57
  • I you came here after Googling for a Terraform specific question, see the answer https://stackoverflow.com/a/56401124/117471 – Bruno Bronosky Jul 12 '19 at 05:36

4 Answers4

55

CentOS publishes their AMI product codes to their wiki. The wiki provides the following information for the latest CentOS 7 AMI:

  • Owner: aws-marketplace
  • Product Code: aw0evgkw8e5c1q413zgy5pjce

Using this information, we can query describe-images with the AWS CLI:

Example:

aws ec2 describe-images \
    --owners 'aws-marketplace' \
    --filters 'Name=product-code,Values=aw0evgkw8e5c1q413zgy5pjce' \
    --query 'sort_by(Images, &CreationDate)[-1].[ImageId]' \
    --output 'text'

Output:

ami-6d1c2007

This query returns a single AMI ID, selected by sorting the collection by creation date and then selecting the last (most recent) element in the collection.

Per the CentOS wiki, multiple AMI ids may be associated with a product key, so while this query would currently only return a single AMI because only one matching this product currently exists... in the future if a new AMI is created for this product code for any reason this query will return it instead.

Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
  • 1
    Note I had initially gone to the wiki page to get the AMI IDs but they were outdated and my cloudFormation template errors mentioned "not authorized for images". I had to use the AWS CLI with the query here to get the latest 7.x image ID values. – Justin C Aug 02 '18 at 17:37
  • not ansible solution but you could search in ui as well while creating VM. Filter by "centos and free" check boxes on the left (I see 19 options with updated date). – prayagupa Mar 09 '19 at 20:36
  • Thanks, this was killing me. – John Humphreys Jul 11 '19 at 20:35
  • 6
    OMG, I found the marketplace CentOS image I wanted to spin up with Ansible (https://aws.amazon.com/marketplace/pp/Centosorg-CentOS-7-x8664-with-Updates-HVM/B00O7WM7QW) . All the information you could want EXCEPT the AMI. 2 hours searching for a suitable AMI is a joke AMAZON! I still have not found a suitable ami number. So frustrating. – englishPete Feb 13 '20 at 16:17
  • 3
    New product codes: 47k9ia2igxpcce2bzo8u3kj03 (centos 8) and cvugziknvmxgqna9noibqnnsy (centos 7) – Denis V Dec 08 '20 at 20:56
  • 1
    The new Product Code mentioned by @DenisV comes from https://wiki.centos.org/Cloud/AWS – Yanal-Yves Fargialla Jan 18 '22 at 18:09
  • How to solve `Unable to locate credentials. You can configure credentials by running "aws configure".` for `aws-marketplace` owner issue? – Roman Newaza Nov 14 '22 at 11:09
11

Check in this page

or

AWS has been changing the “aws marketplace” UI. Now (2022) We get the ami id in the “configure this software” Page in the AWS Marketplace itself.

Steps:

  1. Search and locate the image. Click on the image.
  2. On the top right, click “continue to subscribe”
  3. On the next window, click on “Continue to configuration”
  4. On the next window, Find the Ami ID located. We can change software versions to get different ami Ids.
Vigneshwar
  • 125
  • 1
  • 7
  • The link @Vigneshwar provided is an old profile. As per https://wiki.centos.org/Cloud/AWS, there's [a new profile](https://aws.amazon.com/marketplace/pp/prodview-foff247vr2zfw). Note "By centos.org" vs "By "Amazon Web Services" – Yanal-Yves Fargialla Jan 18 '22 at 18:12
9

If you are looking for all Centos7 images

$ aws ec2 describe-images \
      --owners aws-marketplace \
      --filters Name=product-code,Values=aw0evgkw8e5c1q413zgy5pjce \
      --query 'Images[*].[CreationDate,Name,ImageId]' \
      --filters "Name=name,Values=CentOS Linux 7*" \
      --region us-west-2 \
      --output table \
  | sort -r
|  2018-06-13T15:58:14.000Z|  CentOS Linux 7 x86_64 HVM EBS ENA 1805_01-b7ee8a69-ee97-4a49-9e68-afaee216db2e-ami-77ec9308.4  |  ami-3ecc8f46  |
|  2018-05-17T09:30:44.000Z|  CentOS Linux 7 x86_64 HVM EBS ENA 1804_2-b7ee8a69-ee97-4a49-9e68-afaee216db2e-ami-55a2322a.4   |  ami-5490ed2c  |
|  2018-04-04T00:11:39.000Z|  CentOS Linux 7 x86_64 HVM EBS ENA 1803_01-b7ee8a69-ee97-4a49-9e68-afaee216db2e-ami-8274d6ff.4  |  ami-0ebdd976  |
|  2017-12-05T14:49:18.000Z|  CentOS Linux 7 x86_64 HVM EBS 1708_11.01-b7ee8a69-ee97-4a49-9e68-afaee216db2e-ami-95096eef.4   |  ami-b63ae0ce  |

add to it similar query available for RedHat

aws ec2 describe-images \
    --owners 309956199498 \
    --query 'Images[*].[CreationDate,Name,ImageId]' \
    --filters "Name=name,Values=RHEL-7.?*GA*" \
    --region us-east-1 \
    --output table \
  | sort -r
|  2018-03-23T20:42:08.000Z |  RHEL-7.5_HVM_GA-20180322-x86_64-1-Hourly2-GP2  |  ami-6871a115 |
|  2017-08-08T15:37:31.000Z |  RHEL-7.4_HVM_GA-20170808-x86_64-2-Hourly2-GP2  |  ami-c998b6b2 |
|  2017-07-24T15:44:39.000Z |  RHEL-7.4_HVM_GA-20170724-x86_64-1-Hourly2-GP2  |  ami-cdc999b6 |
|  2016-10-26T22:32:29.000Z |  RHEL-7.3_HVM_GA-20161026-x86_64-1-Hourly2-GP2  |  ami-b63769a1 |
|  2015-11-12T21:06:58.000Z |  RHEL-7.2_HVM_GA-20151112-x86_64-1-Hourly2-GP2  |  ami-2051294a |
|  2015-02-25T20:24:23.000Z |  RHEL-7.1_HVM_GA-20150225-x86_64-1-Hourly2-GP2  |  ami-12663b7a |
|  2015-02-09T22:54:40.000Z |  RHEL-7.0_HVM_GA-20150209-x86_64-1-Hourly2-GP2  |  ami-60a1e808 |
|  2014-10-17T20:29:24.000Z |  RHEL-7.0_HVM_GA-20141017-x86_64-1-Hourly2-GP2  |  ami-a8d369c0 |
|  2014-05-28T19:17:11.000Z |  RHEL-7.0_GA_HVM-x86_64-3-Hourly2               
|  ami-785bae10 |
Bruno Bronosky
  • 66,273
  • 12
  • 162
  • 149
Prashant Lakhera
  • 850
  • 7
  • 13
5

I used the other answers here for a good way to test AMI lookups in Terraform.

Using...

aws ec2 describe-images \
    --owners aws-marketplace \
    --filters '[
        {"Name": "name",                "Values": ["CentOS Linux 7*"]},
        {"Name": "virtualization-type", "Values": ["hvm"]},
        {"Name": "architecture",        "Values": ["x86_64"]},
        {"Name": "image-type",          "Values": ["machine"]}
    ]' \
    --query 'sort_by(Images, &CreationDate)[-1]' \
    --region us-east-1 \
    --output json

...gave me the chance to trial and error my lookup of

data "aws_ami" "centos" {
  most_recent = true
  owners      = ["aws-marketplace"]

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

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

  filter {
    name   = "image-type"
    values = ["machine"]
  }

  filter {
    name   = "name"
    values = ["CentOS Linux 7*"]
  }
}

resource "aws_launch_configuration" "launch_configuration" {
  name_prefix = "${var.name}-"

  image_id = "${data.aws_ami.centos.image_id}"
  instance_type        = "t2.nano"
  iam_instance_profile = "${aws_iam_instance_profile.instance_profile.name}"
  security_groups      = ["${aws_security_group.lc_security_group.id}"]

  user_data = "${data.template_file.user_data.rendered}"

  lifecycle {
    create_before_destroy = true
  }
}
Bruno Bronosky
  • 66,273
  • 12
  • 162
  • 149
  • It doesn't work any more: `Error: Your query returned no results. Please change your search criteria and try again.` – Roman Newaza Nov 14 '22 at 11:15