AWS CloudFormation Nested Stacks

Lab Details

  1. This lab walks you through the steps to create a Nested Stack using cloudFormation.

  2. In this lab, you will create two separate stacks for Auto Scaling and Load Balancing and then attach the Autoscaling group to the Load balancer using a Nested Stack.

  3. Duration: 1 hour

  4. AWS Region: US East (N. Virginia) us-east-1

Introduction

Before going into Nested Stack, we need to be familiar with a few concepts such as Cloudformation, Stack and Template

Cloudformation

  1. CloudFormation is a service provided by AWS for designing our own infrastructure using code i.e infrastructure as code.

  2. Currently, CloudFormation supports two languages JSON and YAML. You can write your code with one of the languages.

  3. CloudFormation comes with great features being able to update your infrastructure whenever you want and also having the ability to delete the stack in case you don’t need it.

  4. A fascinating feature of CloudFormation is that it saves more time in building infrastructure and helps in focusing on the development.

  5. It is also possible to replicate our infrastructure in a short amount of time.

  6. It eliminates human error and works according to the code you have written. It consists of two main components, Stack and Templates.

Template

  1. Cloudformation

  2. It consists of various sections like 

  • AWS Template Format Version

  • Description

  • Metadata

  • Parameters

  • Mappings

  • Conditions

  • Resources

  • Outputs

  1. It is not mandatory that the template requires all the above-mentioned sections. By using only the Resources section, we will be able to create a template.    

  2. The resources section plays an important role in the template creation.

  3. For example, to create an EC2 instance, a template shall consist of various parameters such as key name, image id, instance type.

  4. It is also possible to create two resources in the same template and refer to one from another i.e. attaching an elastic IP with an EC2 instance.

Stack

  1. A stack consists of a collection of resources. 

  2. In other words, the stack consists of one or more templates.

  3. The advantage of the stack is that it is easy to create, delete or update the collection of resources.

  4. The advanced stacks have a nested stack which holds a collection of stacks.

Nested Stack

  1. As the name suggests, a Nested Stack consists of one or more stacks referencing each other.

  2. As infrastructure scales, a particular CloudFormation template may need to be used multiple times.

  3. In such cases, we isolate a common template and reference it as needed from a nested stack.

  4. Nested stack itself consists of one or stacks in a reference hierarchy.

  5. Nested Stack will have a parent stack that has one or more child stacks.

 Architecture Diagram

Case Study

In this lab, we are going to see the example of a Nested Stack by creating an Autoscaling group stack and a Load balancer stack and attaching the Load Balancer stack to the Autoscaling stack.

Tasks Details

  1. Launching Lab Environment

  2. Understand the CloudFormation Template

  3. Template for Autoscaling group

  4. Template for a Load Balancer

  5. Template for a Nested Stack

  6. Editing Nested_stack.yaml file

  7. Creating a web server with an Autoscaling group and a Load balancer using a CloudFormation Nested stack

  8. Check the resources created by the Nested Stack

  9. Checking for an Auto Scaling group

  10. Checking for Launch configuration

  11. Checking for an EC2 instance

  12. Checking for a Load Balancer

  13. Testing the load balancer

  14. Validation of the lab