Day - 10 | AWS CloudFormation
Getting Started with AWS CloudFormation (Infrastructure as Code)
As part of my cloud learning journey, I recently explored AWS CloudFormation, a powerful service that allows us to define and provision AWS infrastructure using code. CloudFormation is a core service for anyone working in Cloud, DevOps, or SRE roles, as it enables automation, consistency, and scalability in infrastructure management.
In this blog, Iโll share my understanding of AWS CloudFormation, its key components, and how it helps manage cloud resources efficiently.
โ๏ธ What is AWS CloudFormation?
AWS CloudFormation is an Infrastructure as Code (IaC) service that allows you to model AWS resources using templates written in YAML or JSON. Instead of manually creating resources through the AWS Console, you can define everything in a template and let CloudFormation handle the provisioning.
With CloudFormation, AWS takes care of:
Creating resources in the correct order
Managing dependencies between resources
Rolling back changes if something fails
๐ Why Use CloudFormation?
Some key benefits of AWS CloudFormation include:
Automation โ Provision infrastructure with a single command
Consistency โ Same template produces the same infrastructure every time
Version control โ Templates can be stored in Git
Scalability โ Easily create or update large infrastructures
Rollback support โ Automatic rollback on failure
๐งฉ Core Components of CloudFormation
๐น Template
A template is a text file written in YAML or JSON that describes AWS resources and their configurations.
๐น Stack
A stack is a collection of AWS resources created and managed together using a template.
๐น Change Sets
Change sets allow you to preview how proposed changes to a stack will affect existing resources before applying them.
๐ Structure of a CloudFormation Template
A typical CloudFormation template includes the following sections:
AWSTemplateFormatVersion โ Template version
Description โ Description of the template
Parameters โ Input values provided at runtime
Resources โ AWS resources to be created (mandatory section)
Outputs โ Values returned after stack creation
๐ ๏ธ Sample CloudFormation Template (EC2 Example)
AWSTemplateFormatVersion: '2010-09-09'
Description: Simple EC2 instance using CloudFormation
Parameters:
InstanceType:
Type: String
Default: t2.micro
Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
InstanceType: !Ref InstanceType
ImageId: ami-0abcdef1234567890
Outputs:
InstanceId:
Description: EC2 Instance ID
Value: !Ref MyEC2Instance
This template launches a basic EC2 instance in AWS using predefined parameters.
๐ Managing Security with CloudFormation
CloudFormation integrates seamlessly with IAM to control who can create, update, or delete stacks. Best practices include:
Using IAM roles for CloudFormation
Granting least privilege permissions
Avoiding hard-coded credentials in templates
๐ Updating and Deleting Stacks
Update Stack โ Modify the template and update the stack
Delete Stack โ Removes all resources created by the stack automatically
This makes cleanup and environment management much easier.
๐ง Real-World Use Cases
Provisioning complete application infrastructure
Creating development, staging, and production environments
Automating VPC, EC2, S3, IAM setups
Managing infrastructure in CI/CD pipelines