rashmi
rashmi

Reputation: 85

Passing values from parent stack to nested stack for Cloudformation

I am new to nested stack and i am trying to pass input parameters from parent to child template. My parent stack looks like below:

AWSTemplateFormatVersion: "2010-09-09"
Transform: 'AWS::Serverless-2016-10-31'
Description: "ParentStack with all child stack"

Parameters:
  AccountName:
    Description: Please Enter valid Account Name.
    Type: "CommaDelimitedList"
    Default: "citi"

  Region:
    Description: Enter Region
    Type: "CommaDelimitedList"
    Default: "us-east-2"



  S3BucketName:
    Type: "CommaDelimitedList"
    Default: ""

  S3KeyName:
    Type: "CommaDelimitedList"
    Default: "Test-LambdaFunction.zip"

Resources:
  LambdaStack1:
    Type: "AWS::CloudFormation::Stack"
    Properties:
      Parameters:
        TemplateURL: https://test272t3.s3.us-east-2.amazonaws.com/CFTemplates/lambda.yaml
        CodeUri:
          Bucket: Fn:Join [ ' ', [!Ref S3BucketName] ]
          Key: Fn::Join [ ' ', [!Ref S3KeyName] ]

 S3Stack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: https://test272t3.s3.us-east-2.amazonaws.com/CFTemplates/s3child.yaml
      Parameters:
        BucketName: <<not sure how !sub can be paased in parent stack>>
        AccessControl: PublicReadWrite
        VersioningConfiguration:
          Status: Suspended


And part of child template is as follows:

Parameters:
  AccountName:
    Description: Please Enter valid Account Name.
    Type: String
    Default: citi
  Region:
    Description: Enter Region
    Type: String
    Default: us-east-2

  S3BucketName:
    Type: "String"
    Default: ""

  S3KeyName:
    Type: "String"
    Default: "MeghFlow-DBConnMgmt-Lambda-DBConnMgmtFunction.zip"

testLambda:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: 
        Bucket: !Ref S3BucketName
        Key: !Ref S3KeyName
      Handler: com.testff.testinghand.dbconnmgmt.lambda.testLambda::handleRequest
      Runtime: java8
      MemorySize: 1024
      Policies: AmazonDynamoDBFullAccess
      Environment:
        Variables:
          REGION: us-east-2
          DYNAMODB_NAME: DBConnectionInfo

ArtifactBucket:
  Type: AWS::S3::Bucket
  DeletionPolicy: Delete
  Properties:
    BucketName:  !Sub ${AccountName}-${Region}-artifacts
    AccessControl: PublicReadWrite
    VersioningConfiguration:
    Status: Suspended

Issue: I am not quite sure how the input parameters can be passed from parent to child. I referred few links like but i was confused even more as to when the input type must be CommaDelimitedList vs string. I even tried keeping the param type to string in both parent and child but still i get below error: "Value of property Parameters must be an object with String (or simple type) properties" and on using Fn::join get error as below: "Fn::Join object requires two parameters, (1) a string delimiter and (2) a list of strings to be joined or a function that returns a list of strings (such as Fn::GetAZs) to be joined."

Have referred link : Trying to pass parameters from Master to child template but no luck . Can anyone guide me in correct direction please. Thanks in advance.

Upvotes: 1

Views: 1768

Answers (2)

rashmi
rashmi

Reputation: 85

Thanks @gandaliter for your guidance. As per above marked answer CloudFormation parent stack accepts only strings and not any object level parameters(sub parameters under parameters just like CodeURI in my above code). I did few tweaks and changed all the parent template to below : Note: All the parameter type are set to String in child and parent template

AWSTemplateFormatVersion: "2010-09-09"
Transform: 'AWS::Serverless-2016-10-31'
Description: "ParentStack with all child stack"

Parameters:

  apiGatewayStageName:
    Type: String
    Default: "dev"

  HandlerName:
    Type: String
    Default: "com.test.tehgsaLambda::handleRequest"

  S3BucketName:
    Type: String
    Default: ""

  S3KeyName:
    Type: String
    Default: "Test-LambdaFunction.zip"

Resources:
  LambdaStack1:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL:
        Fn::Sub: "https://testyu2y73.s3.us-east-2.amazonaws.com/CFTemplates/lambda.yaml"
      Parameters:
        S3BucketName: !Ref S3BucketName
        S3KeyName: !Ref S3KeyName
        HandlerName: !Ref HandlerName
        apiGatewayStageName: !Ref apiGatewayStageName

        ```

Upvotes: 1

gandaliter
gandaliter

Reputation: 10101

I imagine the code you've given above isn't the only combination you've tried, and the parameters don't exactly line up between your parent and child stack, but in any case, the problem is that you're trying to give parameter values of:

CodeUri:
  Bucket: Fn:Join [ ' ', [!Ref S3BucketName] ]
  Key: Fn::Join [ ' ', [!Ref S3KeyName] ]

and

VersioningConfiguration:
  Status: Suspended

Both of these are objects, not 'String (or simple type) properties'. The error is saying that the whole Parameters object must have only simple values.

Incidentally, TemplateURL needs to go outside the Parameters object.

Upvotes: 0

Related Questions