简体   繁体   English

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

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

Im using nested stack to create ELB and application stacks...And i need to pass list of subnets to ELB and Application stack... 我正在使用嵌套堆栈来创建ELB和应用程序堆栈...并且我需要将子网列表传递给ELB和应用程序堆栈...

And the main json has the below code... 而且主要的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"
               }
        }

now when i run this json using lambda or cloudformation i get the below error under cloudformation Events Tab.... 现在当我使用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)

Your JSON is not well-formed. 您的JSON格式不正确。 Running your JSON through aws cloudformation validate-template (or even jsonlint.com ) quickly reveals several basic syntax errors: 通过aws cloudformation validate-template (甚至jsonlint.com )运行JSON会迅速发现一些基本语法错误:

  1. Resources:{ requires the key to be surrounded by quotes: "Resources": { Resources:{要求密钥必须用引号引起来: "Resources": {
  2. Some of your quotation marks are invalid 'smart-quotes' "subnet-1”, that need to be replaced with standard ASCII quotes: "subnet-1", 您的某些引号是无效的“智能引号” "subnet-1”,需要用标准ASCII引号替换: "subnet-1",
  3. (This is the one your error message refers to) The "Properties" object in your "ELBStack" resource "S3Object: {"Ref: "S3Bucket"}," has a trailing comma after its last element that needs to be removed: "S3Object: {"Ref: "S3Bucket"}" (这是你的错误信息是指一个)的"Properties"在“ELBStack”资源对象"S3Object: {"Ref: "S3Bucket"},"有需要被删除其最后一个元素之后尾随逗号: "S3Object: {"Ref: "S3Bucket"}"

You can't pass a list to a nested stack. 您不能将列表传递给嵌套堆栈。 You have to pass a concatenation of items with the intrinsic function Join like this: !Join ["separator", [item1, item2, …]] . 您必须像这样通过内部函数Join传递项目的串联: !Join ["separator", [item1, item2, …]]

In the nested stack, the type of the parameter needs to be List<Type> . 在嵌套堆栈中,参数的类型需要为List<Type>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM