Archive · DevOps

CloudFormation and the AWS CDK

The AWS-native route to infrastructure as code: templates, or real programming languages with CDK v2.

1 min read

  • AWS CloudFormation
  • AWS CDK v2
  • TypeScript

CloudFormation creates resources from a YAML template as a stack, with rollback on failure.

Resources:
  Assets:
    Type: AWS::S3::Bucket
    Properties:
      VersioningConfiguration: { Status: Enabled }
aws cloudformation deploy --template-file assets.yaml --stack-name assets

The AWS CDK (version 2 is the current line) lets you write the same thing in TypeScript, Python and other languages, then synthesises CloudFormation.

npm install -g aws-cdk
cdk init app --language typescript
cdk bootstrap aws://111122223333/ap-south-1
cdk diff
cdk deploy
new s3.Bucket(this, 'Assets', { versioned: true, encryption: s3.BucketEncryption.S3_MANAGED });

Choose CloudFormation or CDK if you are all-in on AWS; choose Terraform if you manage several providers. Review cdk diff or a change set before deploying, and use stack termination protection and DeletionPolicy: Retain for databases.