TL;DR
--query
to narrow down to the latest one--filters
are likely to differ depending on the OS, so I will summarize them.describe-images
Use the AWS CLI describe-images
command.
$ 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.
--region
… ap-northeast-1
--query
… reverse(sort_by(Images, &CreationDate))[0]
--owners
… Changed according to the AMI to be searched--filters
… Changed according to the AMI to be searched--output
… table
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]
.
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
.
$ 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