AWS CloudFormation

Shruti
featurepreneur
Published in
3 min readJan 20, 2021
AWS CloudForamtion

In AWS, configuration and provisioning of any resource are done either through its CLI or the console. When you use them, you have to follow several steps to configure the resources or the architecture. For fewer resources, it will not be as hard to configure, but in the case of large scale deployments, it will be tough to handle the configurations. This is where CloudFormation comes into the picture. CloudFormation is strictly an AWS service to configure resources using a template. This template is used to define all required configurations to set up an entire architecture in one go.

The AWS CloudFormation template lets you declare the infrastructure resources and their settings in a single file. This template can either be written in JSON or YAML format.

  1. An example template in JSON format

2. An example template in YAML format

A basic skeleton of a CloudFormation template in YAML:

AWSTemplateFormatVersion: 2010-09-09 # Optional
Description: # Optional
Parameters: # Optional
Mappings: # Optional
Conditions: # Optional
Resources: # Required
Outputs: # Optional
  • AWSTemplateFormatVersion: 2010–09–09: This is the latest (at the time this article was written) default template format supported by AWS.
  • Description: A basic introduction/description briefing the purpose of the template.
  • Parameters: They’re basically inputs to the template. Parameters allow you to pass user inputs at the time of stack creation or update. A parameter name is a user-defined name and should be unique.
  • Mappings: This allows you to create a “key: value” map of literal strings. It matches a key to a corresponding set of named values. A common use case of Mappings in CloudFormation is declaring a map of AWS region to AMI Ids. This mapping allows the use of the same template across multiple regions.
  • Conditions: This section of the template allows you to pre-condition the creation of a resource or generate the output if the condition statement is true or false. A condition is evaluated based on the input parameter values supplied by the user during stack launch.
  • Resources: This section of the template must be present in a cloud formation template for stack creation. Each AWS infrastructure resource can be defined separately. During stack creation/updation, these resources will be deployed based on their definition. Without this, we can’t configure any resource in a CloudFormation template.
  • Outputs: This is a crucial section in a template when you plan to return resource values from your current stack to be used by other stacks in a cross-stack reference or a nested stack scenario. They are used to get values after the execution of the template.

Parameters and Resources are the most important top-level properties of a CloudFormation template.

To launch a stack from the example given above:

aws cloudformation create-stack --template-body file://templates/single-instance.yml --stack-name single-instance --parameters ParameterKey=KeyName,ParameterValue=tutorial ParameterKey=InstanceType,ParameterValue=t2.micro

To check on the status of the newly launch stack, you can use the AWS CloudFormation console.

To destroy the resources that were created so you do not get charged more money than you have to:

aws cloudformation delete-stack --stack-name single-instance

Benefits of AWS CloudFormation:

  • Simplify infrastructure management
  • Quickly replicate your infrastructure
  • Easily control and track changes to your infrastructure

Check out the AWS docs for a better understanding!

--

--

Shruti
featurepreneur

Tech builds shouldn't be intimidating – I break down my projects in detail, turning my learning experiences into your shortcuts.