0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

describe-imagesコマンドについて(AWSCLI)

Posted at

注意

aws マネジメントコンソールにログインし、CloudShellを開き操作を行うことを前提とします。
コマンドの実行権限を持ったユーザで操作を行うことを前提とします。

記事の概要

■describe-imagesコマンドでよく使いそうなパターンと簡単な注意点について記載します。

AWSCLIを開く
  • aws マネジメントコンソールにログインし、下記画像の赤矢印で示した部分をクリックしCloudShellを開きます。
AWSCLIを使用した環境確認
  • RedHatが提供しているイメージを確認コマンドで出力
    環境に何も作っていない場合を想定して、RedHatが提供しているイメージ一覧を参照した場合を例として記載します。
aws ec2 describe-images --owners 309956199498

【アカウントID参考】+++++++++++++++++++++++++++++++++++++++++++++++++++
https://access.redhat.com/solutions/15356
--owners 309956199498
Red Hatが所有するパブリックAMIが見られます。
https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/finding-an-ami.html
--owners amazon
Amazonが所有するパブリックAMIが見られます。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

コマンドを打つだけだとjson形式で表示されます。表示形式については下記種類があります。

【形式参考】+++++++++++++++++++++++++++++++++++++++++++++++++++
https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-usage-output-format.html
json - 出力は JSON 文字列としてフォーマットされます。 ←デフォルト
yaml - 出力は YAML 文字列としてフォーマットされます。
YAML ストリーム出力形式 - 出力はストリームされ、YAML 文字列としてフォーマットされます。ストリーミングにより、大きなデータタイプの処理を高速化できます。
text - 出力は、複数行のタブ区切りの文字列値としてフォーマットされます。これは、grep、sed、または awk などのテキストプロセッサに出力を渡すのに役立ちます。
table - 出力は、テーブルとしてフォーマットされ、文字の「+|-」を使用してセルの境界を形成します。通常、情報は他の形式よりも読みやすい「わかりやすい」形式で表示されますが、プログラムとしては役立ちません。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  • 形式の変更
    tableの説明に情報は他の形式よりも読みやすい「わかりやすい」形式で表示と書いてあるのでテーブル形式で出力してみます。
aws ec2 describe-images --output table --owners 309956199498
  • 情報絞り込みの対象選定
    必要な情報は、tableコマンドで出力した結果から探していきます。

出力結果の抜粋
| DescribeImages |
+---------------------------------------------------------------------------------------+
|| Images ||
|+----------------------+--------------------------------------------------------------+|
|| Architecture | x86_64 ||
|| BootMode | legacy-bios ||
|| CreationDate | 2024-11-06T01:32:17.000Z ||
|| DeprecationTime | 2026-11-06T01:32:17.000Z ||

  • 絞り込み(query)
    必要な情報を絞る場合--queryを使用します。
    CreationDateを見たい場合、下記のように指定します。
    今回は、DescribeImages下の階層にあるImagesの項目であるCreationDateを指定します。
    (text形式よりも、json形式で出力した方が階層構造はわかりやすいかもしれないです)
aws ec2 describe-images --output table --query 'Images[].[CreationDate]' --owners 309956199498

項目のごとに見出しを付ける場合は下記です。今回はCreationDateに「C_Date」という名前を付けます。

aws ec2 describe-images --output table --query 'Images[].{C_Date:CreationDate}' --owners 309956199498

見出しをつける場合は、項目の括り方が、[]から{}に指定形式が変わる[CreationDate]→{C_Date:CreationDate}

  • 複数の項目に増やして検証
    名前、イメージID、作成日、スナップショットIDを見てみます。
aws ec2 describe-images --owners 309956199498 --output table --query 'Images[].{NAME:Name,IMAGEID:ImageId,C_Date:CreationDate,SnapshotId:BlockDeviceMappings[].Ebs[].SnapshotId }'

出力結果抜粋
| DescribeImages |
+-----------------------------+---------------------------+---------------------------------------------------+
| C_Date | IMAGEID | NAME |
+-----------------------------+---------------------------+---------------------------------------------------+
| 2023-11-28T06:25:23.000Z | ami-0d62c5a22e4d74cc2 | RHEL-8.8.0_HVM-20231127-arm64-3-Hourly2-GP3 |
+-----------------------------+---------------------------+---------------------------------------------------+
|| SnapshotId ||
|+-----------------------------------------------------------------------------------------------------------+|
|| snap-028587b00c0eb6f8b ||

C_Date、IMAGEID、NAMEの三つとSnapshotIdが分かれて表示されます。

  • 分離した項目の結合
    SnapshotIdについても横並びに表示したいのでjoinを使用して、列を結合して表示させます。

【joinコマンド参考】+++++++++++++++++++++++++++++++++++++++++++++++++++
https://jmespath.org/examples.html?highlight=join
+++++++++++++++++++++++++++++++++++++++++++++++++++

aws ec2 describe-images --owners 309956199498 --output table --query 'Images[].{NAME:Name,IMAGEID:ImageId,DATE:CreationDate,SnapshotId:join(`,`,BlockDeviceMappings[].Ebs[].SnapshotId)}'

