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.
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.
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With