🏪

パブリックパラメータを利用して各OSの最新のAMI IDを取得してみる

2023/08/05に公開

本エントリについて

AWS Systems Manager (SSM) パラメータストアのパブリックパラメータを利用して、Debian、Ubuntu、AmazonLinuxの最新のAMI IDを取得してみます。
ここではAWS CLIを利用します。

参考

https://dev.classmethod.jp/articles/aws-systems-manager-public-parameter-list/
https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/parameter-store-finding-public-parameters.html

パブリックパラメータ一覧

まず、どのようなサービスのパブリックパラメータがあるのか、リストを取得してみます。

$ aws ssm get-parameters-by-path --path /aws/service/list --region ap-northeast-1 --query Parameters[].Value
[
    "/aws/service/appmesh/",
    "/aws/service/aws-storage-gateway-latest/",
    "/aws/service/bottlerocket/",
    "/aws/service/canonical/",
    "/aws/service/cloud9/",
    "/aws/service/datasync/",
    "/aws/service/debian/",
    "/aws/service/ecs/",
    "/aws/service/eks/",
    "/aws/service/freebsd/",
    "/aws/service/ami-amazon-linux-latest/",
    "/aws/service/ami-windows-latest/",
    "/aws/service/aws-for-fluent-bit/",
    "/aws/service/global-infrastructure/",
    "/aws/service/marketplace/",
    "/aws/service/redhat/",
    "/aws/service/storagegateway/",
    "/aws/service/suse/"
]

Debian

Debian に関するパラメータは /aws/service/debian/ にあるようですので、配下の情報を取得してみます。
(数十行ありますので、出力は途中で割愛します)
パスを見ればどんなバージョンなのか分かる形になっています。

