如何将 CommaDelimitedList 参数传递到 AWS CloudFormation 中的嵌套堆栈?
我想要将 CommaDelimitedList 参数传递到 AWS CloudFormation 中的嵌套堆栈。
简短描述
您不能将类型 CommaDelimitedList 的值传递到嵌套堆栈中。相反,使用父堆栈中的 Fn::Join 内部函数将类型 CommaDelimitedList 转换为类型 String。
解决方法
以下示例向您显示如何将 SecurityGroupIds 列表从父堆栈传递到嵌套堆栈。
1. 打开父堆栈的 JSON 或 YAML 文件,然后将 SecurityGroupIds 的类型设置为 CommaDelimitedList。
在 JSON 文件的资源部分中,Fn::Join 函数会返回组合字符串。在 YAML 文件的资源部分中,!Join 函数会返回组合字符串。在 JSON 和 YAML 文件中,组合字符串都会将 SecurityGroupIds 参数类型从 CommaDelimitedList 转换为 String。
示例父 JSON 文件:
{ "AWSTemplateFormatVersion": "2010-09-09", "Parameters": { "SubnetId": { "Type": "AWS::EC2::Subnet::Id" }, "SecurityGroupIds": { "Type": "List<AWS::EC2::SecurityGroup::Id>" }, "KeyName": { "Type": "AWS::EC2::KeyPair::KeyName" }, "ImageId": { "Type": "String" } }, "Resources": { "Instance": { "Type": "AWS::CloudFormation::Stack", "Properties": { "TemplateURL": "https://s3.amazonaws.com/cloudformation-templates-us-east-2/nested.yml", "Parameters": { "SubnetId": { "Ref": "SubnetId" }, "SecurityGroupIds": { "Fn::Join": [ ",", { "Ref": "SecurityGroupIds" } ] }, "KeyName": { "Ref": "KeyName" }, "ImageId": { "Ref": "ImageId" } } } } } }
示例父 YAML 文件:
AWSTemplateFormatVersion: 2010-09-09 Parameters: SubnetId: Type: 'AWS::EC2::Subnet::Id' SecurityGroupIds: Type: 'List<AWS::EC2::SecurityGroup::Id>' KeyName: Type: 'AWS::EC2::KeyPair::KeyName' ImageId: Type: String Resources: Instance: Type: 'AWS::CloudFormation::Stack' Properties: TemplateURL: 'https://s3.amazonaws.com/cloudformation-templates-us-east-2/nested.yml' Parameters: SubnetId: !Ref SubnetId SecurityGroupIds: !Join - ',' - !Ref SecurityGroupIds KeyName: !Ref KeyName ImageId: !Ref ImageId
**注意:**如果您传递两个子网,如 ["subnet-aaaa, subnet-bbbb"],则 Fn::Join 的输出为 {"subnet-aaaa, subnet-bbbb"}。
2. 在嵌套堆栈的 JSON 或 YAML 文件中,将 SecurityGroupIds 的类型设置为 CommaDelimitedList。
示例嵌套 JSON 文件:
{ "AWSTemplateFormatVersion": "2010-09-09", "Parameters": { "SubnetId": { "Type": "String" }, "SecurityGroupIds": { "Type": "CommaDelimitedList" }, "KeyName": { "Type": "String" }, "ImageId": { "Type": "String" } }, "Resources": { "Ec2instance": { "Type": "AWS::EC2::Instance", "Properties": { "ImageId": { "Ref": "ImageId" }, "KeyName": { "Ref": "KeyName" }, "SecurityGroupIds": { "Ref": "SecurityGroupIds" }, "SubnetId": { "Ref": "SubnetId" } } } } }
示例嵌套 YAML 文件:
AWSTemplateFormatVersion: 2010-09-09 Parameters: SubnetId: Type: String SecurityGroupIds: Type: CommaDelimitedList KeyName: Type: String ImageId: Type: String Resources: Ec2instance: Type: 'AWS::EC2::Instance' Properties: ImageId: !Ref ImageId KeyName: !Ref KeyName SecurityGroupIds: !Ref SecurityGroupIds SubnetId: !Ref SubnetId
**注意:**在嵌套堆栈中,父堆栈的组合字符串作为 CommaDelimitedList 传递到 SecurityGroupIds 中。例如,值 {"sg-aaaaa, sg-bbbbb"} 被转换回 ["sg-aaaaa", "sg-bbbbb"]。因此,SecurityGroupIds 必须由 SecurityGroupIds: !Ref SecurityGroupIds 直接引用,而不是作为字符串列表。
相关信息
相关内容
- AWS 官方已更新 1 年前
- AWS 官方已更新 2 年前
- AWS 官方已更新 2 年前
- AWS 官方已更新 2 年前