Skip to main content

Command Palette

Search for a command to run...

πŸš€ IaC with AWS CloudFormation (CFT) | Complete Beginner Guide with Hands-on Examples

Learn AWS CloudFormation (CFT) from scratch with simple explanations, real-world examples, YAML templates, best practices, Drift Detection, CloudFormation vs Terraform, interview questions, and hands-on demonstrations.

Updated
β€’14 min readβ€’View as Markdown
πŸš€ IaC with AWS CloudFormation (CFT) | Complete Beginner Guide with Hands-on Examples
H
πŸ‘‹ Hi, I’m Hritik Ranjan β€” a B.Tech CSE student and a passionate tech enthusiast focused on Quality Engineering, AI/ML, Cybersecurity, and DevOps. πŸ’‘ I enjoy building and testing scalable, secure, and intelligent systems that solve real-world problems. My expertise and interests include: πŸ”Ή Quality Assurance & Testing Hands-on experience in manual and automation testing using Selenium & Java, ensuring high-quality and reliable applications. πŸ”Ή Artificial Intelligence & Machine Learning Exploring advanced algorithms and developing intelligent systems for practical use cases. πŸ”Ή Cybersecurity Focused on vulnerability assessment, security testing, and system hardening. πŸ”Ή Web Development Building responsive and user-friendly applications using modern technologies. πŸ”Ή Data Science Analyzing complex data to extract actionable insights. πŸ’Ό Key Projects: πŸš€ Blindness Detection System Applied computer vision techniques to detect blindness-related conditions. πŸš€ AI-Powered Rail Madad Enhancement Developed an intelligent complaint management system to improve railway customer service. πŸš€ Interactive Applications Built multiple projects like quiz apps, calculators, and productivity tools. 🌱 I’m continuously learning and improving my skills in DevOps, Cloud, and Automation to become a well-rounded engineer. 🀝 Open to collaborations, internships, and opportunities in QA, DevOps, AI/ML, and Cybersecurity. πŸ“« Let’s connect: hritikranjan1408@gmail.com

πŸ“– Introduction

As cloud infrastructure grows, manually creating AWS resources becomes time-consuming, error-prone, and difficult to manage.

Imagine creating:

  • 20 EC2 Instances

  • 15 S3 Buckets

  • 8 IAM Roles

  • 10 Security Groups

  • 5 VPCs

every time you deploy an application.

Sounds impossible, right?

This is where Infrastructure as Code (IaC) comes into the picture.

Instead of creating infrastructure manually from the AWS Console, we define everything in code.

AWS provides its own IaC service called CloudFormation (CFT).

In this guide, we'll learn everything about CloudFormation in a beginner-friendly way.


πŸ“š Table of Contents

  • What is Infrastructure as Code (IaC)?

  • Why do we need IaC?

  • Problems with Manual Infrastructure

  • What is AWS CloudFormation?

  • How CloudFormation Works

  • CloudFormation Architecture

  • CloudFormation Components

  • Anatomy of a CloudFormation Template

  • Resources Section

  • Parameters

  • Outputs

  • Mappings

  • Conditions

  • Metadata

  • YAML vs JSON

  • Creating First CloudFormation Stack

  • Deploying an S3 Bucket

  • Stack Lifecycle

  • Change Sets

  • Drift Detection

  • CloudFormation Designer

  • CloudFormation Best Practices

  • CloudFormation vs Terraform

  • Advantages & Limitations

  • Real World Use Cases

  • Interview Questions

  • Conclusion


☁️ What is Infrastructure as Code (IaC)?

Infrastructure as Code (IaC) means creating and managing cloud infrastructure using code instead of manually clicking through the AWS Console.

Instead of logging into AWS and creating resources one by one, you write a template that describes your infrastructure.

AWS automatically creates everything from that template.

Think of IaC like a blueprint for your infrastructure.

Just as architects use blueprints to construct buildings, DevOps engineers use IaC templates to build cloud infrastructure.


πŸ€” Why Do We Need Infrastructure as Code?

