如何将列表传递给 AWS CloudFormation 中的嵌套堆栈参数?

How to pass a list to a nested stack parameter in AWS CloudFormation?

我正在使用嵌套堆栈创建 ELB 和应用程序堆栈...我需要将子网列表传递给 ELB 和应用程序堆栈...

主要 json 有以下代码...

"Mappings":{
        "params":{

              "Subnets": {
                    "dev":[
                "subnet-1”,
                "subnet-2”
                ],
                   "test":[
                "subnet-3”,
                "subnet-4”,
                "subnet-5”,
                "subnet-6”
                ],

            "prod":[
                "subnet-7”,
                "subnet-8”,
                "subnet-9”
                ]
                }
        }
      },
 "Parameters":{
    "Environment":{
      "AllowedValues":[
        "prod",
        "preprod",
        "dev"
      ],
      "Default":"prod",
      "Description":"What environment type is it (prod, preprod, test, dev)?",
      "Type":"String"
    }
},
        Resources:{
         "ELBStack": {
               "Type": "AWS::CloudFormation::Stack",
               "Properties": {
                 "TemplateURL": {
                   "Fn::Join":[
                     "",
                     [
                       "https://s3.amazonaws.com/",
                       "myS3bucket",
                       "/ELB.json"
                     ]
                   ]
                 },
                 "Parameters": {
                   "Environment":{"Ref":"Environment"},

                   "ELBSHORTNAME":{"Ref":"ELBSHORTNAME"},
                   "Subnets":{"Fn::FindInMap":[
                                  "params",
                                  "Subnets",
                                  {
                                    "Ref":"Environment"
                                  }
                                ]},
                   "S3Bucket":{"Ref":"S3Bucket"},

                 },
                 "TimeoutInMinutes": "60"
               }
        }

现在,当我 运行 这个 json 使用 lambda 或 cloudformation 时,我在 cloudformation 事件选项卡下收到以下错误....

 CREATE_FAILED AWS::CloudFormation::Stack   ELBStack    Value of property Parameters must be an object with String (or simple type) properties

using below lambda


import boto3
import time

date = time.strftime("%Y%m%d")
time = time.strftime("%H%M%S")
stackname = 'FulfillSNSELB'
client = boto3.client('cloudformation')
response = client.create_stack(
    StackName= (stackname + '-' + date + '-' + time),
    TemplateURL='https://s3.amazonaws.com/****/**/myapp.json',
    Parameters=[
        {
            'ParameterKey': 'Environment',
            'ParameterValue': 'dev',
            'UsePreviousValue': False
        }]
)

def lambda_handler(event, context):
    return(response)

您的 JSON 格式不正确。 运行 您的 JSON 到 aws cloudformation validate-template(甚至 jsonlint.com)很快就会发现几个基本的语法错误:

  1. Resources:{ 要求密钥用引号括起来:"Resources": {
  2. 您的某些引号无效 'smart-quotes' "subnet-1”, 需要用标准 ASCII 引号替换:"subnet-1",
  3. (这是您的错误消息所指的那个)您的 "ELBStack" 资源 "S3Object: {"Ref: "S3Bucket"}," 中的 "Properties" 对象在其最后一个需要删除的元素之后有一个尾随逗号: "S3Object: {"Ref: "S3Bucket"}"

您不能将列表传递给嵌套堆栈。您必须像这样传递具有内在函数 Join 的项目串联:!Join ["separator", [item1, item2, …]].

在嵌套栈中,参数的类型需要为List<Type>