Introduction

Infrastructure as code(IaC) is a popular term in cloud environment. I have been using CloudFormation to automate AWS infrastructure deployment. However I always had hard time writing out the configuration file since they are written in string json or yaml file. There are some best practice for writing out CloudFormation out there, I always have to validate and revalidate the stacks and had to double check whether the infrastructure was deployed as I intended to. Recently I have stumbled upon AWS CDK and I fell in love with it. Here are some plus using CDK.

In short, AWS CDK provides high level abstraction of CloudFormation json/yaml syntax. In this blog post, I will rewrite my previous ecs-sample architecture(beta stage) in CDK. Please refer to my previous blog post here for infrastructure that I will be deploying with CDK.

kokospapa8/ecs-fargate-sample-app

cloudformation file

Before diving into writing CDK code, let's take a quick look at CDK.

CDK Concepts

There are three main components in CDK: contruct, stack and app. Everything in the AWS CDK is a construct. You can think of constructs as cloud components that can represent architectures of any complexity: a single resource, such as an S3 bucket or an SNS topic, a static website, or even a complex, multi-stack application that spans multiple AWS accounts and regions. To foster reusability, constructs can include other constructs. You compose constructs together into stacks, that you can deploy into an AWS environment, and apps, a collection of one of more stacks.

App

Written in TypeScript, JavaScript, Python, Java, or C# that uses the AWS CDK to define AWS infrastructure

Stack

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/5eac8140-0158-4e7b-aa32-ea67de4ad076/Untitled.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/9f2f2fc2-da6a-4342-a236-09fad1f0f110/Untitled.png

Constructs