Without IaC:

  • Manual work

  • Human errors

  • Inconsistent environments

  • Difficult deployments

  • Slow provisioning

  • Hard to reproduce infrastructure

With IaC:

  • Automation

  • Consistency

  • Repeatability

  • Version control

  • Faster deployments

  • Easy disaster recovery


❌ Problems with Manual Infrastructure

Suppose you have to deploy an application.

You manually create:

  • EC2 Instance

  • VPC

  • Security Group

  • S3 Bucket

  • IAM Role

  • Load Balancer

Next month, you need the same infrastructure for testing.

You repeat everything again.

There is a high chance of:

  • Missing configurations

  • Wrong security rules

  • Different instance types

  • Incorrect subnet configuration

IaC solves all these problems.


☁️ What is AWS CloudFormation?

AWS CloudFormation is an Infrastructure as Code (IaC) service that allows you to create, update, and manage AWS resources using YAML or JSON templates.

Instead of manually provisioning resources, you define them in a template, and CloudFormation automatically creates everything for you.


πŸ—οΈ Real-Life Example

Imagine you're building a house.

Instead of explaining every room to the workers every day, you give them a blueprint.

They build the same house every time.

CloudFormation works exactly the same way.

Your YAML file is the blueprint.

AWS builds the infrastructure.


βš™οΈ How CloudFormation Works

Developer

↓

Write YAML Template

↓

CloudFormation Stack

↓

AWS APIs

↓

AWS Resources Created

CloudFormation acts as a middle layer between your template and AWS services.


πŸ› CloudFormation Architecture

Template (YAML/JSON)

↓

CloudFormation

↓

AWS APIs

↓

EC2
S3
IAM
VPC
Lambda
RDS
CloudFront
Load Balancer
Security Groups

πŸ“„ What is a CloudFormation Template?

A template is simply a YAML or JSON file containing all the infrastructure details.

Example:

Create

1 EC2
1 S3 Bucket
1 IAM Role
1 Security Group

Everything is defined inside one file.


🧩 Components of a CloudFormation Template

A CloudFormation template can include:

  • AWSTemplateFormatVersion

  • Description

  • Metadata

  • Parameters

  • Mappings

  • Conditions

  • Resources βœ… (Mandatory)

  • Outputs

πŸ‘‰ Resources is the only mandatory section. All other sections are optional.


πŸ“Œ Resources (Most Important Section)

The Resources section defines the AWS services that CloudFormation will create.

Example resources:

  • EC2 Instance

  • S3 Bucket

  • Lambda Function

  • IAM Role

  • Security Group

  • RDS Database

  • VPC

Without this section, CloudFormation cannot create anything.


🎯 Parameters

Parameters allow users to provide values during stack creation instead of hardcoding them.

Examples:

  • Instance Type

  • Bucket Name

  • Region

  • Environment (Dev, Test, Prod)

Benefits:

  • Reusable templates

  • Flexible deployments

  • Better customization


πŸ“€ Outputs

Outputs display useful information after deployment.

Examples:

  • EC2 Public IP

  • Load Balancer DNS

  • Bucket Name

  • VPC ID

This saves time by providing important values immediately after stack creation.


πŸ—ΊοΈ Mappings

Mappings help define different values for different regions or environments.

Example:

  • Mumbai β†’ AMI A

  • Virginia β†’ AMI B

This avoids hardcoding region-specific values.


πŸ”€ Conditions

Conditions allow CloudFormation to create resources only if specific criteria are met.

Example:

If Environment = Production

Create:

  • RDS Database

  • Auto Scaling Group

Else

Skip those resources.


πŸ“ Metadata

Metadata stores additional information about the template.

It doesn't create resources but helps document and organize your infrastructure.


πŸ“„ YAML vs JSON

CloudFormation supports:

  • YAML βœ… (Recommended)

  • JSON

YAML is easier to read, cleaner, and widely used in DevOps projects.


πŸš€ Creating Your First CloudFormation Stack

Step 1

Open AWS Console

↓

CloudFormation


Step 2

Create Stack


Step 3

Upload YAML Template


