如何将 CommaDelimitedList 参数传递到 AWS CloudFormation 中的嵌套堆栈?

上次更新时间:2019 年 12 月 31 日

我想要将 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 中。例如,值 {"subnet-aaaa, subnet-bbbb"} 被转换回 ["subnet-aaaa, subnet-bbbb"]


这篇文章对您有帮助吗?

我们可以改进什么?


需要更多帮助?