Amazon Elastic Container Service
开发人员指南 (API 版本 2014-11-13)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

检索Amazon ECS-Optimized AMI 元数据

不同Amazon ECS-optimized AMI 的 AMI ID、映像名称、操作系统、容器代理版本和运行时版本可通过查询 SSM Parameter Store API 以编程方式检索。有关 SSM Parameter Store API 的更多信息,请参阅 GetParametersGetParametersByPath

注意

用户账户必须具有以下 IAM 权限才能检索Amazon ECS-optimized AMI 元数据。这些权限已添加到 AmazonECS_FullAccess IAM 策略。

  • ssm:GetParameters

  • ssm:GetParameter

  • ssm:GetParametersByPath

以下是参数名称的格式。

  • Amazon ECS-optimized Amazon Linux 2 AMI 元数据:

    /aws/service/ecs/optimized-ami/amazon-linux-2/<version>
  • Amazon ECS-optimized Amazon Linux 2 (arm64) AMI 元数据:

    /aws/service/ecs/optimized-ami/amazon-linux-2/arm64/<version>
  • Amazon ECS-optimized Amazon Linux AMI 元数据:

    /aws/service/ecs/optimized-ami/amazon-linux/<version>
  • Amazon ECS-optimized Windows AMI 元数据:

    /aws/service/ecs/optimized-ami/<os family>/<os version>/<os locale>/<os sku>/<version>

以下参数名称格式通过使用 recommended 检索最新稳定的Amazon ECS-optimized Amazon Linux 2 AMI 的元数据。

/aws/service/ecs/optimized-ami/amazon-linux-2/recommended

以下是针对参数值返回的 JSON 对象的示例。

{
	"schema_version": 1,
	"image_name": "amzn2-ami-ecs-hvm-2.0.20181017-x86_64-ebs",
	"image_id": "ami-04a4fb062c609f55b",
	"os": "Amazon Linux 2",
	"ecs_runtime_version": "Docker version 18.06.1-ce",
	"ecs_agent_version": "1.21.0"
}

上述输出中的每个字段都可作为子参数查询。通过将子参数名称追加到所选 AMI 的路径来构造子参数的参数路径。可用子参数如下:

  • schema_version

  • image_id

  • image_name

  • os

  • ecs_agent_version

  • ecs_runtime_version

以下参数名称格式通过使用子参数 image_id 检索最新稳定的Amazon ECS-optimized Amazon Linux 2 AMI 的映像 ID。

/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id

以下参数名称格式通过指定 AMI 名称来检索特定Amazon ECS-optimized AMI 版本的元数据。

  • Amazon ECS-optimized Amazon Linux 2 AMI 元数据:

    /aws/service/ecs/optimized-ami/amazon-linux-2/amzn2-ami-ecs-hvm-2.0.20181112-x86_64-ebs
  • Amazon ECS-optimized Amazon Linux 2 (arm64) AMI 元数据:

    /aws/service/ecs/optimized-ami/amazon-linux-2/arm64/amzn2-ami-ecs-hvm-2.0.20181120-arm64-ebs
  • Amazon ECS-optimized Amazon Linux AMI 元数据:

    /aws/service/ecs/optimized-ami/amazon-linux/amzn-ami-2017.09.l-amazon-ecs-optimized
  • Amazon ECS-optimized Windows AMI 元数据:

    /aws/service/ecs/optimized-ami/windows_server/2016/english/full/2018.03.26

注意

所有版本的Amazon ECS-optimized Amazon Linux 2 AMI 都可供检索。只能检索经 Amazon ECS 优化的 AMI 版本 amzn-ami-2017.09.l-amazon-ecs-optimized (Linux) 和 Windows_Server-2016-English-Full-ECS_Optimized-2018.03.26 (Windows) 及更高版本。有关更多信息,请参阅 Amazon ECS-Optimized Amazon Linux AMI 版本

例 检索最新稳定的Amazon ECS-optimized AMI 的元数据

