A First Look at AWS CDK
Published:
AWS Cloud Development Kit (CDK) is a framework for defining cloud infrastructure in code and provisioning it through AWS CloudFormation.
Outline
All of this project’s code can be found in the First Look monorepo on my GitHub.
Introduction
AWS Cloud Development Kit (CDK) is a software development framework for defining cloud infrastructure in code and provisioning it through AWS CloudFormation. It supports TypeScript, JavaScript, Python, Java, C#/.Net, and (almost) Go.
Developers can use one of the supported programming languages to define reusable cloud components known as Constructs that are composed together into Stacks and Apps.
Setup
Configure AWS CLI
Make sure you have the AWS CLI installed and an AWS account. For general use, aws configure
is recommended as the fastest way to set up your AWS CLI installation.
When you enter this command, the AWS CLI prompts you for four pieces of information:
- Access key ID
- Secret access key
- AWS Region
- Output format
Go to My Security Credentials to find your Access Key ID, Secret Access Key, and default region. You can leave the output format blank.
Install CDK CLI
The aws-cdk
can be globally installed with npm
.
Check aws-cdk
version.
Output:
Create Project Directory
Files will be generated based on the project name. This means you can’t give your project a cute, personal name like ajcwebdev-cdk
which I always do in all my tutorials but okay fine AWS I’ll play by your rules.
Initialize Project
Output:
List Stacks
Output:
Project Structure
With one exception, our package.json
should be straight forward if you have worked with npm
before. It includes a few scripts and our dependencies.
It also includes a less well known (at least to me) option, the bin
field. Some packages have one or more executable files that need to be installed into the PATH. The bin
field is a map of a command name to a local file name. On install, npm
will symlink that file into prefix/bin
for global installs, or ./node_modules/.bin/
for local installs.
CDK Configuration
Many features of the CDK Toolkit require one or more AWS CloudFormation templates be synthesized, which in turn requires running your application. cdk.json
uses a configuration option to specify the exact command necessary to run your app and is located in the main directory of your project.
Your configuration option can be specified using the app
key. The CDK Toolkit provides an appropriate command when creating a new project with cdk init
. The CDK Toolkit looks for cdk.json
in the current working directory when attempting to run your app.
App Entry Point
Files referenced in bin
must start with #!/usr/bin/env node
to make sure the scripts aren’t started without the node executable.
HelloCdkStack
The code that defines your stack goes inside the constructor
, under super
.
Add S3 Bucket
Right now the app doesn’t do anything since the stack it contains doesn’t define any resources. We can add an Amazon S3 bucket by installing the aws-cdk/aws-s3 module from the AWS Construct Library.
Define S3 Bucket Construct
Inside your stack, initialize an s3
variable by importing it from @aws-cdk/aws-s3
with the CommonJS require()
syntax. Create MyFirstBucket
with the Bucket class.
Our dependencies in package.json
now includes @aws-cdk/aws-s3
.
Generate CloudFormation Template
Synthesize an AWS CloudFormation template for the app. Hey put that Moog away, not that kind of synth!
This will output a CloudFormation file that will be mostly gibberish if you’ve never seen a CloudFormation template before.
You’ll eventually want to learn what all this junk means and how it works. But there’s a reason so many libraries exist to abstract CloudFormation away or replace it (including Pulumi, SAM, Terraform, and SST).
The reason so many exist is because it’s a huge pain and no one wants to write it. We now treat it moreso as a compile target because compilers are the new frameworks.
Deploy Stack to AWS
We will use the AWS CDK Toolkit to deploy our project. However, since cdk synth
generates valid AWS CloudFormation templates we could take it and deploy it using the AWS CloudFormation console or other tools.
Output with account-id
and resource-id
redacted:
We’ll figure out what an ARN is/does in future articles, but basically it’s an AWS NFT.
A non-fungible token is a unit of data that certifies a digital asset to be unique and therefore not interchangeable.