Get the latest AMI information with the AWS CLI

TL;DR

describe-images

Use the AWS CLI describe-images command.

Finding a Linux AMI

describe-images

$ aws ec2 describe-images \
  --region [region] \
  --query '[query]' \
  --owners [owner] \
  --filters '[condition]' \
  --output [output]

The inside of [] is changed according to the conditions. This time, we will go below.

Select ʻowners from ʻamazon, ʻaws-marketplace, and microsoft`. If not specified, the search will be performed on all AMIs that you can use.

--owners (list)

Filters the images by the owner. Specify an AWS account ID, self (owner is the sender of the request), or an AWS owner alias (valid values are amazon | aws-marketplace | microsoft ). Omitting this option returns all images for which you have launch permissions, regardless of ownership.

For example.

$ aws ec2 describe-images \
  --region ap-northeast-1 \
  --query 'reverse(sort_by(Images, &CreationDate))[:1]' \
  --owners amazon \
  --filters 'Name=name,Values=amzn2-ami-hvm-2.0.*-x86_64-gp2' \
  --output table
----------------------------------------------------------------------------
|                              DescribeImages                              |
+---------------------+----------------------------------------------------+
|  Architecture       |  x86_64                                            |
|  CreationDate       |  2019-12-18T01:33:12.000Z                          |
|  Description        |  Amazon Linux 2 AMI 2.0.20191217.0 x86_64 HVM gp2  |
|  EnaSupport         |  True                                              |
|  Hypervisor         |  xen                                               |
|  ImageId            |  ami-011facbea5ec0363b                             |
|  ImageLocation      |  amazon/amzn2-ami-hvm-2.0.20191217.0-x86_64-gp2    |
|  ImageOwnerAlias    |  amazon                                            |
|  ImageType          |  machine                                           |
|  Name               |  amzn2-ami-hvm-2.0.20191217.0-x86_64-gp2           |
|  OwnerId            |  137112412989                                      |
|  Public             |  True                                              |
|  RootDeviceName     |  /dev/xvda                                         |
|  RootDeviceType     |  ebs                                               |
|  SriovNetSupport    |  simple                                            |
|  State              |  available                                         |
|  VirtualizationType |  hvm                                               |
+---------------------+----------------------------------------------------+
||                           BlockDeviceMappings                          ||
|+------------------------------------+-----------------------------------+|
||  DeviceName                        |  /dev/xvda                        ||
|+------------------------------------+-----------------------------------+|
|||                                  Ebs                                 |||
||+--------------------------------+-------------------------------------+||
|||  DeleteOnTermination           |  True                               |||
|||  Encrypted                     |  False                              |||
|||  SnapshotId                    |  snap-0ffc2bf377f23be03             |||
|||  VolumeSize                    |  8                                  |||
|||  VolumeType                    |  gp2                                |||
||+--------------------------------+-------------------------------------+||

By the way, if you only want the AMI ID, you can set --query toreverse (sort_by (Images, & CreationDate)) [0]. [ImageId].

For each OS

Then, from here, I will write the search method for each OS.

With --filters, specify which information in the AMI (Name) and what to search for (Values).

By the way, it seems that * and ? Can be used as wildcards for --filters.

You can also use wildcards with the filter values. An asterisk (*) matches zero or more characters, and a question mark (?) matches zero or one character.

Listing and Filtering Using the CLI and API

Amazon Linux 2

Narrow down by name. ʻOwners is ʻamazon.

$ aws ec2 describe-images \
  --region ap-northeast-1 \
  --query 'reverse(sort_by(Images, &CreationDate))[:1]' \
  --owners amazon \
  --filters 'Name=name,Values=amzn2-ami-hvm-2.0.*-x86_64-gp2' \
  --output table

Amazon Linux

As with Amazon Linux 2, narrow down by name. ʻOwners is ʻamazon.

$ aws ec2 describe-images \
  --region ap-northeast-1 \
  --query 'reverse(sort_by(Images, &CreationDate))[:1]' \
  --owners amazon \
  --filters 'Name=name,Values=amzn-ami-hvm-*-x86_64-gp2' \
  --output table

CentOS 7

Use product-code to narrow down as you can see on the CentOS Wiki. ʻOwners is ʻaws-marketplace.

Images

Finding AMI ids

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

Ubuntu Server 18.04 LTS

Narrow down by name. ʻOwners is 099720109477`.

$ aws ec2 describe-images \
  --region ap-northeast-1 \
  --owners 099720109477 \
  --query 'reverse(sort_by(Images, &CreationDate))[:1]' \
  --filters 'Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*' \
  --output table

Recommended Posts

Get the latest AMI information with the AWS CLI
Get the latest Linux kernel version with Arch Linux
Get information with zabbix api
Get Alembic information with Python
System trade starting with Python3: Get the latest program code
How to get the information of organizations, Cost Explorer of another AWS account with Lambda (python)
Get AWS account ID with boto3
Get the weather with Python requests
Get the weather with Python requests 2
Get weather information with Python & scraping
PhytoMine-I tried to get the genetic information of plants with Python
Get property information by scraping with python
Run the IDCF cloud CLI with Docker
[Python] Get the variable name with str
Get BITCOIN LTP information with Raspberry PI
Since the alert was issued, get the server information with low awareness with fabric
Get started with the documentation tool Sphinx
I get a Python No module named'encodings' error with the aws command
Get holidays with the Google Calendar API
[Python] Get Python package information with PyPI API
I tried to get the movie information of TMDb API with Python
Get coincheck virtual currency information with API ♪
Get information on the 100 most influential tech Twitter users in the world with python.
Scraping the holojour and displaying it with CLI
[Python] Get the files in a folder with Python
The easiest way to get started with Django
Use the MediaWiki API to get Wiki information
Manage AWS nicely with the Python library Boto
Get CPU information of Raspberry Pi with Python
Get the region where AWS Lambda is running
Python script to get note information with REAPER
Get information equivalent to the Network tab of Chrome developer tools with Python + Selenium
List AWS ami
A little addictive information about Cliff, the CLI framework
Add information to the bottom of the figure with Matplotlib
Get the sum of each of multiple columns with awk
I tried Amazon Comprehend sentiment analysis with AWS CLI.
[Python] Get the numbers in the graph image with OCR
[AWS CLI] Enable the AWS CLI on Red Hat Enterprise Linux 8
Get the result in dict format with Python psycopg2
Pull out smart meter information with the cheapest hardware
Information for controlling the motor with Python on RaspberryPi
Get swagger.json with Flask-RESTX (Flask-RESTPlus) without starting the server
I tried to get an AMI using AWS Lambda
Try to get the contents of Word with Golang
termux × AWS Send smartphone location information to AWS with IoT
Get comments and subscribers with the YouTube Data API
Install the latest stable Python with pyenv (both 2 and 3)
Get the operation status of JR West with Python
Install the latest Python from pyenv installed with homebrew
[Python] Get user information and article information with Qiita API
Get the full path referenced by .lnk with wsl
Extract the band information of raster data with python