Multi-level Cloud formation Templates — AWS

Jayasuriyan
4 min readMay 14, 2021

Amazon Web Services (AWS) is a cloud services platform, which offers many functionalities to help businesses scale and grow. AWS allows you to run web application servers in the cloud to host dynamic websites.

Generally, We will log in to the AWS console and create resources that serve our purpose. But we have other methods as well, AWS CLI and CloudFormation Template.

The AWS Command Line Interface (AWS CLI) is an Amazon Web Services tool that enables developers to control Amazon public cloud services by typing commands on a specified line.

A template is a declaration of the AWS resources that make up a stack. The template is stored as a text file whose format can be JavaScript Object Notation (JSON) or YAML standard.

CloudFormation Templates simplifies provisioning and management on AWS. CloudFormation uses those templates for quick and reliable provisioning of the services or applications.

How we are using Cloud formation?

Generally, we will write a cloud formation stack to create multiple resources in AWS.

In this example, we need to create an S3 bucket, We will create a cloud formation template stack to create such resources. When we run this stack in cloud formation service in AWS. S3 bucket will be created with the specified properties.
Note: Properties can be updated later by updating the cloud formation stacks.

So, When we need to create any AWS resources, we will create a cloud formation stack and run it in cloud formation service in AWS.

What if we need to create an entire web application with a single click?

Before we created a cloud formation template to create multiple resources. In a real-time application, we need to create 100–200 resources, i.e, RDS, S3, EC2, VPC, Subnets, etc.

If we need to create all resources needed for web application and make it a single click deployment. Here comes the solution for that,

One-click deployment is a way of deploying an entire application with the help of cloud formation service in AWS in a single click. i.e, by running one Cloudformation template.

One-click deployment

Let’s get this by an example, Consider we are deploying a web application in a new AWS account. Instead of having multiple templates for multiple resources, we can have all resources in templates that are ordered being multi-level.

Let us build our first serverless web application, AWS Resources we need are listed here,

IAM Roles, Policies, VPC, Subnet, NAT gateway.

API gateway, Lambda functions, Log groups.

S3 bucket, bucket policies, Cloudfront, WAF.

Cognito user pool, email templates, SNS.

RDS Instances, KMS keys.

These are the set of resources we use for a basic web application. So, we will start creating these resources using Cloudformation templates. First, we will create a master stack (a stack that is going to be deployed in AWS Cloudformation service). Master stack will have resources of type AWS::CloudFormation::Stack.

Here, we have created multiple stacks where we have multiple resources inside each stack. All these stacks are differentiated based on the type of resources they have. In the master stack, we mapped all the stacks based on the order of execution. Each stack will have multiple configurations such as DependsOn, which says that a particular stack depends on other stacks.

By doing this, we can able to control the order of execution of stacks. So, instead of having multiple stacks, we can map it to one stack and we can run this stack alone, It will automatically handle all other stacks.

Inputs and Outputs:

The input for the stack is called Parameters. The Parameters are passed from master stack to child stack. The output of each stack is also handled similarly.

Error handling:

If any errors on any of the stacks, It will automatically delete the entire resources of all the stacks. So, It will be in a case either the entire application is built or nothing. Though some resources take much time like RDS instances, Other stacks will wait until it is completed.

Updating entire Application

Updating the master stack is similar to updating the individual stack, On updating, it will check all the stacks about the changes, and it will update only that particular resource.

Migrating application

This methodology plays a major role in migrating the application from one account to other. Or If you need to remove the application from one account. Instead of deleting all resources manually, Here we just delete one stack which removes the entire application. And also create a new application in the new AWS account.

This approach has some extra work on start, as we need to create multi-level templates. But, in the long run, we don’t need to look after each resource, and updating and migrating will be a cakewalk.

--

--