AWS CloudFormation
CloudFormation
AWS CloudFormation is an Infrastructure as Code (IaC) service that allows you to define and provision AWS infrastructure using JSON or YAML templates.
Instead of manually creating resources in AWS Console, you define infrastructure in a template file and deploy it as a Stack.
Uses CloudFormation
- Infrastructure as Code (IaC)
- Automation
- Version control
- Repeatable deployments
- Rollback on failure
- Change tracking
- Drift detection
How CloudFormation Works
Step-by-Step Flow
- Write Template (JSON/YAML)
- Upload Template to CloudFormation
- Create Stack
- CloudFormation reads template
- Calls AWS service APIs
- Creates resources in correct order
- Stack creation completes
Flow Diagram
flowchart TD
A[CloudFormation Template] --> B[JSON / YAML]
B --> C[CloudFormation Service]
C --> D[AWS Resource APIs] --> E[S3, EC2, IAM, VPC]
E --> F[AWS Resources] --> G[S3 Bucket, EC2, etc.]
Template Structure
## Template Structure
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "TemplateDescription",
"Parameters": {},
"Resources": {},
"Outputs": {}
}
Example: JSON Template to Create S3 Bucket
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Create an S3
Bucket using CloudFormation", "Resources": { "MyS3Bucket": { "Type":"AWS::S3::Bucket",
"Properties": {
"BucketName": "my-demo-cloudformation-bucket-12345",
"VersioningConfiguration": {
"Status": "Enabled"
}
}
}
},
"Outputs": {
"BucketName": {
"Description": "Name of the S3 bucket",
"Value": {
"Ref": "MyS3Bucket"
}
}
}
}
Deployment Using AWS CLI
aws cloudformation create-stack
–stack-name my-s3-stack
–template-body file://s3-bucket.json
Important Exam Points
- Declarative Infrastructure
- Supports JSON & YAML
- Automatic rollback
- Change Sets
- Drift Detection
- Stack-based resource management
CloudFormation template
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "create-s3",
"Parameters": {},
"Resources": {
"s3Bucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": "qt-demo-9876543",
"Tags": [
{
"Key" : "enviroment",
"Value" : "dev"
}
]
}
}
},
"Outputs": {
"BucketName": {
"Value": {
"Ref": "s3Bucket"
},
"Description": "Name of the sample Amazon S3 bucket with a lifecycle configuration."
}
}
}
