TL;DR
- AWS CLIで、特定のOSの最新のAMI IDを探したい
-
--query
で最新1件に絞り込み -
--filters
の条件はOSごとにそれなりに違いそうなので、まとめておく
describe-images
AWS CLIのdescribe-images
コマンドを使います。
$ aws ec2 describe-images \
--region [region] \
--query '[query]' \
--owners [owner] \
--filters '[condition]' \
--output [output]
[]
の中は、条件に合わせて変更。今回は、以下でいきます。
-
--region
…ap-northeast-1
-
--query
…reverse(sort_by(Images, &CreationDate))[0]
-
--owners
… 検索対象のAMIに合わせて変更 -
--filters
… 検索対象のAMIに合わせて変更 -
--output
…table
owners
は、amazon
、aws-marketplace
、microsoft
から選択します。指定しない場合は、自分が利用できるAMIすべてを対象に検索を行います。
--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.
例えば。
$ 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 |||
||+--------------------------------+-------------------------------------+||
ちなみに、AMI IDだけが欲しければ、--query
をreverse(sort_by(Images, &CreationDate))[0].[ImageId]
とすればOKです。
OSごとに
では、ここからはOSごとに検索方法を書いていきます。
--filters
で、AMIのどの情報を(Name
)、なにで検索するか(Values
)で指定していきます。
ところで、--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
name
で絞り込みます。owners
は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
Amazon Linux 2と同じく、name
で絞り込みます。owners
は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
CentOSのWikiにあるとおり、product-code
を使って絞り込みます。owners
は、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
name
で絞り込みます。owners
は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