Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters between one cloud formation to another cloud formation template

How to pass parameters between one cloud formation template file to another cloud formation template file in aws ? I am not talking about the nested templates , as I understand they are seperate resource blocks within one CFT only , please correct me if I am wrong.

like image 382
Ishu Gupta Avatar asked Aug 21 '15 21:08

Ishu Gupta


People also ask

How do I import and export parameters from one AWS CloudFormation stack to another?

To create a cross-stack reference, use the export field to flag the value of a resource output for export. Then, use the Fn::ImportValue intrinsic function to import the value in any stack within the same AWS Region and account. AWS CloudFormation identifies exported values by the names specified in the template.

What part of a CloudFormation template allows you to pass values into the template?

The Resources section is the only required section. Some sections in a template can be in any order. However, as you build your template, it can be helpful to use the logical order shown in the following list because values in one section might refer to values from a previous section.

How do you reference parameters in CloudFormation?

Referencing a parameter within a template You use the Ref intrinsic function to reference a parameter, and AWS CloudFormation uses the parameter's value to provision the stack. You can reference parameters from the Resources and Outputs sections of the same template.

How do I pass values between nested stacks?

FSPPass the output value from NestedStackA as the parameter value for NestedStackB. To access this value in the parent stack, use the Fn::GetAtt function. Use the logical name of NestedStackA and the name of the output value in Outputs. NestedStackOutputName format.


2 Answers

Check out the official documentation for cross-stack references and how it can be used.

The basic idea is that you'll have shared resources put into their own stack so that other stacks can reference those resources. This leads to less copy/paste in all of your cloud formation templates.

For example, you might have multiple related web applications that use the same security group rules for their servers. Rather than defining the same exact security group multiple times in each cloud formation template for each web application, you can put that security group into its own template/stack, declare the security group id as an output, and then in all of your web applications' cf templates, you can import that security group id and reference it for your instance's security group. This leads to less duplicate code, and a separation of concerns, as a network engineer can manage the security around the VPC/subnets/security groups and you only need to worry about your servers/applications.

like image 114
Chad Greenburg Avatar answered Oct 19 '22 18:10

Chad Greenburg


It only makes sense to pass parameters between stacks, not between templates - the templates are just JSON structures.

You can do it like this:

In the first template, create output values like this:

"Outputs" : {
    "ParentVPC" : {
        "Value" : {"Ref":"VPC"},
        "Description" : "VPC ID"
    },
    ...
}

Then, in the second template, create parameters like this:

"Parameters" : {
    "ParentVPC" : {
        "Type" : "AWS::EC2::VPC::Id",
    },
    ...
}

When creating a stack from the second template, call describe-stack on a stack created from the first template to get the output values, and pass them as parameters to create-stack.

like image 30
bsvingen Avatar answered Oct 19 '22 19:10

bsvingen