繁体   English   中英

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

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

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

   "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"内部函数。 只是类型就足够了。 同样,并非所有内容都支持作为参数类型( 这里是列表 ),例如, "Type": "AWS::ApiGateway::RestApi"不是有效的参数类型。 如果不直接支持某些内容,则只需使用"String"类型。 实际上,对于嵌套堆栈,您可以使用"String"类型使生活更轻松。

下一步要修复的是AWS::CloudFormation::Stack资源块。 在这里,您为每个传递的"Parameters"使用了"Type"属性,但实际上无法在此处指定类型。 嵌套模板的工作是确定期望的输入类型。

我强烈建议您花时间阅读CloudFormation文档 更好的是,阅读一些由AWS制作的示例。 这是嵌套堆栈的一个很好的例子 ,只需看看master.yaml。

暂无
暂无

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

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