Amazon LinuxのAMI-IDをAWS CLIで取得する方法

cdkでec2を起動するようにしていたところ、AMIのIDや一覧を取得する必要があったので、メモしておきます。

AWS CLIで取得したので、フィルタは下記の通りです。

  • ownerはamazon
  • amiの名前(name)をamzn-ami*
  • descfriptionをAmazon*2018*
  • virtualization-typeをhvm

AWS CLIを実行すると下記の通りです。

$ aws ec2 describe-images --filter "Name=owner-alias,Values=amazon" "Name=name,Values=amzn-ami*"  "Name=description,Values=Amazon*2018*" "Name=virtualization-type,Values=hvm"
{
    "Images": [
        {
            "Architecture": "x86_64",
            "CreationDate": "2018-08-13T19:09:56.000Z",
            "ImageId": "ami-0041c416aa23033a2",
            "ImageLocation": "amazon/amzn-ami-2018.03.e-amazon-ecs-optimized",
            "ImageType": "machine",
            "Public": true,
            "OwnerId": "591542846629",
            "State": "available",
            "BlockDeviceMappings": [
                {
                    "DeviceName": "/dev/xvda",
                    "Ebs": {
                        "DeleteOnTermination": true,
                        "SnapshotId": "snap-09326f109fc250e2a",
                        "VolumeSize": 8,
                        "VolumeType": "gp2",
                        "Encrypted": false
                    }
                },
                {
                    "DeviceName": "/dev/xvdcz",
                    "Ebs": {
                        "DeleteOnTermination": true,
                        "VolumeSize": 22,
                        "VolumeType": "gp2",
                        "Encrypted": false
                    }
                }
            ],
            "Description": "Amazon Linux AMI 2018.03.e x86_64 ECS HVM GP2",
            "EnaSupport": true,
            "Hypervisor": "xen",
            "ImageOwnerAlias": "amazon",
            "Name": "amzn-ami-2018.03.e-amazon-ecs-optimized",
            "RootDeviceName": "/dev/xvda",
            "RootDeviceType": "ebs",
            "SriovNetSupport": "simple",
            "VirtualizationType": "hvm"
        },
        {
            "Architecture": "x86_64",
            "CreationDate": "2018-11-17T01:08:05.000Z",
            "ImageId": "ami-007a27b62715a73d5",
            "ImageLocation": "amzn-ami-ap-northeast-1/957cba359c4af654e937d39e75cbf1778809a16c19297f6b535fb987d92eaf94/137112412989/amzn-ami-minimal-hvm-2018.03.0.20181116-x86_64.ext4.gpt.10g.manifest.xml",
            "ImageType": "machine",
            "Public": true,
            "OwnerId": "137112412989",
            "State": "available",
            "BlockDeviceMappings": [],
            "Description": "Amazon Linux AMI 2018.03.0.20181116 x86_64 Minimal HVM s3",
            "EnaSupport": true,
            "Hypervisor": "xen",
            "ImageOwnerAlias": "amazon",
            "Name": "amzn-ami-minimal-hvm-2018.03.0.20181116-x86_64-s3",
            "RootDeviceType": "instance-store",
            "SriovNetSupport": "simple",
            "VirtualizationType": "hvm"
        },
以下省略...