LoginSignup
21
6

More than 3 years have passed since last update.

AWS CLIで、最新のAMIの情報を取得する

Last updated at Posted at 2020-01-28

TL;DR

  • AWS CLIで、特定のOSの最新のAMI IDを探したい
  • --queryで最新1件に絞り込み
  • --filtersの条件はOSごとにそれなりに違いそうなので、まとめておく

describe-images

AWS CLIのdescribe-imagesコマンドを使います。

Finding a Linux AMI

describe-images

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

[]の中は、条件に合わせて変更。今回は、以下でいきます。

  • --regionap-northeast-1
  • --queryreverse(sort_by(Images, &CreationDate))[0]
  • --owners … 検索対象のAMIに合わせて変更
  • --filters … 検索対象のAMIに合わせて変更
  • --outputtable

ownersは、amazonaws-marketplacemicrosoftから選択します。指定しない場合は、自分が利用できる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だけが欲しければ、--queryreverse(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で絞り込みます。ownersamazonです。

$ 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で絞り込みます。ownersamazonです。

$ 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です。

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

nameで絞り込みます。owners099720109477です。

$ 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
21
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
6