Why Should You Use AWS CloudFormation?

Introduction

Managing the data and information with many services can be really hard. Also, managing and creating multiple resources of AWS can be time-consuming and challenging.
In fact, doing such things could cause spending a lot of time handling or managing the AWS resources instead of developing the applications.
How one can solve this?

CloudFormation performs a major role here!


It can manage or handle your complete infrastructure and can provide the simplest way to create and manage almost all of your AWS resources in a Template. And the collection of the AWS resources is called “Stack”.

Users can create or update any AWS resource using the template which is reusable i.e. user can reuse the template to replicate your infrastructure in multiple environments.
User can use mappings, conditions, and parameters sections to make the template reusable and for customization.

Stacks in CloudFormation

The Collection of AWS resources is known as a Stack, and that can be handled or managed in a single unit. The Stack in which all the resources can be modified, deleted, updated, or created in a specific way. The Stack can have every resource (e.g. database, web servers, VPC, etc.) which are required to run any web application.


The hierarchy of stacks can be called as Nested Stack. We can create nested stacks (i.e. stack within another stack) by using the CloudFormation stack resource.

The Windows stack can configure and update your own stack in Windows servers or instances. Using AWS CloudFormation, you can create Microsoft Windows stacks for Amazon EC2’s Windows Amazon Machine Images (AMI).

You can define a StackSet Using an AWS CloudFormation template by which you can create the stacks in your AWS account around the globe using a single template. After a StackSet is defined by you, creating, updating or deleting stacks in the target accounts and regions can also be specified.

Concepts Of CloudFormation

The CloudFormation allows you to model your complete infrastructure and application resources with either a programming language or text file. The CloudFormation Registry can make it easy to manage third party resources with CloudFormation. This provides a single source of truth for all your resources and helps you to standardize infrastructure components used across your organization, enabling configuration compliance and faster troubleshooting.


An AWS CloudFormation template is a formatted text file in JSON or YAML language that describes your AWS infrastructure. To create, view, and modify templates, you can use AWS CloudFormation Designer or any text editor tool. An AWS CloudFormation template consists of nine main objects:

  • Format version
  • Description
  • Metadata
  • Parameters
  • Mappings
  • Conditions
  • Transform
  • Resources
  • Output

Feature Description

Format version: This Format version describes or defines the capability of the CloudFormation template.

Description: Comments on your template can be written or specified in the description section.

Metadata: The metadata section can be used inside the template to provide any further information using either YAML or JSON objects.

Parameters: You can customize the Templates using parameters. Parameters can help you give your template custom values at runtime each time you create or update your stack.

Mappings: The mapping section enables you to map keys to a corresponding named value that you specify in a conditional parameter. And you can retrieve values in a map by using the “Fn:: FindInMap” intrinsic function.

Conditions: The conditions section defines whether specific resources are created or when will be the resource properties are assigned to a value during the stack creation or updating. Conditions can be used to reuse the templates by creating resources in different ways or contexts. You can use the functions (intrinsic) to define conditions.

In your template, all the conditions are evaluated during stack creation. All the resources that are associated with a valid or true condition are created, and the invalid conditions are ignored automatically.

Transform: This Transform section can enable the reuse of template components and builds a simple declarative language for AWS CloudFormation. And here, you can declare a single or multiple transforms within a template.

Resources: In the Resources section, you can declare the AWS resource that you want to specify and create in the stack, like an AWS Lambda or Amazon S3 bucket.​​​​​​​

Output: In this section, it describes the output values that you can import into any other stacks or values that are returned when you view your own stack properties (e.g. for an S3 bucket name, you can declare an output and use the “Description-stacks” command from the AWS CloudFormation service to make the bucket name easier to find).

CloudFormation - Template Resource Attribute

CreationPolicy
Associate the CreationPolicy attribute with a resource if you want to delay resource configuration actions before proceeding with stack creation.

A stack creation is delayed with this attribute until AWS CloudFormation receives a specified number of success signals. And it can be used only for an AWS EC2 instance, an AWS CloudFormation, and AWS AutoScaling.

When an associated resource is created in AWS CloudFormation, it configures many required success signals and the time length that AWS CloudFormation waits for those signals.

DeletionPolicy
Using the DeletionPolicy, preserving and backing up a resource is possible when its stack is deleted.

And By default, AWS CloudFormation deletes all the resource and all its content if a resource has no DeletionPolicy attribute in the template. Before deleting any resource, CloudFormation creates a snapshot (backup) of that resource.

And when this stack will be deleted, AWS CloudFormation leaves the bucket without deleting it.

DependsOn
The DependsOn attribute can define the creation of a specific resource in the template followed by another resource.

Example 1: Resource X and resource Y (where resource X is assigned to DependsOn). Result: Resource Y is created before resource X.


Example 2: AWS EC2 resource with a specified AWS S3 bucket resource (where S3 is assigned to DependsOn attribute). When a stack is created by AWS CloudFormation, it first creates an EC2 instance, then creates an S3 bucket.

Metadata
The Metadata attribute enables you to associate a resource with structured data. By adding this Metadata attribute to a resource, you can specify the data in either YAML or JSON language.

UpdatePolicy
With this UpdatePolicy attribute in the AWS CloudFormation, you can arrange, manage, and replace the instances updates in the Auto Scaling group. And during an update, it specifies whether an Auto Scaling group and the instances it contains are replaced.