簡體   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