如何在嵌套的 cloudformation 中将资源从父堆栈传递到子堆栈?

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

我的嵌套堆栈需要位于我的主堆栈中的资源。例如:嵌套堆栈中的 lambda 函数需要数据库配置

   "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"
            ]
        },

所以我将所需的参数从我的主堆栈传递到嵌套使用参数:

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

这就是我在嵌套堆栈中接收它们的方式:

{
    "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"
        }
    },

然而,当我 运行 cloudformation 脚本时,它无法创建嵌套堆栈。我是否错误地将资源从我的主堆栈传递到嵌套堆栈?

我是否应该在主堆栈的输出中导出参数并使用 "Fn:ImportValue" 将它们导入我的嵌套堆栈?

有很多因素阻碍了这些模板的工作。

让我们从嵌套堆栈模板开始。您不能在输入参数中使用 "Ref" 内部函数。只要类型就够了。此外,并非所有参数类型 (here's the list) 都受支持,例如,"Type": "AWS::ApiGateway::RestApi" 不是有效的参数类型。当不直接支持某些内容时,只需使用 "String" 类型。事实上,对于嵌套堆栈,你可以让你的生活更轻松,只需使用 "String" 类型。

接下来要修复的是 AWS::CloudFormation::Stack 资源块。在这里,您为每个传递的 "Parameters" 使用了 "Type" 属性,但实际上您不能在那里指定类型。嵌套模板的工作是指定它期望的输入类型。

我强烈建议您花时间阅读 CloudFormation documentation. Even better, read some examples made by AWS. Here's a good example of nested stacks,看看 master.yaml。