CloudFormation跨堆栈参考在根堆栈和子堆栈之间传递变量

时间:2019-05-16 13:50:58

标签: amazon-web-services amazon-cloudformation

我的目标是创建一个根堆栈,该根堆栈将stack-name值传递给子堆栈,然后是子堆栈使用表名称:stack-name-dynamodb-table创建DynamoDB表。我正在尝试使dynamodb.json模板可重用。因此,如果它像代码,我想做类似的事情:

const createDynamoDB = require('createDynamoDB');
fucntion main(stackName) {
  createDynamoDB(stackName);
  createOtherResources(stackName);
}

可重用功能:

function createDynamoDB(name) {
  const tableName = `${name}-table`;
  // create the table with tableName
}

我陷入了鸡肉和鸡蛋的境地:

我的root-stack1

"Parameters": {
        "DatabaseStack": {
            "Description": "The database stack that creates DynamoDB table.",
            "Type": "String",
            "MinLength" : 1,
            "MaxLength" : 255,
            "AllowedPattern" : "^[a-zA-Z][-a-zA-Z0-9]*$",
            "Default" : "dynamodb1"
        }
    },

    "Resources": {
        "Database": {
            "Type": "AWS::DynamoDB::Table",
            "Properties": {
                "Fn::ImportValue" :  {"Fn::Sub": "${DatabaseStack}-DynamoDBProperties" }
            }
        }
    },

    "Outputs": {
        "DatabaseName" : {
            "Value" : {
                "Fn::Sub": "${AWS::StackName}-table"
            },
            "Export": {
                "Name": {
                    "Fn::Sub": "${AWS::StackName}-table"
                }
            }
        }
    }

我的dynamodb1

    "Parameters": {
        "RootStack": {
            "Description": "The root stack that controls all templates.",
            "Type": "String",
            "MinLength" : 1,
            "MaxLength" : 255,
            "AllowedPattern" : "^[a-zA-Z][-a-zA-Z0-9]*$",
            "Default" : "root-stack1"
        }
    },

    "Resources": {
        "DDBTable": {
            "Type": "AWS::DynamoDB::Table",
            "Properties": {
                "TableName": {
                    "Fn::ImportValue" :  {"Fn::Sub": "${RootStack}-DatabaseName" }
                },
                "AttributeDefinitions": [
                    ...
    "Outputs": {
        "DynamoDBProperties": {
            "Value": {
                "Fn::GetAtt": [
                    "DDBTable",
                    "Properties"
                ]
            },
            "Export": {
                "Name": {
                    "Fn::GetAtt": ["DDBTable", "TableName"]
                }
            }
        }
    }
}

问题是当我使用root-stack1运行create-stack时,出现此错误:

An error occurred (ValidationError) when calling the CreateStack operation: Template format error: Expected an object, received {"Fn::ImportValue":{"Fn::Sub":"${DatabaseStack}-DynamoDBProperties"}}

如何将在dynamodb.json模板中创建的DynamoDB表的整个属性传递给root-stack.json模板?

我是否必须再次描述DynamoDB表属性?如果是,那似乎是多余的。目标是重用dynamodb.json模板,因此在将来,如果我需要创建具有相同架构的表,则可以在具有新堆栈名称的新根堆栈中引用它。

0 个答案:

没有答案