您可将 AWS CLI 与以下 AWS CLI 命令结合使用来检索最新稳定的Amazon ECS-optimized AMI。

  • 对于Amazon ECS-optimized Amazon Linux 2 AMI:

    aws ssm get-parameters --names /aws/service/ecs/optimized-ami/amazon-linux-2/recommended --region us-east-1

    输出:

    {
        "Parameters": [
            {
                "Name": "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended",
                "Type": "String",
                "Value": "{\"schema_version\":1,\"image_name\":\"amzn2-ami-ecs-hvm-2.0.20181017-x86_64-ebs\",\"image_id\":\"ami-0a6be20ed8ce1f055\",\"os\":\"Amazon Linux 2\",\"ecs_runtime_version\":\"Docker version 18.06.1-ce\",\"ecs_agent_version\":\"1.21.0\"}",
                "Version": 1,
                "LastModifiedDate": 1539908415.817,
                "ARN": "arn:aws:ssm:us-east-1::parameter/aws/service/ecs/optimized-ami/amazon-linux-2/recommended"
            }
        ],
        "InvalidParameters": []
    }
  • 对于Amazon ECS-optimized Amazon Linux 2 (arm64) AMI:

    aws ssm get-parameters --names /aws/service/ecs/optimized-ami/amazon-linux-2/arm64/recommended --region us-east-1

    输出:

    {
        "Parameters": [
            {
                "Name": "/aws/service/ecs/optimized-ami/amazon-linux-2/arm64/recommended",
                "Type": "String",
                "Value": "{\"schema_version\":1,\"image_name\":\"amzn2-ami-ecs-hvm-2.0.20181120-arm64-ebs\",\"image_id\":\"ami-053b2a8c2f3e87928\",\"os\":\"Amazon Linux 2\",\"ecs_runtime_version\":\"Docker version 18.06.1-ce\",\"ecs_agent_version\":\"1.22.0\"}",
                "Version": 1,
                "LastModifiedDate": 1542745522.454,
                "ARN": "arn:aws:ssm:us-east-1::parameter/aws/service/ecs/optimized-ami/amazon-linux-2/arm64/recommended"
            }
        ],
        "InvalidParameters": []
    }
  • 对于Amazon ECS-optimized Amazon Linux AMI:

    aws ssm get-parameters --names /aws/service/ecs/optimized-ami/amazon-linux/recommended --region us-east-1

    输出:

    {
        "Parameters": [
            {
                "Name": "/aws/service/ecs/optimized-ami/amazon-linux/recommended",
                "Type": "String",
                "Value": "{\"schema_version\":1,\"image_name\":\"amzn-ami-2018.03.h-amazon-ecs-optimized\",\"image_id\":\"ami-07eb698ce660402d2\",\"os\":\"Amazon Linux\",\"ecs_runtime_version\":\"Docker version 18.06.1-ce\",\"ecs_agent_version\":\"1.21.0\"}",
                "Version": 11,
                "LastModifiedDate": 1539892113.403,
                "ARN": "arn:aws:ssm:us-east-1::parameter/aws/service/ecs/optimized-ami/amazon-linux/recommended"
            }
        ],
        "InvalidParameters": []
    }
  • 对于Amazon ECS-optimized Windows AMI:

    aws ssm get-parameters --names /aws/service/ecs/optimized-ami/windows_server/2016/english/full/recommended --region us-east-1

    输出:

    {
        "Parameters": [
            {
                "Name": "/aws/service/ecs/optimized-ami/windows_server/2016/english/full/recommended",
                "Type": "String",
                "Value": "{\"image_name\":\"Windows_Server-2016-English-Full-ECS_Optimized-2018.09.19\",\"os\":\"Windows_Server-2016-English-Full\",\"schema_version\":1,\"ecs_runtime_version\":\"Docker version 18.03.1-ee-3, build b9a5c95\",\"ecs_agent_version\":\"1.20.2\",\"image_id\":\"ami-0711d16ae98c1422d\"}",
                "Version": 8,
                "LastModifiedDate": 1537942304.061,
                "ARN": "arn:aws:ssm:us-east-1::parameter/aws/service/ecs/optimized-ami/windows_server/2016/english/full/recommended"
            }
        ],
        "InvalidParameters": []
    }
  • 对于Amazon ECS-optimized Windows AMI:使用 AWS PowerShell

    Get-SSMParameter -Name /aws/service/ecs/optimized-ami/windows_server/2016/english/full/recommended/image_id -region us-east-1

    输出:

    Name                                                                                 Type   Value        Version
    ----                                                                                 ----   -----        -------
    /aws/service/ecs/optimized-ami/windows_server/2016/english/full/recommended/image_id String ami-4734a738 3

例 检索特定Amazon ECS-optimized Amazon Linux AMI 版本的元数据

将 AWS CLI 与以下 AWS CLI 命令结合使用来检索特定Amazon ECS-optimized Amazon Linux AMI 版本的元数据。将 AMI 名称替换为要检索的Amazon ECS-optimized Amazon Linux AMI 的名称。有关可用版本的更多信息,请参阅 Amazon ECS-Optimized Amazon Linux AMI 版本

aws ssm get-parameters --names /aws/service/ecs/optimized-ami/amazon-linux/amzn-ami-2017.09.l-amazon-ecs-optimized --region us-east-1

例 使用 SSM GetParametersByPath API 检索Amazon ECS-optimized Amazon Linux AMI 元数据

将 AWS CLI 与以下命令结合使用来通过 SSM GetParametersByPath API 检索Amazon ECS-optimized Amazon Linux AMI 元数据。

aws ssm get-parameters-by-path --path /aws/service/ecs/optimized-ami/amazon-linux/ --region us-east-1

例 检索最新推荐的Amazon ECS-optimized Amazon Linux AMI 的映像 ID

您可以通过使用子参数 image_id 检索最新推荐的Amazon ECS-optimized Amazon Linux AMI ID 的映像 ID。

aws ssm get-parameters --names /aws/service/ecs/optimized-ami/amazon-linux/recommended/image_id --region us-east-1

输出:

{
    "Parameters": [
        {
            "Name": "/aws/service/ecs/optimized-ami/amazon-linux/recommended/image_id",
            "Type": "String",
            "Value": "ami-07eb698ce660402d2",
            "Version": 10,
            "LastModifiedDate": 1539892113.519,
            "ARN": "arn:aws:ssm:us-east-1::parameter/aws/service/ecs/optimized-ami/amazon-linux/recommended/image_id"
        }
    ],
    "InvalidParameters": []
}

要仅检索 image_id 值,您可以查询特定参数值;例如:

aws ssm get-parameters --names /aws/service/ecs/optimized-ami/amazon-linux/recommended/image_id --region us-east-1 --query "Parameters[0].Value"

输出:

"ami-f9ac2f86"

例 在 AWS CloudFormation 模板中使用最新推荐的Amazon ECS-optimized AMI

您可以通过引用 SSM Parameter Store 名称在 AWS CloudFormation 模板中检索最新推荐的Amazon ECS-optimized AMI;例如:

Amazon Linux 2:

Parameters: ECSAMI: Description: AMI ID Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id> Default: /aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id

Windows:

Parameters: ECSAMI: Description: AMI ID Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id> Default: /aws/service/ecs/optimized-ami/windows_server/2016/english/full/recommended/image_id