Step 4

Provide Stack Name


Step 5

Review Configuration


Step 6

Click Create Stack

CloudFormation automatically provisions all resources.


πŸͺ£ Demo Project – Create an S3 Bucket

CloudFormation Template:

AWSTemplateFormatVersion: '2010-09-09'

Description: Create S3 Bucket

Resources:

  MyBucket:

    Type: AWS::S3::Bucket

    Properties:

      BucketName: my-demo-cloudformation-bucket

Deploy the template.

Within seconds, AWS creates the S3 bucket automatically.


πŸ“¦ What is a Stack?

A Stack is a collection of AWS resources created from a CloudFormation template.

One template = One Stack

A stack can include:

  • EC2

  • VPC

  • S3

  • IAM

  • Lambda

  • RDS

Everything is managed together.


πŸ”„ Stack Lifecycle

CloudFormation supports:

  • Create Stack

  • Update Stack

  • Delete Stack

  • Rollback Stack

This makes infrastructure management simple.


πŸ” What is Drift Detection?

Drift Detection checks whether someone manually changed AWS resources outside CloudFormation.

Example:

Template says:

S3 Versioning Enabled

Someone manually disables versioning from AWS Console.

CloudFormation detects the difference.

This difference is called Drift.


🎨 CloudFormation Designer

AWS provides a visual drag-and-drop interface called CloudFormation Designer.

Benefits:

  • Visual architecture

  • Easy editing

  • Beginner friendly

  • Auto-generates templates


πŸ’‘ Tips & Tricks for Writing Better Templates

  • Use YAML instead of JSON.

  • Keep templates modular.

  • Use Parameters instead of hardcoding values.

  • Add Outputs for important resources.

  • Use Change Sets before updating production.

  • Organize templates with comments.

  • Validate templates before deployment.

  • Store templates in GitHub for version control.


βœ… Best Practices

  • Follow Least Privilege IAM.

  • Enable Drift Detection regularly.

  • Use descriptive stack names.

  • Separate Dev, Test, and Production stacks.

  • Use nested stacks for large projects.

  • Never hardcode secrets.

  • Reuse templates.

  • Test templates before production.


βš–οΈ CloudFormation vs Terraform

Feature CloudFormation Terraform
Cloud Support AWS Only Multi-Cloud
Language YAML / JSON HCL
AWS Integration Excellent Excellent
Azure Support No Yes
GCP Support No Yes
Learning Curve Easy Moderate
State File Managed by AWS Local/Remote
Open Source No Yes

🎯 When Should You Use CloudFormation?

Choose CloudFormation if:

  • You only use AWS.

  • You want native AWS integration.

  • You need tight AWS service support.

  • Your infrastructure is AWS-specific.


🎯 When Should You Use Terraform?

Choose Terraform if:

  • You manage AWS + Azure + GCP.

  • You want cloud-independent infrastructure.

  • You use Kubernetes, VMware, GitHub, Cloudflare, or other providers.

  • You need multi-cloud deployments.


🌍 Real-World Use Cases

CloudFormation is widely used for:

  • Creating complete VPC architectures

  • Deploying EC2 fleets

  • Setting up S3 buckets

  • Creating IAM users and roles

  • Deploying Lambda functions

  • Creating RDS databases

  • Auto Scaling Groups

  • Load Balancers

  • Disaster Recovery environments

  • CI/CD infrastructure automation


🎯 Advantages

  • Native AWS service

  • Fully automated deployments

  • Version-controlled infrastructure

  • Repeatable environments

  • Easy rollback

  • Cost-effective

  • Supports Drift Detection

  • Integrates with CI/CD pipelines


⚠️ Limitations

  • AWS only

  • YAML/JSON can become large

  • Less flexible than Terraform for multi-cloud

  • Debugging complex templates can be challenging


πŸ’Ό Real-World DevOps Workflow

Developer

↓

GitHub Repository

↓

CloudFormation Template

↓

CI/CD Pipeline

↓

CloudFormation Stack

↓

AWS Infrastructure

↓

Application Deployment