$ aws ssm describe-parameters --parameter-filters "Key=Name, Option=BeginsWith, Values=/aws/service/debian" --region ap-northeast-1 --query Parameters[].Name
[
    "/aws/service/debian/daily/11/20220711-1073/amd64",
    "/aws/service/debian/daily/11/latest/amd64",
    "/aws/service/debian/daily/bullseye/20220711-1073/amd64",
    "/aws/service/debian/daily/bullseye/latest/amd64",
:

AMI IDの取得

最新といいつつ、ここでは Debian buster(amd64) についての 最新のAMI IDを取得してみます。

$ aws ssm get-parameters --names /aws/service/debian/release/buster/latest/amd64 --region ap-northeast-1
{
    "InvalidParameters": [],
    "Parameters": [
        {
            "Name": "/aws/service/debian/release/buster/latest/amd64",
            "DataType": "text",
            "LastModifiedDate": 1671904733.766,
            "Value": "ami-041e370f47f9b619e",
            "Version": 7,
            "Type": "String",
            "ARN": "arn:aws:ssm:ap-northeast-1::parameter/aws/service/debian/release/buster/latest/amd64"
        }
    ]
}

AMI の情報を取得してみます。

$ aws ec2 describe-images --image-ids $(aws ssm get-parameters --names /aws/service/debian/release/buster/latest/amd64 --region ap-northeast-1 --query Parameters[].Value --output text)
{
    "Images": [
        {
            "VirtualizationType": "hvm",
            "Description": "Debian 10 (20221224-1239)",
            "PlatformDetails": "Linux/UNIX",
            "EnaSupport": true,
            "Hypervisor": "xen",
            "ImageOwnerAlias": "amazon",
            "State": "available",
            "SriovNetSupport": "simple",
            "ImageId": "ami-041e370f47f9b619e",
            "UsageOperation": "RunInstances",
            "BlockDeviceMappings": [
                {
                    "DeviceName": "/dev/xvda",
                    "Ebs": {
                        "SnapshotId": "snap-073024108ff9b72b4",
                        "DeleteOnTermination": true,
                        "VolumeType": "gp2",
                        "VolumeSize": 8,
                        "Encrypted": false
                    }
                }
            ],
            "Architecture": "x86_64",
            "ImageLocation": "amazon/debian-10-amd64-20221224-1239",
            "RootDeviceType": "ebs",
            "OwnerId": "136693071363",
            "RootDeviceName": "/dev/xvda",
            "CreationDate": "2022-12-24T17:56:29.000Z",
            "Public": true,
            "ImageType": "machine",
            "Name": "debian-10-amd64-20221224-1239"
        }
    ]
}

Ubuntu

$ aws ssm describe-parameters --parameter-filters "Key=Name, Option=BeginsWith, Values=/aws/service/canonical" --region ap-northeast-1 --query Parameters[].Name
[
    "/aws/service/canonical/meta/publisher-id",
    "/aws/service/canonical/ubuntu/eks/18.04/1.17/stable/20211004/amd64/hvm/ebs-gp2/ami-id",
    "/aws/service/canonical/ubuntu/eks/18.04/1.17/stable/20211102/amd64/hvm/ebs-gp2/ami-id",
    "/aws/service/canonical/ubuntu/eks/18.04/1.17/stable/20211105/amd64/hvm/ebs-gp2/ami-id",
    "/aws/service/canonical/ubuntu/eks/18.04/1.17/stable/20211109/amd64/hvm/ebs-gp2/ami-id",
    "/aws/service/canonical/ubuntu/eks/18.04/1.17/stable/current/amd64/hvm/ebs-gp2/ami-id",
    "/aws/service/canonical/ubuntu/eks/20.04/1.18/stable/20210720/amd64/hvm/ebs-gp2/ami-id",
    "/aws/service/canonical/ubuntu/eks/20.04/1.18/stable/20211005/amd64/hvm/ebs-gp2/ami-id",
    "/aws/service/canonical/ubuntu/eks/20.04/1.18/stable/20211029/amd64/hvm/ebs-gp2/ami-id",

AMI ID の取得

$ aws ssm get-parameters --names /aws/service/canonical/ubuntu/server-minimal/20.04/stable/current/amd64/hvm/ebs-gp2/ami-id --region ap-northeast-1
{
    "InvalidParameters": [],
    "Parameters": [
        {
            "Name": "/aws/service/canonical/ubuntu/server-minimal/20.04/stable/current/amd64/hvm/ebs-gp2/ami-id",
            "DataType": "aws:ec2:image",
            "LastModifiedDate": 1690393872.145,
            "Value": "ami-0379e43f3c0f61024",
            "Version": 280,
            "Type": "String",
            "ARN": "arn:aws:ssm:ap-northeast-1::parameter/aws/service/canonical/ubuntu/server-minimal/20.04/stable/current/amd64/hvm/ebs-gp2/ami-id"
        }
    ]
}

$ aws ec2 describe-images --image-ids $(aws ssm get-parameters --names /aws/service/canonical/ubuntu/server-minimal/20.04/stable/current/amd64/hvm/ebs-gp2/ami-id --region ap-northeast-1 --query Parameters[].Value --output text)
{
    "Images": [
        {
            "VirtualizationType": "hvm",
            "Description": "Canonical, Ubuntu Minimal, 20.04 LTS, amd64 focal image build on 2023-07-26",
            "PlatformDetails": "Linux/UNIX",
            "EnaSupport": true,
            "Hypervisor": "xen",
            "ImageOwnerAlias": "amazon",
            "State": "available",
            "SriovNetSupport": "simple",
            "ImageId": "ami-0379e43f3c0f61024",
            "UsageOperation": "RunInstances",
            "BlockDeviceMappings": [
                {
                    "DeviceName": "/dev/sda1",
                    "Ebs": {
                        "SnapshotId": "snap-06cd4b27dc2879d8c",
                        "DeleteOnTermination": true,
                        "VolumeType": "gp2",
                        "VolumeSize": 8,
                        "Encrypted": false
                    }
                },
                {
                    "DeviceName": "/dev/sdb",
                    "VirtualName": "ephemeral0"
                },
                {
                    "DeviceName": "/dev/sdc",
                    "VirtualName": "ephemeral1"
                }
            ],
            "Architecture": "x86_64",
            "ImageLocation": "amazon/ubuntu-minimal/images/hvm-ssd/ubuntu-focal-20.04-amd64-minimal-20230726",
            "RootDeviceType": "ebs",
            "OwnerId": "099720109477",
            "RootDeviceName": "/dev/sda1",
            "CreationDate": "2023-07-26T17:25:20.000Z",
            "Public": true,
            "ImageType": "machine",
            "Name": "ubuntu-minimal/images/hvm-ssd/ubuntu-focal-20.04-amd64-minimal-20230726"
        }
    ]
}

Amazon Linux

$ aws ssm describe-parameters --parameter-filters "Key=Name, Option=BeginsWith, Values=/aws/service/ami-amazon-linux-latest" --region ap-northeast-1 --query Parameters[].Name
[
    "/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-6.1-arm64",
    "/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-6.1-x86_64",
    "/aws/service/ami-amazon-linux-latest/al2023-ami-minimal-kernel-6.1-arm64",
    "/aws/service/ami-amazon-linux-latest/al2023-ami-minimal-kernel-6.1-x86_64",
    "/aws/service/ami-amazon-linux-latest/al2023-ami-minimal-kernel-default-arm64",
    "/aws/service/ami-amazon-linux-latest/amzn-ami-hvm-x86_64-gp2",
    "/aws/service/ami-amazon-linux-latest/amzn-ami-hvm-x86_64-s3",
    "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-ebs",
    "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2",
    "/aws/service/ami-amazon-linux-latest/amzn2-ami-kernel-5.10-hvm-x86_64-ebs",
    "/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64",
    "/aws/service/ami-amazon-linux-latest/al2023-ami-minimal-kernel-default-x86_64",
    "/aws/service/ami-amazon-linux-latest/amzn-ami-hvm-x86_64-ebs",
    "/aws/service/ami-amazon-linux-latest/amzn-ami-minimal-hvm-x86_64-s3",
    "/aws/service/ami-amazon-linux-latest/amzn-ami-minimal-pv-x86_64-s3",
    "/aws/service/ami-amazon-linux-latest/amzn-ami-pv-x86_64-s3",
    "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-arm64-gp2",
    "/aws/service/ami-amazon-linux-latest/amzn2-ami-kernel-5.10-hvm-arm64-gp2",
    "/aws/service/ami-amazon-linux-latest/amzn2-ami-kernel-5.10-hvm-x86_64-gp2",
    "/aws/service/ami-amazon-linux-latest/amzn2-ami-minimal-hvm-arm64-ebs",
    "/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-arm64",
    "/aws/service/ami-amazon-linux-latest/amzn-ami-minimal-hvm-x86_64-ebs",
    "/aws/service/ami-amazon-linux-latest/amzn-ami-minimal-pv-x86_64-ebs",
    "/aws/service/ami-amazon-linux-latest/amzn-ami-pv-x86_64-ebs",
    "/aws/service/ami-amazon-linux-latest/amzn2-ami-minimal-hvm-x86_64-ebs"
]

AMI IDの取得

Amazon Linux 2 についての 最新のAMI IDを取得してみます。

$ aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 --region ap-northeast-1
{
    "InvalidParameters": [],
    "Parameters": [
        {
            "Name": "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2",
            "DataType": "text",
            "LastModifiedDate": 1690826899.314,
            "Value": "ami-0f38fb80a464efed4",
            "Version": 92,
            "Type": "String",
            "ARN": "arn:aws:ssm:ap-northeast-1::parameter/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
        }
    ]
}

$ aws ec2 describe-images --image-ids $(aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 --region ap-northeast-1 --query Parameters[].Value --output text)
{
    "Images": [
        {
            "VirtualizationType": "hvm",
            "Description": "Amazon Linux 2 AMI 2.0.20230727.0 x86_64 HVM gp2",
            "PlatformDetails": "Linux/UNIX",
            "EnaSupport": true,
            "Hypervisor": "xen",
            "ImageOwnerAlias": "amazon",
            "State": "available",
            "SriovNetSupport": "simple",
            "ImageId": "ami-0f38fb80a464efed4",
            "UsageOperation": "RunInstances",
            "BlockDeviceMappings": [
                {
                    "DeviceName": "/dev/xvda",
                    "Ebs": {
                        "SnapshotId": "snap-07d138af2090eb78a",
                        "DeleteOnTermination": true,
                        "VolumeType": "gp2",
                        "VolumeSize": 8,
                        "Encrypted": false
                    }
                }
            ],
            "Architecture": "x86_64",
            "ImageLocation": "amazon/amzn2-ami-hvm-2.0.20230727.0-x86_64-gp2",
            "RootDeviceType": "ebs",
            "OwnerId": "137112412989",
            "RootDeviceName": "/dev/xvda",
            "CreationDate": "2023-07-27T02:47:23.000Z",
            "Public": true,
            "ImageType": "machine",
            "Name": "amzn2-ami-hvm-2.0.20230727.0-x86_64-gp2"
        }
    ]
}

AmzonLinux2023 についても 最新のAMI IDを取得してみます。

$ aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64 --region ap-northeast-1
{
    "InvalidParameters": [],
    "Parameters": [
        {
            "Name": "/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64",
            "DataType": "text",
            "LastModifiedDate": 1690476810.857,
            "Value": "ami-08c84d37db8aafe00",
            "Version": 15,
            "Type": "String",
            "ARN": "arn:aws:ssm:ap-northeast-1::parameter/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64"
        }
    ]
}

$ aws ec2 describe-images --image-ids $(aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64 --region ap-northeast-1 --query Parameters[].Value --output text)
{
    "Images": [
        {
            "VirtualizationType": "hvm",
            "Description": "Amazon Linux 2023 AMI 2023.1.20230725.0 x86_64 HVM kernel-6.1",
            "PlatformDetails": "Linux/UNIX",
            "EnaSupport": true,
            "Hypervisor": "xen",
            "ImageOwnerAlias": "amazon",
            "State": "available",
            "SriovNetSupport": "simple",
            "ImageId": "ami-08c84d37db8aafe00",
            "UsageOperation": "RunInstances",
            "BlockDeviceMappings": [
                {
                    "DeviceName": "/dev/xvda",
                    "Ebs": {
                        "VolumeSize": 8,
                        "Encrypted": false,
                        "VolumeType": "gp3",
                        "Iops": 3000,
                        "DeleteOnTermination": true,
                        "SnapshotId": "snap-0eda456f8e60d0d94"
                    }
                }
            ],
            "Architecture": "x86_64",
            "ImageLocation": "amazon/al2023-ami-2023.1.20230725.0-kernel-6.1-x86_64",
            "RootDeviceType": "ebs",
            "OwnerId": "137112412989",
            "RootDeviceName": "/dev/xvda",
            "CreationDate": "2023-07-25T22:46:51.000Z",
            "Public": true,
            "ImageType": "machine",
            "Name": "al2023-ami-2023.1.20230725.0-kernel-6.1-x86_64"
        }
    ]
}
GitHubで編集を提案

Discussion