AWS CloudFormation is like a service that helps you model and set up your AWS resources so that you can like spend less time managing those resources and more time focusing on what matters most that is your application. The best part is you don't need to individually create and configure AWS resources and figure out what's dependent on what Cloud Formation handles that for you.
Introduction
I am Prajwal KP, a second-year student from Bangalore, passionate about full-stack web development and DevOps.
Here is my LinkedIn, GitHub
What is AWS CloudFormation?
AWS CloudFormation is an Infrastructure as Code( IAC ) service that allows you to define and provision AWS resources in a declarative way.
We use CloudFormation templates which can be written in YAML or JSON to like describe the desired state of the infrastructure.
Why do we need CloudFormation?
Managing cloud infra manually can quickly become complex and error-prone, mainly on a application scale.
AWS CloudFormation solves this by offering a way to define, deploy, and manage cloud resources using code. Here’s why it’s essential:
Automation and Consistency: With CloudFormation, we can automate resource creation and ensure every deployment is consistent — no more accidental misconfigurations.
Infrastructure as Code (IaC): It lets us version-control your infrastructure the same way we manage application code, making rollbacks and collaboration easier.
Dependency Management: CloudFormation automatically handles resource dependencies, so we don’t have to worry about creating resources in the correct order — it figures that out for us.
Scalability: Need to spin up identical environments for development, testing, or production? CloudFormation makes it seamless by deploying the same stack repeatedly.
Change Management: It allows you to preview changes (using Change Sets) before applying them, reducing the risk of unexpected updates breaking your setup.
Cross-Region and Cross-Account Deployments: Easily replicate infrastructure across AWS regions and accounts using a single template.
Now lets look into some terminology
Understanding AWS CloudFormation involves a few key terms. Let’s break them down simply and clearly!
-
Templates: These are text files written in either JSON or YAML that acts as the "scripts" for CloudFormation.
- CloudFormation reads templates as inputs.
- They describe the desired state of your infrastructure.
-
Stacks: A stack is a collection of AWS resources that CloudFormation manages as a single unit.
- CloudFormation uses templates to create stacks.
- To make changes, you updates the template and apply the changes by updating the stack.
-
ChangeSet: Before updating a stack, you can generate a change set.
- A change set shows how proposed changes will affect your existing resources.
- This is crucial for live systems to prevent unexpected disruptions.
- For example, renaming an RDS instance will:
- Create a new instance.
- Delete the old one.
- Result: This could cause data loss if not handled carefully.
Now lets look into Templates.
In AWS CloudFormation, templates are written with either JSON or YAML files that define the AWS resources you want to create, like EC2 instances, S3 buckets, or IAM roles. These templates act as blueprints for your infrastructure.
Templates Formats
CloudFormation supports two formats for writing templates:
- YAML: A human-readable format, easier to write and maintain.
- JSON: A structured format, more rigid but familiar to developers used to working with APIs.
Most developers prefer YAML because of its simplicity, especially for larger templates. Both formats work the same way, so it's just a matter of readability and convenience.
Example for template(JSON)
{
"AWSTemplateFormatVersion": "version date",
"Description": "JSON string",
"Metadata": {},
"Parameters": {},
"Rules": {},
"Mappings": {},
"Conditions": {},
"Transform": {},
"Resources": {},
"Outputs": {}
}
Example for template(YAML)
---
AWSTemplateFormatVersion: version date
Description: String
Metadata: template metadata
Parameters: set of parameters
Rules: set of rules
Mappings: set of mappings
Conditions: set of conditions
Transform: set of transforms
Resources: set of resources
Outputs: set of outputs
Now lets start an instance.
create a file named .yaml
extenstion and write down the below code
Resources:
MyInstance:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: ami-0ff8a91507f77f867
Now lets run the above template
Search for the CloudFormation service in AWS.
- Click on create task.
- Click on Template is ready -> Upload a template file.
- Select the file and upload it.
- Give the stack name -> Click Next.
- U can give some tags if required -> u continue with the default -> Next.
- Last in review section just check everything and then click Create stack.
- We can see that in events tab u can look at the status-it might say CREATE_IN_PROGRESS.
- Click on refresh to see the updated status.
- It says CREATE_COMPLETE after the completion.
now for updating the name we add tags to specify the instance name
Resources:
MyInstance:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: ami-0ff8a91507f77f867
Tags:
- Key: "Name"
Value: "MyFirstInstance"
- Now back to AWS console click on Update stack -> Replace current template -> Update a template file -> Click Next -> Keep everything default and click next -> At last click Update stack.
- After it updates we can verify it by checking the EC2 instance and the name of it should be updated.
This is it for the introduction to AWS CloudFormation.
Top comments (0)