AWS CloudFormation StackSets and Nested Stacks

Home » AWS Cheat Sheets » AWS Management Tools » Management Related Notes » AWS CloudFormation StackSets and Nested Stacks

AWS CloudFormation StackSets and Nested Stacks

Last updated on June 3, 2023

AWS CloudFormation lets you model and provision resources for your environment using programming language, leveraging the concept of Infrastructure as Code (IaC). You don’t need to manually create all of your resources one by one, CloudFormation will do this for you. All resources are defined on the AWS CloudFormation template. This means that this AWS environment can be reliably and easily reproduced since everything is declared on a template. For example, creating a new Test environment similar to your current setup, or when creating a Disaster Recovery environment in another region. 

With AWS CloudFormation, you can either upload your own template, use a sample template, or create a template using Designer. These templates can be in JSON or YAML format. Resources that will be created from these templates are treated and managed as a single unit called stacks. 

StackSets

AWS CloudFormation is used mainly for automating deployments of different applications. If your application has cross-region and multi-accounts deployment requirements, you should consider using StackSets. This will allow you to do these kinds of deployment simultaneously with ease.

stacksets1

Diagram from AWS Docs

Creating StackSets

The following is a step-by-step guide on how to create a StackSet that you can use when deploying on CloudFormation.

1. Just like when creating a regular CloudFormation stack, you can either upload a template, use a sample template,
or create a template using Designer.

stacksets2

2. Provide a name and description for your StackSets. You can also configure your parameters here if you have any on your template.

Tutorials dojo strip

stacksets3

3. The next step is to add Tags and IAM Role if you need one.

stacksets4

4. Now we have two options on how we will do the deployment: through (1) accounts or (2) organizational units. You need to provide the Organization ID if you want to deploy using an organizational unit. For AWS accounts, you can provide a list of account numbers on the field or upload it as a .csv file.

stacksets5

5. Specify the region of the deployment. You can select multiple regions here.

stacksets6

6. There is also an optional configuration where you can set a number for the maximum concurrent accounts and failure tolerance of your deployment.

stacksets7

7. Once done, you just need to Review all the configurations you set. Lastly, you can now proceed with the deployment. 

Nested Stacks

As your infrastructure grows, there will be some cases where you need to declare the same resources to multiple CloudFormation templates. In these instances, it is a good practice to use nested stacks. You can create separate templates for these common resources and reference that on other templates. This way, you’ll avoid copying and pasting the same configuration on your templates, and this also simplifies stack updates.

Creating Nested Stacks

The following steps will guide you on how to create Nested Stacks.

For example, we have this simple CloudFormation Template in YAML format named ‘cf-template-s3.yaml’ that creates a bucket on S3. 

nestedstacks1

1. Let’s upload this template on a bucket and take note of its object URL.nestedstacks2

Using this template, we will create a nested stack into the main stack using the AWS::CloudFormation::Stack resource. We have here the template named ‘cf-template-stack.yaml’ for our main stack. On the Resources part, we set the logical name “MyStack” and put the Object URL of ‘cf-template-s3.yaml’ on TemplateURL. 

nestedstacks3

The important part here is the output. As you can see here, ‘cf-template-s3.yaml’ is referenced on the main stack ‘cf-template-stack.yaml’. This enables the GetAtt function to pull out the values of the output in the other template.

nestedstacks4

2. Now let’s create a stack and upload the template for our main stack.

nestedstacks5

3. Provide a stack name and click Next.

nestedstacks6

4. Here, we just set a tag and leave everything as default. Once done reviewing the stack, click Create Stack and it will now begin the deployment.

nestedstacks7

On the Stack Events, you will see the progress of the deployment. You’ll also see here if there are any rollbacks that happened. You’ll notice that there are two stacks created: our main stack and the nested stack.

nestedstacks8

Looking at the output of our main stack, you will see the created bucket on our nested stack.

nestedstacks10

Final Thoughts

Deploying a complex architecture can be a real pain and maintaining it is another thing. AWS CloudFormation has made these tasks much easier to accomplish. While StackSets enables us to do multi-account and cross-region deployments, nested stacks on the other hand makes the process of updating stacks easier. Imagine doing these things manually. You can also see the list of CloudFormation best practices for additional guidelines.

 

Sources:

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/what-is-cfnstacksets.html
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-nested-stacks.html

Tutorials Dojo portal

Be Inspired and Mentored with Cloud Career Journeys!

Tutorials Dojo portal

Enroll Now – Our Azure Certification Exam Reviewers

azure reviewers tutorials dojo

Enroll Now – Our Google Cloud Certification Exam Reviewers

Tutorials Dojo Exam Study Guide eBooks

tutorials dojo study guide eBook

FREE AWS Exam Readiness Digital Courses

Subscribe to our YouTube Channel

Tutorials Dojo YouTube Channel

FREE Intro to Cloud Computing for Beginners

FREE AWS, Azure, GCP Practice Test Samplers

Recent Posts

Written by: Lervin John Obando

Lervin is a Cloud Technology enthusiast and an AWS Community Builder whose expertise expands from Systems Administration, Database to Cloud Infrastructure. He is a Multi-Cloud Certified professional holding multiple certifications from AWS, Google, and Oracle. When he is not in front of his computer, you can find him spending time with his wife and dog.

AWS, Azure, and GCP Certifications are consistently among the top-paying IT certifications in the world, considering that most companies have now shifted to the cloud. Earn over $150,000 per year with an AWS, Azure, or GCP certification!

Follow us on LinkedIn, YouTube, Facebook, or join our Slack study group. More importantly, answer as many practice exams as you can to help increase your chances of passing your certification exams on your first try!

View Our AWS, Azure, and GCP Exam Reviewers Check out our FREE courses

Our Community

~98%
passing rate
Around 95-98% of our students pass the AWS Certification exams after training with our courses.
200k+
students
Over 200k enrollees choose Tutorials Dojo in preparing for their AWS Certification exams.
~4.8
ratings
Our courses are highly rated by our enrollees from all over the world.

What our students say about us?