简体   繁体   中英

How to pass resources from parent to child stack in nested cloudformation?

My nested stack requires resources located in my main stack. eg: A lambda function in the nested stack requiring DB configs

   "ProjectsusgetProjectFinancialsLF": {
            "Type": "AWS::Lambda::Function",
            "Properties": {
                "Code": {
                    "S3Bucket": "dev",
                    "S3Key": "test-lamda.zip",
                    "S3ObjectVersion": "9eNYbcI5EOuuut9igX2xpgbGCtKD1D4K"
                },
                "Environment": {
                    "Variables": {
                        "MYSQLDB_USER": {
                            "Ref": "DBuser"
                        },
                        "MYSQLDB_HOST": {
                            "Fn::GetAtt": [
                                "testDB",
                                "Endpoint.Address"
                            ]
                        },
                        "MYSQLDB_DATABASE": {
                            "Ref": "DBname"
                        },
                        "MYSQLDB_PASSWORD": {
                            "Ref": "DBpass"
                        }
                    }
                },
                "Description": "A get project financials function",
                "FunctionName": {
                    "Fn::Join": [
                        "-",
                        [
                            {
                                "Ref": "EnvType"
                            },
                            "getProjectFinancials"
                        ]
                    ]
                },
                "Handler": "src/controllers/projects.geFinancials",
                "Role": {
                    "Fn::GetAtt": [
                        "LambdaExecutionRole",
                        "Arn"
                    ]
                },
                "Runtime": "nodejs6.10"
            },
            "DependsOn": [
                "LambdaExecutionRole"
            ]
        },

So I am passing the required params from my main stack to the nested using parameters :

"FinancialStack": {
    "Type": "AWS::CloudFormation::Stack",
    "Properties": {
        "TemplateURL": "https://s3.amazonaws.com/dev/child-cft.json",
        "TimeoutInMinutes": "5",
        "Parameters": {
            "DBuser": {
                "Ref": "DBuser",
                "Type": "String"
            },
            "epmoliteDB": {
                "Ref": "testDB",
                "Type": "AWS::RDS::DBInstance"
            },
            "DBname": {
                "Ref": "DBname",
                "Type": "String"
            },
            "DBPass": {
                "Ref": "DBpass",
                "Type": "String"
            },
            "EnvType": {
                "Ref": "EnvType",
                "Type": "String"
            },
            "LambdaExecutionRole": {
                "Ref": "LambdaExecutionRole",
                "Type": "AWS::IAM::Role"
            },
            "ApiGatewayRestApi": {
                "Ref": "ApiGatewayRestApi",
                "Type": "AWS::ApiGateway::RestApi"
            }
        }
    }
}

And this is how I am receiving them in my nested stack :

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "AWS CloudFormation to generate testone shot deployment",
    "Parameters": {
        "DBuser": {
            "Ref": "DBuser",
            "Type": "String"
        },
        "epmoliteDB": {
            "Ref": "testDB",
            "Type": "AWS::RDS::DBInstance"
        },
        "DBname": {
            "Ref": "DBname",
            "Type": "String"
        },
        "DBPass": {
            "Ref": "DBpass",
            "Type": "String"
        },
        "EnvType": {
            "Ref": "EnvType",
            "Type": "String"
        },
        "LambdaExecutionRole": {
            "Ref": "LambdaExecutionRole",
            "Type": "AWS::IAM::Role"
        },
        "ApiGatewayRestApi": {
            "Ref": "ApiGatewayRestApi",
            "Type": "AWS::ApiGateway::RestApi"
        }
    },

Yet when I run the cloudformation script it fails to create the nested stack. Am I passing the resources incorrectly from my main stack to the nested stack?

Should I instead export the parameters in the output of the main stack and import them in my nested stack using "Fn:ImportValue" ?

There's many things preventing these templates to work.

Let's start with the nested stack template. You can't to use the "Ref" intrinsic function inside the input parameters. Just the type is enough. Also not everything is supported as parameter type ( here's the list ), for exemple, "Type": "AWS::ApiGateway::RestApi" is not a valid parameter type. When something is not directly supported, just use the "String" type. In fact, for nested stacks you can make your life easier and just use the "String" type.

Next thing to fix is the AWS::CloudFormation::Stack resource block. Here you used the "Type" properties to for each passed "Parameters" but you actually can't specify the type there. It's the job of the nested template to dictate which type of input it's expecting.

I highly recommend you to take the time to read the CloudFormation documentation . Even better, read some examples made by AWS. Here's a good example of nested stacks , just have a look at master.yaml.

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