出力結果の抜粋
| DescribeImages |
+--------------------------+------------------------+--------------------------------------------------------+--------------------------+
| DATE | IMAGEID | NAME | SnapshotId |
+--------------------------+------------------------+--------------------------------------------------------+--------------------------+
| 2025-01-22T00:47:17.000Z| ami-032d085cd11e0a01b | RHEL_HA-8.10.0_HVM-20250121-x86_64-1652-Hourly2-GP3 | snap-0d875b5763fefd5c0 |
| 2024-04-24T02:45:10.000Z| ami-0aabed5569d350194 | RHEL-8.6.0_HVM-20240419-arm64-63-Hourly2-GP3 | snap-06ee04115c67475f2 |
| 2025-05-07T15:34:32.000Z| ami-0ff630e60519ee1fa | RHEL_HA-9.6.0_HVM_GA-20250423-x86_64-0-Hourly2-GP3 | snap-0b0c80def75e5e31f |
| 2025-08-06T16:21:54.000Z| ami-010537fc64cb15f34 | RHEL-9.4.0_HVM-20250805-x86_64-0-Hourly2-GP3 | snap-090e727114a8209c8 |

横並びに表示されるようになりました。

  • 並び替え
    必要な情報を並び替えるためにsort_by()またはreverse(sort_by())を使用します。
    例として日付順に並び替える方法を考えます。CreationDateが新しいもの、が一番上に来るように並び替えをします。

【sortコマンド参考】+++++++++++++++++++++++++++++++++++++++++++++++++++
https://docs.aws.amazon.com/ja_jp/cli/v1/userguide/cli-usage-filter.html
+++++++++++++++++++++++++++++++++++++++++++++++++++

aws ec2 describe-images --owners 309956199498 --output table --query 'reverse(sort_by(Images,&CreationDate))[].{NAME:Name,IMAGEID:ImageId,DATE:CreationDate,SnapshotId:join(`,`,BlockDeviceMappings[].Ebs[].SnapshotId)}'

出力結果の抜粋
| DescribeImages |
+--------------------------+------------------------+--------------------------------------------------------+--------------------------+
| DATE | IMAGEID | NAME | SnapshotId |
+--------------------------+------------------------+--------------------------------------------------------+--------------------------+
| 2025-09-18T14:43:06.000Z| ami-0cb69f840156dccd3 | RHEL-9.4.0_HVM-20250917-x86_64-0-Hourly2-GP3 | snap-0523623881aee87bc |
| 2025-09-18T14:39:04.000Z| ami-057a9d91209dfe868 | RHEL-9.4.0_HVM-20250917-arm64-0-Hourly2-GP3 | snap-03bc5e65cc4f4805c |
| 2025-09-18T14:30:05.000Z| ami-0a7f00ec0046df044 | RHEL-10.0.0_HVM-20250918-x86_64-0-Hourly2-GP3 | snap-0914ba2e9a0b19972 |
| 2025-09-18T14:23:18.000Z| ami-0958365e62a0c53a4 | RHEL-10.0.0_HVM-20250918-arm64-0-Hourly2-GP3 | snap-092687d4e6a28eff1 |

日付順に並び替えられました。

  • 絞り込み(filter)
    次に--filterを使用した絞り込みについてです。
    上記で出力した
    | 2025-09-18T14:39:04.000Z| ami-057a9d91209dfe868 | RHEL-9.4.0_HVM-20250917-arm64-0-Hourly2-GP3 | snap-03bc5e65cc4f4805c |
    のIMAGEIDを指定し絞り込みます。
aws ec2 describe-images --owners 309956199498 --output table --filter 'Name=image-id,Values=ami-057a9d91209dfe868'

filter条件のNameで指定する値がdescribeコマンドの出力結果と違うので、helpを参照して探す必要があります。今回の例だと、ami-xxxxxxはdescribeでは「ImageId」という文字列配下に所属していますが、filter条件は、「image-id」で指定する必要があります。

【参考】+++++++++++++++++++++++++++++++++++++++++++++++++++
下記コマンドでhelpを表示し、filters(list)の項目を参照
aws ec2 describe-images help
出力結果抜粋
--filters (list)
The filters.
~中略~
o ena-support - A Boolean that indicates whether enhanced networking
with ENA is enabled.
o free-tier-eligible - A Boolean that indicates whether this image
can be used under the Amazon Web Services Free Tier (true | false
).
o hypervisor - The hypervisor type (ovm | xen ).
o image-allowed - A Boolean that indicates whether the image meets
the criteria specified for Allowed AMIs.
o image-id - The ID of the image. ←今回の対象

  • 絞り込み(query&filter)
    queryで絞り込んだ内容から、filterもできます。
    下記はRHEL-9だけを絞ってみた場合のコマンドです。
aws ec2 describe-images --owners 309956199498 --output table --query 'reverse(sort_by(Images,&CreationDate))[].{NAME:Name,IMAGEID:ImageId,DATE:CreationDate,SnapshotId:join(`,`,BlockDeviceMappings[].Ebs[].SnapshotId)}' --filter 'Name=name,Values=RHEL-9*'

以上、describe-imagesコマンドでよく使いそうなパターンと簡単な注意点について記載しました。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?