Modern AWS infrastructure is expected to be reproducible, version-controlled,
reviewable, and deployable through automation. AWS CloudFormation sits at the
center of that operational model.
A CloudFormation stack is the deployed instance of an infrastructure template.
The template defines the desired state of AWS resources, while the stack
represents the actual running infrastructure created from that definition.
Core Concept:
The template is the blueprint. The stack is the deployed infrastructure.
Understanding What Gets Deployed
Every deployment begins with a CloudFormation template. That template may be:
- Written directly in YAML or JSON
- Generated by AWS CDK
- Expanded through AWS SAM transforms
- Produced through automation pipelines
| Tool | What You Write | What CloudFormation Receives |
|---|---|---|
| AWS CDK | TypeScript / Python / Java | Synthesized CloudFormation Template |
| CloudFormation | YAML / JSON | Same Template |
| AWS SAM | Serverless Template | Expanded CloudFormation Template |
Deploying Infrastructure Using AWS CDK
AWS CDK allows infrastructure to be written using real programming languages
instead of verbose YAML templates.
Step 1: Initialize the CDK Application
npm install -g aws-cdk
mkdir ecs-demo
cd ecs-demo
cdk init app --language typescript
Step 2: Define the Stack
import * as cdk from 'aws-cdk-lib';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as ecs_patterns from 'aws-cdk-lib/aws-ecs-patterns';
export class EcsDemoStack extends cdk.Stack {
constructor(scope, id) {
super(scope, id);
const vpc = new ec2.Vpc(this, 'AppVpc');
const cluster = new ecs.Cluster(this, 'AppCluster', {
vpc
});
new ecs_patterns.ApplicationLoadBalancedFargateService(
this,
'AppService',
{
cluster,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('nginx')
}
}
);
}
}
Step 3: Bootstrap the Environment
cdk bootstrap
This creates the bootstrap stack:
CDKToolkit
Step 4: Synthesize the Template
cdk synth
CDK converts application constructs into a full CloudFormation template.
Step 5: Deploy the Stack
cdk deploy
What Happens During Deployment
- CloudFormation validates the template
- Dependencies between resources are resolved
- Resources are provisioned in parallel
- Stack events stream continuously
- Rollback occurs automatically on failure
Monitoring Stack Events
aws cloudformation describe-stack-events \
--stack-name EcsDemoStack
Rollback Behavior
If resource creation fails midway, CloudFormation automatically rolls back
the deployment.
For debugging large deployments:
cdk deploy --no-rollback
Deploying Using the AWS CLI
Create a Stack
aws cloudformation create-stack \
--stack-name networking-stack \
--template-body file://vpc.yaml \
--capabilities CAPABILITY_IAM
Update a Stack
aws cloudformation update-stack \
--stack-name networking-stack \
--template-body file://vpc.yaml
Delete a Stack
aws cloudformation delete-stack \
--stack-name networking-stack
Deploying Through CI/CD Pipelines
Using Change Sets
aws cloudformation create-change-set \
--stack-name production-stack \
--change-set-name v2-update \
--template-body file://template.yaml
Change sets preview infrastructure modifications before execution.
Drift Detection
aws cloudformation detect-stack-drift \
--stack-name production-stack
Drift detection identifies infrastructure modified outside CloudFormation.
Termination Protection
aws cloudformation update-termination-protection \
--stack-name production-stack \
--enable-termination-protection
Termination protection prevents accidental deletion of production
infrastructure.
Final Thoughts
CloudFormation deployment is fundamentally about managing infrastructure as a
deterministic and reproducible system.
Whether infrastructure is deployed through:
- AWS CDK
- Raw CloudFormation
- AWS SAM
- AWS CLI
- CI/CD Pipelines
the underlying CloudFormation engine remains the same.
Understanding dependency resolution, rollback handling, resource
stabilization, and stack lifecycle management is what separates
basic AWS usage from production-grade infrastructure engineering.
Leave a Reply