简体   繁体   中英

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...

And the main json has the below code...

"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....

 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. Running your JSON through aws cloudformation validate-template (or even jsonlint.com ) quickly reveals several basic syntax errors:

  1. Resources:{ requires the key to be surrounded by quotes: "Resources": {
  2. Some of your quotation marks are invalid 'smart-quotes' "subnet-1”, that need to be replaced with standard ASCII quotes: "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"}"

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, …]] .

In the nested stack, the type of the parameter needs to be List<Type> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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