AWS CloudFormation + Nested Stacks
Tags: aws, cloudformation, iac
Published: 2022-09-16 01:32:54

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bf7lw8skmgipkgn99xhf.png)
**Example code:**
Export VPC ID:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/56lez21e4930h7bocbxy.png)

Import VPC ID:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qpa9iivbdooyt7a25gk1.png)

---

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tnb36wzlk6eklemcdu1t.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7okom1nt3n69d6pr6yp.png)
**Example code:**
The stack, named VPCStack, contains the output of the VPC ID.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/us8rbr0w25ltncuc6qsd.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9xa5o30msx6e8zohveho.png)
Stack called NACLStack, gets the VPC ID with the GetAtt function.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0mi6lvamkgg1c90zc60d.png)

---

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wbpz9crq72z9y2p9sxzj.png)
I hope that this article will be of great help to you. A big hug and see you another time. Thank you!