嵌套的 cloudformation 堆栈中的资源依赖性问题

Resource dependency issues in nested cloudformation stack

当我尝试使用新的嵌套堆栈更新我的根堆栈时收到错误消息。

错误:"Template format error: Unresolved resource dependencies [ProjectsusgetFinancialsLF] in the Resources block of the template".

以下是我将值从主堆栈传递到嵌套堆栈的方式:

    "Resources": {
    "FinancialStack": {
        "Type": "AWS::CloudFormation::Stack",
        "Properties": {
            "TemplateURL": "https://s3.amazonaws.com/example/child-cft.json",
            "TimeoutInMinutes": "10",
            "Parameters": {
                "DBuser": {
                    "Ref": "DBuser"
                },
                "testDB": {
                    "Fn::GetAtt": [
                        "testDB",
                        "Endpoint.Address"
                    ]
                },
                "DBname": {
                    "Ref": "DBname"
                },
                "DBpass": {
                    "Ref": "DBpass"
                },
                "EnvType": {
                    "Ref": "EnvType"
                },
                "LambdaExecution": {
                    "Fn::GetAtt": [
                        "LambdaExecutionRole",
                        "Arn"
                    ]
                },
                "ApiGatewayRestApi": {
                    "Ref": "ApiGatewayRestApi"
                },
                "AuthorizerFuncApiGateway": {
                    "Ref": "AuthorizerFuncApiGatewayAuthorizer"
                },
                "ApiGatewayResourcePortfoliosPortfolioidVarProjectsProjectidVar" : {
                    "Ref": "ApiGatewayResourcePortfoliosPortfolioidVarProjectsProjectidVar"
                }
            }
        },
        "DependsOn": [
            "testDB",
            "LambdaExecutionRole",
            "AuthorizerFuncApiGatewayAuthorizer",
            "ApiGatewayResourcePortfoliosPortfolioidVarProjectsProjectidVar"
        ]
    }

这是我的子堆栈和引发错误的函数:

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "AWS CloudFormation to generate test one shot deployment",
"Parameters": {
    "DBuser": {
        "Type": "String"
    },
    "testDB": {
        "Type": "String"
    },
    "DBname": {
        "Type": "String"
    },
    "DBpass": {
        "Type": "String"
    },
    "EnvType": {
        "Type": "String"
    },
    "LambdaExecution": {
        "Type": "String"
    },
    "ApiGatewayRestApi": {
        "Type": "String"
    },
    "AuthorizerFuncApiGateway": {
        "Type": "String"
    },
    "ApiGatewayResourcePortfoliosPortfolioidVarProjectsProjectidVar": {
        "Type": "String"
    }
},
"Resources": {
    "ProjectsusgetProjectFinancialsLF": {
        "Type": "AWS::Lambda::Function",
        "Properties": {
            "Code": {
                "S3Bucket": "dev",
                "S3Key": "test-lamda.zip",
                "S3ObjectVersion": "9eNYbcI5EOuuut9igX2xpgbGCtKD1D4K"
            },
            "Environment": {
                "Variables": {
                    "MYSQLDB_USER": {
                        "Ref": "DBuser"
                    },
                    "MYSQLDB_HOST": {
                        "Ref": "testDB"
                    },
                    "MYSQLDB_DATABASE": {
                        "Ref": "DBname"
                    },
                    "MYSQLDB_PASSWORD": {
                        "Ref": "DBpass"
                    }
                }
            },
            "Description": "A get project financials function",
            "FunctionName": {
                "Fn::Join": [
                    "-",
                    [
                        {
                            "Ref": "EnvType"
                        },
                        "getProjectFinancials"
                    ]
                ]
            },
            "Handler": "src/controllers/projects.getProjectFinancials",
            "Role": {
                "Ref": "LambdaExecution"
            },
            "Runtime": "nodejs6.10"
        }
    },

我不确定为什么 [ProjectsusgetFinancialsLF] 有未解决的依赖项。我觉得我已经提供了功能所需的一切。我不知道是什么问题。有人可以向我解释发生了什么问题吗?

编辑:在根堆栈中包含 testDB 资源

"       "testDB": {
        "Type": "AWS::RDS::DBInstance",
        "Properties": {
            "DBName": {
                "Ref": "DBname"
            },
            "DBSecurityGroups": [
                {
                    "Ref": "DBSecurityGroup"
                }
            ],
            "AllocatedStorage": "5",
            "DBInstanceClass": "db.t2.micro",
            "DBInstanceIdentifier": "testinst",
            "Engine": "MySQL",
            "EngineVersion": "5.7",
            "MasterUsername": {
                "Ref": "DBuser"
            },
            "MasterUserPassword": {
                "Ref": "DBpass"
            },
            "DBParameterGroupName": {
                "Ref": "RDSDBParameterGroup"
            }
        }
    },"

这可能是因为在您的根堆栈中,当您引用 epmoliteDB 时,您并未声明它是另一个嵌套堆栈的输出。例如它应该看起来像。

                    "LambdaExecution": {
                    "Fn::GetAtt" : [ "epmoliteDB", "Outputs.Address" ] 
                }

我也是在假设您在 epmoliteDB 中输出正确信息的情况下这样做的。在这种情况下,一个名为 Address

的值

你可以在这里找到一些很好的例子 https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-cloudformation.html

此外,在参数部分的旁注中,当将 DBpass 声明为字符串时,您还想添加 NoEcho 属性 并将其设置为 true。这将使您的密码在输入和更新堆栈时无法以明文形式查看。有关详细信息,请参阅 https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html 并转到 NoEcho