DEV Community

Cover image for AWS CloudFormation + Nested Stacks
Frank Pimienta
Frank Pimienta

Posted on • Updated on

AWS CloudFormation + Nested Stacks

I want to share with you a current topic, which is the process of managing and provisioning IT infrastructures with IaC (Infrastructure as Code). The main goal of IaC is to automate much of the architecture and management of the IT infrastructure. In addition, it allows you to quickly replicate your infrastructure, record resource changes in detail, and establish a flexible workflow that facilitates the collaboration of all those involved in the development process.

This concept did not arise with the cloud and DevOps, as some belief but instead has its roots in managing on-premises systems. Many tools, such as Terraform, Ansible, Chef Infra, and Puppet, fulfill the provisioning and automation capabilities. Still, since the blog will mainly focus on Amazon Web Services, I will talk about the native service of AWS for IaC: AWS CloudFormation.

I'm not going to talk about the structure of an AWS CloudFormation template, there's quite a lot of documentation about it, but today I'm going to talk about Nested Stacks.

For many people, the simplest way to use AWS CloudFormation is to create a single template in which you define all the resources you want to make. But as your infrastructure or project grows, your templates grow in resources, which means more lines of code. Having such a large template, it becomes difficult to manage.

In this case, I recommend dividing your templates into smaller ones to manage them more efficiently; that is where Nested Stack comes in.

But you should know that AWS CloudFormation provides two different methods to manage your templates: Cross-stack and Nested Stacks.


Cross-stack

The concept is straightforward: you manage your stacks separately, export output from one stack, and import it into another.

For example: In the VPC stack, you want to export the ID, and in the Load Balancer stack, you want to import that exported ID.

Image description
Example code:
Export VPC ID:

Image description

Import VPC ID:

Image description


Nested Stacks
Nested Stacks are composed of a root template, the main stack for the first-level stacks. This root template contains the references to the rest of the templates. For those familiar with Terraform, it works like a parent template that calls your public or local modules.

Image description

You can upload the individual templates to AWS CloudFormation, but Nested Stacks require templates to be held in an S3 bucket. This is a prerequisite for the Nested Stack to work.

You can also pass Outputs from one Nested Stack to another in a root template using the Fn::GetAtt intrinsic function. Output values can only be used between Nested Stacks, while Export (Cross-stack) values can be imported into other templates outside the nesting.

Image description
Example code:
The stack, named VPCStack, contains the output of the VPC ID.

Image description
Image description
Stack called NACLStack, gets the VPC ID with the GetAtt function.

Image description


Can Nested Stack and Cross-stack be mixed?
Yes, it is usual to mix them in large infrastructures, where there are several types of services, and each service is represented by a root template and its set of nested stacks.

Image description
I hope that this article will be of great help to you. A big hug and see you another time. Thank you!

Top comments (0)