The AWS CDK enables us to define application infrastructure using a programming language instead of markup, which is then transformed by the CDK to CloudFormation templates for the management of cloud infrustructure services
The CDK supports TypeScript, JavaScript, Python, Java, and C#
Prerequisites
AWS Lamda development requires SAM to be installed, depending on your OS you can use the installation instructions here
I’m using aws-sam-cli@1.12.0 to avoid certain compat issues from the current version
And lastly, you will need to install cdk
Init Project
To initialize a new project using SAM and CDK run the following command:
This will generate the following file structure:
In the generated files we can see the bin/my-project.ts file which creates an instance of the Stack that we expose from lib/my-project-stack.ts
bin/my-project.ts
Create a Handler
Next, we can create a handler for our file, we’ll use the Typescript handler but the concept applies to any handler we may want to use
First, we’ll export a handler function from our code, I’ve named this handler but this can be anything and we will configure CDK as to what function to look for. We’ll do this in the lambdas/hello.ts file as seen below. Note the use of the APIGatewayProxyHandler type imported from aws-lambda, this helps inform us if our event and return types are what AWS expects
lambdas/hello.ts
Define Stack
Next, in order to define our application stack we will need to use CDK, we can do this in the lib/my-project-stack.ts file utilizing @aws-cdk/aws-lambda-nodejs to define our Nodejs handler:
lib/my-project-stack.ts
If we want, we can alternatively use the lower-level cdk.Function class to define the handler like so:
Note, avoid running the above command using npm run sdk ... as it will lead to the template.yaml file including the npm log which is not what we want
Create API
Next, we need to add our created lambda to an API Gateway instance so that we can route traffic to it, we can do this using the @aws-cdk/aws-apigateway package
To setup the API we use something like this in the Stack:
So our Stack now looks something like this:
lib/my-project-stack.ts
Generate Template
Now that we have some API up, we can look at the process for making it requestable. The first step in the process for running this locally is generating a template.yaml file which the sam CLI will look for in order to setup the stack
We can build a Cloud Formation template using the cdk synth command:
You can take a look at the generated file to see the CloudFormation config that CDK has generated, note that creating the template this way is only required for local sam testing and isn’t the way this would be done during an actual deployment kind of level
Run the Application
Once we’ve got the template.yaml file it’s just a matter of using sam to run our API. To start our API Gateway application locally we can do the following:
This will allow you to make requests to the lambda at http://localhost:3000. A GET request to the above URL should result in the following:
Use a DevContainer
I’ve also written a Dev container Docker setup file for use with CDK and SAM, It’s based on the Remote Containers: Add Development Container Configuration Files > Docker from Docker and has the following config:
Dockerfile
.devcontainer/devcontainer.json
Especially note the workspaceMount and `workspaceFolderz sections as these ensure the directory structure maps correctly between your local folder structure and container volume so that the CDK and SAM builds are able to find and create their assets in the correct locations