Systems Manager 参数类型 - AWS CloudFormation

Systems Manager 参数类型

Systems Manager 参数类型对应于 Systems Manager Parameter Store 中的现有参数。将 Systems Manager 参数键指定为 Systems Manager 参数类型的值,然后 CloudFormation 将从 Parameter Store 中获取最新的值来用于堆栈。例如,当您需要使用亚马逊机器映像(AMI)ID 等不断变化的值来频繁更新应用程序资源时,这可能非常实用。

您可以通过控制台中该堆栈的参数选项卡,或通过运行 describe-stacksdescribe-change-set 来查看 Systems Manager 参数的解析值。这些是目前在堆栈定义中用于相应的 Systems Manager 参数键的值。请注意,这些值是在创建或更新堆栈时设置的,因此它们可能与 Parameter Store 中的最新值不同。

由于 Systems Manager 参数类型的值是参数键,因此您应注意以下行为:

  • 对于堆栈更新,控制台中的使用现有值选项以及 update-stackUsePreviousValue 属性将告知 CloudFormation 使用现有的 Systems Manager 参数键,而不是其值。CloudFormation 在更新堆栈时始终从 Parameter Store 获取最新的值。

  • CloudFormation 可以对 Systems Manager 参数键执行验证,但不能对其对应的值执行验证。为了进行验证,您可以将参数键视为字符串。您应为 Parameter Store 中的 Systems Manager 参数值执行任何验证。

创建或更新堆栈并创建更改集时,CloudFormation 会在此操作运行时使用 Parameter Store 中存在的任何值。如果某个指定的参数在调用方的 AWS 账户下的 Parameter Store 中不存在,CloudFormation 会返回验证错误。

执行更改集时,CloudFormation 将使用在更改集中指定的值。您应在执行更改集前检查这些值,因为在您创建更改集和运行更改集之间的时间内,它们可能会在 Parameter Store 中发生更改。

有关 Parameter Store 的更多信息,请参阅 Systems Manager Parameter Store

注意

对于存储在同一 AWS 账户中的 Parameter Store 参数,必须提供参数名称。对于由其他 AWS 账户共享的 Parameter Store 参数,必须提供完整的参数 ARN。

支持的 Systems Manager 参数类型

CloudFormation 支持以下 Systems Manager 参数类型:

AWS::SSM::Parameter::Name

Systems Manager 参数键的名称。

请在您要传递参数键时使用此参数。例如,您可以使用此类型验证此参数是否存在。

AWS::SSM::Parameter::Value<String>

其值是字符串的 Systems Manager 参数。这与 Parameter Store 中的 String 参数对应。

AWS::SSM::Parameter::Value<List<String>>AWS::SSM::Parameter::Value<CommaDelimitedList>

其值是字符串列表的 Systems Manager 参数。这与 Parameter Store 中的 StringList 参数对应。

AWS::SSM::Parameter::Value<AWS-specific parameter type>

其值是 特定于 AWS 的参数类型的 Systems Manager 参数。例如,下面指定了 AWS::EC2::KeyPair::KeyName 类型:

AWS::SSM::Parameter::Value<AWS::EC2::KeyPair::KeyName>

AWS::SSM::Parameter::Value<List<AWS-specific parameter type>>

其值是 特定于 AWS 的参数类型列表的 Systems Manager 参数。例如,下面指定了 AWS::EC2::KeyPair::KeyName 类型的列表:

AWS::SSM::Parameter::Value<List<AWS::EC2::KeyPair::KeyName>>

不支持的 Systems Manager 参数类型

CloudFormation 不支持以下 Systems Manager 参数类型:

  • Systems Manager 参数类型的列表,例如:List<AWS::SSM::Parameter::Value<String>>

此外,CloudFormation 不支持将模板参数定义为 SecureString Systems Manager 参数类型。不过对于某些资源,您可以将安全字符串指定为参数。有关更多信息,请参阅 使用动态引用指定存储在其他服务中的值

示例

将引用 Systems Manager 公有参数作为默认值的 AMI ID

<AWS::EC2::Image::Id> 类型专用于 AMI ID 参数,并且支持 AWS 提供的公有参数。

以下示例声明了一个名为 LatestAmiId,类型为 AWS::SSM::Parameter::Value<AWS::EC2::Image::Id> 的参数。默认情况下,EC2 实例的 ImageId 属性会引用 /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2。此公有参数是最新 Amazon Linux 2 AMI 的区域 AMI ID 值的别名。有关公有参数的更多信息,请参阅《AWS Systems Manager 用户指南》中的查找公有参数

JSON

{ "Parameters": { "LatestAmiId": { "Description": "Region specific image from the Parameter Store", "Type": "AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>", "Default": "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2" } }, "Resources": { "Instance": { "Type": "AWS::EC2::Instance", "Properties": { "ImageId": { "Ref": "LatestAmiId" } } } } }

YAML

Parameters: LatestAmiId: Description: Region specific image from the Parameter Store Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>' Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2' Resources: Instance: Type: 'AWS::EC2::Instance' Properties: ImageId: !Ref LatestAmiId

将引用 Systems Manager 参数并且没有默认值的 AMI ID

以下示例声明了一个名为 ImageId,类型为 AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>,且没有默认值的参数。这意味着在创建堆栈时,您必须提供引用所需的 AMI ID 的 Systems Manager 参数键。

JSON

{ "Parameters": { "ImageId": { "Type": "AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>" } }, "Resources": { "Instance": { "Type": "AWS::EC2::Instance", "Properties": { "ImageId": { "Ref": "ImageId" } } } } }

YAML

Parameters: ImageId: Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>' Resources: Instance: Type: 'AWS::EC2::Instance' Properties: ImageId: !Ref ImageId

以下命令基于示例模板创建堆栈。它提供 Systems Manager 参数键 (myLatestAMI) 作为 ImageId 模板参数的值。这假定 myLatestAMI 参数存在于调用方的 AWS 账户下的 Parameter Store 中。

aws cloudformation create-stack --stack-name S2 --template-body example template \ --parameters ParameterKey=ImageId,ParameterValue=myLatestAMI

引用 Systems Manager 字符串参数

以下示例声明了一个名为 InstanceType,类型为 AWS::SSM::Parameter::Value<String> 的参数。由于没有默认值,因此在创建堆栈时,您必须提供引用所需实例类型的 Systems Manager 参数键。

JSON

{ "Parameters": { "InstanceType": { "Type": "AWS::SSM::Parameter::Value<String>" } }, "Resources": { "Instance": { "Type": "AWS::EC2::Instance", "Properties": { "InstanceType": { "Ref": "InstanceType" } } } } }

YAML

Parameters: InstanceType: Type: 'AWS::SSM::Parameter::Value<String>' Resources: Instance: Type: 'AWS::EC2::Instance' Properties: InstanceType: !Ref InstanceType

以下命令基于示例模板创建堆栈。它提供 Systems Manager 参数键 (myInstanceType) 作为 InstanceType 模板参数的值。这假定 myInstanceType 参数存在于调用方的 AWS 账户下的 Parameter Store 中。

aws cloudformation create-stack --stack-name S1 --template-body example template \ --parameters ParameterKey=InstanceType,ParameterValue=myInstanceType