Is there a way to send the variables from the root stack down to the children, then let each child stack create each resource by using cross stack references?
I'm trying to use a root stack to pass down some variables to the child stacks like stackName
, then each child stack will use the stackName
to create its own resource (e.g. API Gateway, DynamoDB, etc.).
==> The goal is to create a whole app from a root stack, with each resource (which is created by a child stack) has the same prefix like this-app-
, then for a resource like DynamoDB, the table name will be this-app-dynamodb-table
. So if it's code, it'd be something like this:
function main(stackName) {
createRoles(stackName);
createAPIGateway(stackName);
createDynamoDB(stackName);
}
function createRoles(appName) {
let roleARN = `${appName}ARN`;
// create some roles
}
// more functions
main('this-app'); // call the root-stack or the main function, pass in the name of the app as a parameter.
From AWS Docs here, it seems like the root stack still has all the Resources
and use "Fn::ImportValue
.
From this guide about nested stacks, it looks like if I just want to reference to another stack, I'll have to save the stack in an S3 bucket then use:
{
"Type" : "AWS::CloudFormation::Stack",
"Properties" : {
"NotificationARNs" : [ String, ... ],
"Parameters" : {Key : Value, ...},
"Tags" : [ Tag, ... ],
"TemplateURL" : String,
"TimeoutInMinutes" : Integer
}
}
I've looked at some answers online but they don't solve the problem, or maybe I'm looking at the problem from a wrong angle.
Update 15 May
I've added the templates to an S3 bucket.
There's one thing I don't understand:
If the child stack looks like a normal one, how does it take the variable? I thought it should look like this:
"Resources": {
"RootStack": {
"Type": "AWS::CloudFormation::Stack",
"Properties" : {
"TemplateURL": "https://s3.amazonaws.com/BUCKET/RootStack.json",
"DynamoDBTableName": {
"Fn::GetAtt" : [ "RootStack", "TableName" ]
}
}
},
"DDBTable": {
"Type": "AWS::DynamoDB::Table",
"Properties": {
"TableName": {
"Fn::GetAtt" : [ "RootStack", "DynamoDBTableName" ]
},
"AttributeDefinitions": [...
and the RootStack.json should look like this:
"Resources": {
"Database": {
"Type": "AWS::CloudFormation::Stack",
"Properties": {
"Parameters": {
"TableName": {
"Fn::Sub": "${AWS::StackName}-dynamodb-table"
}
},
"TemplateURL": "https://s3.amazonaws.com/BUCKET/DatabaseStack.json.json"
}
}
},...
Do I understand it correctly?