🎀 AWS CloudFormation Interview Questions & Answers

Q1. What is AWS CloudFormation?

Answer: AWS CloudFormation is an Infrastructure as Code (IaC) service that lets you create, manage, and update AWS resources using YAML or JSON templates instead of manual configuration.


Q2. What is Infrastructure as Code (IaC)?

Answer: IaC is the practice of managing infrastructure using code, enabling automation, consistency, version control, and repeatable deployments.


Q3. Which template formats does CloudFormation support?

Answer: YAML and JSON. YAML is preferred because it is easier to read and maintain.


Q4. Which section is mandatory in a CloudFormation template?

Answer: The Resources section. Without it, CloudFormation cannot create any AWS resources.


Q5. What is a CloudFormation Stack?

Answer: A Stack is a collection of AWS resources created and managed together using a single CloudFormation template.


Q6. What are Parameters?

Answer: Parameters allow users to provide input values (such as instance type or bucket name) during stack creation, making templates reusable and flexible.


Q7. What are Outputs?

Answer: Outputs display useful information after deployment, such as EC2 Public IP, Load Balancer DNS, or S3 Bucket Name.


Q8. What is Drift Detection?

Answer: Drift Detection identifies differences between the CloudFormation template and the actual AWS resources if someone manually changes resources outside CloudFormation.


Q9. What is a Change Set?

Answer: A Change Set previews the changes CloudFormation will make before updating a stack, helping avoid accidental modifications.


Q10. What is the difference between CloudFormation and Terraform?

Answer: CloudFormation is AWS-native and best for AWS-only environments, while Terraform supports multiple cloud providers (AWS, Azure, GCP) and is ideal for multi-cloud infrastructure.


πŸŽ‰ Conclusion

AWS CloudFormation is a powerful Infrastructure as Code service that helps automate AWS resource provisioning, improve consistency, and simplify infrastructure management. By using reusable YAML templates, Drift Detection, and stack management features, you can deploy reliable cloud environments with confidence. If your organization primarily uses AWS, CloudFormation is an excellent choice for managing infrastructure efficiently and following modern DevOps best practices.


πŸš€ Continue Your Learning Journey

Thank you for taking the time to read this article.

Technology is evolving rapidly, and continuous learning is one of the most valuable investments you can make in your career. Whether you're exploring DevOps, Cloud Computing, Artificial Intelligence, Cybersecurity, Software Development, Data Science, or Career Growth, the resources below can help you deepen your knowledge and stay ahead in the industry.


πŸŽ“ Recommended Learning Platforms

πŸš€ Coursera

Learn from world-renowned universities and industry leaders including Google, IBM, Stanford, Microsoft, Meta, and many more.

βœ” Professional Certificates βœ” Career-focused Learning Paths βœ” AI & Machine Learning Programs βœ” Cloud & DevOps Certifications βœ” Business & Leadership Courses

πŸ”— https://imp.i384100.net/k0KvbV


πŸ’» Udemy

One of the largest online learning platforms with practical, hands-on courses covering:

βœ” DevOps & Kubernetes βœ” Docker & Cloud Computing βœ” AWS, Azure & GCP βœ” Programming & Development βœ” Cybersecurity & Ethical Hacking

πŸ”— https://trk.udemy.com/MAL2MY


πŸ“Š DataCamp

A great platform for anyone interested in:

βœ” Python Programming βœ” SQL & Databases βœ” Data Analytics βœ” Machine Learning βœ” Artificial Intelligence

Interactive learning paths and hands-on projects make it ideal for beginners and professionals alike.

πŸ”— https://datacamp.pxf.io/nX4kER


πŸŽ“ edX

Access high-quality courses and certifications from leading institutions such as:

βœ” Harvard University βœ” MIT βœ” Berkeley βœ” Microsoft

Perfect for learners seeking university-level education online.

πŸ”— https://edx.sjv.io/POvVeN


🎨 Domestika

Enhance your creative skills with courses on:

βœ” Graphic Design βœ” Video Editing βœ” Animation βœ” Digital Marketing βœ” Content Creation

