【问题标题】:How to pass a list to a nested stack parameter in AWS CloudFormation?如何将列表传递给 AWS CloudFormation 中的嵌套堆栈参数?
【发布时间】:2016-07-10 00:37:49
【问题描述】:

我使用嵌套堆栈来创建 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"
               }
        }

现在,当我使用 lambda 或 cloudformation 运行此 json 时,我在 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)

【问题讨论】:

    标签: amazon-web-services aws-lambda amazon-cloudformation


    【解决方案1】:

    您不能将列表传递给嵌套堆栈。您必须使用内部函数 Join 传递项目的串联,如下所示:!Join ["separator", [item1, item2, …]]

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 我猜这对作者有帮助,但不是直接回答问题。
      • 好吧,要学究起来,你是对的——这不是直接回答问题,因为实际上从来没有直接问过问题:)
      猜你喜欢
      • 2020-06-28
      • 1970-01-01
      • 2015-02-21
      • 2020-10-31
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      • 1970-01-01
      • 2020-03-10
      相关资源
      最近更新 更多