Skip to content

AWS CloudFormation & CDK — Infrastructure as Code

AWS CloudFormation & CDK — Infrastructure as Code

Section titled “AWS CloudFormation & CDK — Infrastructure as Code”

AWS offers two native IaC tools: CloudFormation (declarative YAML/JSON templates) and the AWS CDK (define infrastructure in your programming language of choice).

In Azure terms: CloudFormation ≈ ARM Templates, CDK ≈ Bicep / Pulumi


CloudFormation lets you model your entire AWS infrastructure as a template and deploy it as a stack.

AWSTemplateFormatVersion: '2010-09-09'
Description: 'Simple web app stack'
Parameters:
EnvironmentName:
Type: String
Default: dev
AllowedValues: [dev, staging, prod]
Resources:
# S3 Bucket
AppBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub 'my-app-${EnvironmentName}-${AWS::AccountId}'
VersioningConfiguration:
Status: Enabled
# EC2 Instance
WebServer:
Type: AWS::EC2::Instance
Properties:
InstanceType: t3.micro
ImageId: ami-0abcdef1234567890
Tags:
- Key: Environment
Value: !Ref EnvironmentName
Outputs:
BucketName:
Value: !Ref AppBucket
Export:
Name: !Sub '${AWS::StackName}-BucketName'
Concept Description
Stack A deployed instance of a template — create, update, or delete as a unit
Change Set Preview changes to a stack before applying them
Stack Set Deploy the same stack across multiple accounts and regions
Drift Detection Detect manual changes to resources not reflected in the template
Nested Stacks Reference other CloudFormation stacks for modular design
Outputs & Exports Share values between stacks
!Ref Reference a parameter or resource logical ID
!Sub String substitution with variable interpolation
!GetAtt Get an attribute of a resource (e.g., bucket ARN)
Terminal window
# Create a stack
aws cloudformation create-stack \
--stack-name my-stack \
--template-body file://template.yaml \
--parameters ParameterKey=EnvironmentName,ParameterValue=dev \
--capabilities CAPABILITY_IAM
# Update a stack
aws cloudformation update-stack \
--stack-name my-stack \
--template-body file://template.yaml
# Create a change set (preview changes)
aws cloudformation create-change-set \
--stack-name my-stack \
--change-set-name my-changes \
--template-body file://template.yaml
# Delete a stack
aws cloudformation delete-stack --stack-name my-stack
# Describe stack resources
aws cloudformation describe-stack-resources --stack-name my-stack

The AWS CDK lets you define infrastructure using TypeScript, Python, Java, C#, or Go — with the full power of a programming language (loops, conditionals, abstractions).

CDK synthesizes your code into a CloudFormation template and then deploys it.

Concept Description
App Root of the CDK application
Stack CloudFormation stack — unit of deployment
Construct A reusable cloud component (L1, L2, or L3)
L1 Constructs Direct CloudFormation resource wrappers (CfnBucket)
L2 Constructs Higher-level, opinionated abstractions (Bucket, Function)
L3 Constructs Patterns — multiple L2s pre-wired together
import * as cdk from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as s3n from 'aws-cdk-lib/aws-s3-notifications';
export class MyStack extends cdk.Stack {
constructor(scope: cdk.App, id: string) {
super(scope, id);
// S3 bucket
const bucket = new s3.Bucket(this, 'MyBucket', {
versioned: true,
encryption: s3.BucketEncryption.S3_MANAGED,
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
// Lambda function triggered by S3 uploads
const fn = new lambda.Function(this, 'ProcessUpload', {
runtime: lambda.Runtime.PYTHON_3_12,
code: lambda.Code.fromAsset('lambda'),
handler: 'handler.main',
environment: {
BUCKET_NAME: bucket.bucketName,
},
});
// Grant Lambda read access to the bucket
bucket.grantRead(fn);
// Trigger Lambda on S3 object creation
bucket.addEventNotification(
s3.EventType.OBJECT_CREATED,
new s3n.LambdaDestination(fn)
);
}
}
Terminal window
# Install CDK
npm install -g aws-cdk
# Initialize a new CDK project (TypeScript)
cdk init app --language typescript
# Synthesize CloudFormation template
cdk synth
# Compare deployed stack with local code
cdk diff
# Deploy
cdk deploy
# Destroy the stack
cdk destroy

Feature CloudFormation CDK Terraform
Language YAML / JSON TypeScript, Python, Java, C#, Go HCL
Learning curve Medium Low (if you know the language) Medium
AWS-native Yes Yes (compiles to CFN) No (multi-cloud)
State management CloudFormation stack state CloudFormation stack state Terraform state file
Multi-cloud No No Yes
Community AWS-focused AWS-focused Massive, multi-cloud
IDEs / autocomplete Limited Excellent (TypeScript) Good
Best for Simple stacks, platform teams Complex apps, developer-friendly Multi-cloud, Terraform shops

Recommendation for Azure devs: CDK with TypeScript feels most like Bicep or Pulumi — familiar code-based approach rather than pure markup.