πŸ”— https://domestika.sjv.io/dynKAW


πŸ› οΈ Recommended Tools & Resources

πŸ”₯ AppSumo

Discover exclusive lifetime deals on:

βœ” AI Tools βœ” Productivity Software βœ” Developer Utilities βœ” Marketing Platforms βœ” Business Applications

A must-have resource for developers, creators, freelancers, and entrepreneurs looking to save money while accessing premium tools.

πŸ”— https://appsumo.8odi.net/L04a33


πŸ›’ Shopify

Looking to start an online business or launch an eCommerce store?

Shopify provides everything you need to build, manage, and scale an online business.

βœ” Online Store Builder βœ” Payment Integration βœ” Inventory Management βœ” Marketing Tools

πŸ”— https://shopify.pxf.io/Vxv09k


🌐 WordPress, WooCommerce & Jetpack

Create professional websites, blogs, and online stores with one of the most trusted web ecosystems in the world.

Ideal for:

βœ” Personal Blogs βœ” Portfolio Websites βœ” Business Websites βœ” eCommerce Stores

πŸ”— https://automattic.pxf.io/Z6vR5W


🌍 Language Learning Resources

πŸ—£οΈ Preply

Learn English and other languages through personalized one-on-one tutoring sessions with experts from around the world.

πŸ”— https://preply.sjv.io/o4gBDY


πŸ“š British Council English Online

Improve your professional communication skills and English fluency through structured learning programs.

πŸ”— https://englishonline.sjv.io/9VOGa4


🧠 Rosetta Stone

One of the most recognized language-learning platforms for immersive language acquisition.

πŸ”— https://aff.rosettastone.com/X4OyqG


πŸ§ͺ Science & Educational Resources

πŸ”¬ MEL Science

Interactive science kits and educational experiences designed to make STEM learning engaging and practical.

πŸ”— https://imp.i328067.net/bk2beg


πŸ“– Carson Dellosa Education

Educational materials and learning resources for students, teachers, and lifelong learners.

πŸ”— https://carsondellosaeducation.sjv.io/E0JbjW


❀️ Support My Work

Creating detailed technical content, tutorials, guides, and learning resources takes significant time and effort.

If you find my articles helpful and would like to support my work, you can do so through the following platforms:

⭐ Become a GitHub Sponsor

Support my open-source contributions, technical content, and community projects.

πŸ”— https://github.com/sponsors/hritikranjan1


β˜• Buy Me a Chai

Enjoying my content? Consider buying me a chai and supporting future tutorials, guides, and educational resources.

πŸ”— https://www.chai4.me/hritikranjan


πŸ‘¨β€πŸ’» Connect With Me

Hritik Ranjan

πŸ’‘ AI Enthusiast ☁️ DevOps Learner πŸ” Cybersecurity Advocate πŸ’» Software Developer

Connect & Follow

πŸ”— GitHub: https://github.com/hritikranjan1

πŸ”— LinkedIn: https://linkedin.com/in/hritikranjan1


πŸ“’ Found This Article Helpful?

If this article added value to your learning journey:

βœ… Share it with your network βœ… Bookmark it for future reference βœ… Follow for more DevOps, AI, Cloud, Cybersecurity, and Software Engineering content

Thank you for reading and being part of this learning journey.

Keep Learning. Keep Building. Keep Growing. πŸš€

AWS for DevOps β˜οΈπŸš€

Part 8 of 8

Learn AWS from a DevOps Engineer's perspective. This series covers AWS fundamentals, IAM, EC2, VPC, S3, Route 53, Load Balancers, Auto Scaling, CloudWatch, ECS, EKS, CI/CD, Infrastructure as Code, Monitoring, Security, and real-world DevOps projects using AWS.

Start from the beginning

☁️ DevOps with AWS – Zero to Hero Journey Begins | Course Overview πŸš€

Master AWS Cloud Fundamentals, DevOps Practices, Infrastructure Automation, CI/CD Pipelines, Kubernetes & Production-Level Cloud Architecture Step-by-Step