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'
ConceptDescription
StackA deployed instance of a template — create, update, or delete as a unit
Change SetPreview changes to a stack before applying them
Stack SetDeploy the same stack across multiple accounts and regions
Drift DetectionDetect manual changes to resources not reflected in the template
Nested StacksReference other CloudFormation stacks for modular design
Outputs & ExportsShare values between stacks
!RefReference a parameter or resource logical ID
!SubString substitution with variable interpolation
!GetAttGet 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.

ConceptDescription
AppRoot of the CDK application
StackCloudFormation stack — unit of deployment
ConstructA reusable cloud component (L1, L2, or L3)
L1 ConstructsDirect CloudFormation resource wrappers (CfnBucket)
L2 ConstructsHigher-level, opinionated abstractions (Bucket, Function)
L3 ConstructsPatterns — 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

FeatureCloudFormationCDKTerraform
LanguageYAML / JSONTypeScript, Python, Java, C#, GoHCL
Learning curveMediumLow (if you know the language)Medium
AWS-nativeYesYes (compiles to CFN)No (multi-cloud)
State managementCloudFormation stack stateCloudFormation stack stateTerraform state file
Multi-cloudNoNoYes
CommunityAWS-focusedAWS-focusedMassive, multi-cloud
IDEs / autocompleteLimitedExcellent (TypeScript)Good
Best forSimple stacks, platform teamsComplex apps, developer-friendlyMulti-cloud, Terraform shops

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