What is AWS CloudFormation and infrastructure as code?
Learn what AWS CloudFormation is and how infrastructure as code provisions repeatable AWS environments from declarative templates with change sets and rollback.
Expected Interview Answer
AWS CloudFormation is a service that lets you define and provision AWS infrastructure using declarative templates, an implementation of infrastructure as code (IaC) where you describe the desired state and CloudFormation creates, updates, and deletes resources to match it.
You write a template in YAML or JSON listing resources, parameters, and outputs, then deploy it as a stack. CloudFormation manages the dependency order, tracks state, and applies changes safely with change sets and automatic rollback on failure. Because the template is version-controlled code, infrastructure becomes repeatable, reviewable, and consistent across environments, eliminating manual, error-prone console clicking.
- Repeatable, consistent environments from one template
- Version control and peer review for infrastructure
- Automatic dependency ordering and rollback on failure
- Change sets preview updates before they apply
- Easy teardown and recreation of whole stacks
AI Mentor Explanation
CloudFormation is like a captain's written field-placement chart: instead of pointing each fielder into position by hand every over, you hand over one master plan and the whole formation is arranged automatically, identically, in every match. Change the chart and the field rearranges; tear it up and everyone leaves the pitch cleanly.
Step-by-Step Explanation
Step 1
Write the template
Declare resources, parameters, mappings, and outputs in a YAML or JSON template describing the desired state.
Step 2
Validate
Run aws cloudformation validate-template to catch syntax errors before deploying.
Step 3
Create a change set
Preview exactly what will be added, modified, or deleted before applying it to a live stack.
Step 4
Deploy the stack
CloudFormation provisions resources in dependency order, tracking each in the stack's state.
Step 5
Update or delete
Edit the template to update the stack, or delete the stack to remove all its resources cleanly.
What Interviewer Expects
- Definition of infrastructure as code and declarative provisioning
- Understanding of templates, stacks, and change sets
- Awareness of dependency ordering and rollback
- Benefits of version control and repeatability
- Comparison with manual console provisioning
Common Mistakes
- Describing CloudFormation as imperative scripting rather than declarative
- Ignoring change sets and updating stacks blindly
- Forgetting rollback behavior on failed stack operations
- Confusing a template with a running stack
- Not version-controlling templates
Best Answer (HR Friendly)
“CloudFormation lets you describe all your AWS infrastructure in a text file and have AWS build it for you automatically. Because it is code, you can review it, reuse it, and recreate the exact same environment every time instead of clicking through the console by hand.”
Code Example
AWSTemplateFormatVersion: '2010-09-09'
Description: Provision a versioned S3 bucket
Parameters:
BucketName:
Type: String
Description: Globally unique bucket name
Resources:
AppBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref BucketName
VersioningConfiguration:
Status: Enabled
Outputs:
BucketArn:
Description: ARN of the created bucket
Value: !GetAtt AppBucket.Arnaws cloudformation deploy \
--template-file bucket.yaml \
--stack-name app-storage \
--parameter-overrides BucketName=my-unique-app-bucket-2026Follow-up Questions
- What is a change set and why preview changes before applying?
- How does CloudFormation handle rollback when a resource fails to create?
- What is drift detection in CloudFormation?
- How do nested stacks help manage large infrastructures?
- How does CloudFormation compare with Terraform?
MCQ Practice
1. CloudFormation templates describe infrastructure in what style?
Templates are declarative: you describe the desired end state and CloudFormation figures out how to reach it.
2. What does a change set do?
A change set shows exactly which resources will be added, modified, or removed before you execute the update.
3. What happens by default if a stack creation fails partway?
By default CloudFormation rolls back on failure, deleting the resources it created to leave a clean state.
Flash Cards
What is CloudFormation? — An AWS IaC service that provisions infrastructure from declarative YAML/JSON templates deployed as stacks.
What is a stack? — A deployed instance of a template — a collection of AWS resources managed together as one unit.
What is a change set? — A preview of the additions, modifications, and deletions an update would make before you apply it.
Default behavior on failed create? — Automatic rollback — CloudFormation deletes the resources it created.