This is the second part of the serverless tutorial with aws Api Gateway + Lambda + DynamoDB + CDK. I am going to cover the services implementation. If you haven’t checked it yet, you can access the first part of this tutorial here. Let the fun part begin!
Infrastructure Definition
I am going to create a class that will serve as a template for a DynamoDB table creation under the services/infra/dynamo directory.
Here I have defined a TableProperties interface to pass to the class constructor as well as a SecondaryIndex class that defines a secondary index type for the table. In the DynamoTable constructor I am creating the table setting partitionKey and name from the tableProperties. I then proceed to add any secondary index specified in the properties. This provides me with a template for creating a DynamoDB table while specifying any secondary indexes. What I really like about using CDK is that I can write unit tests to make sure my template initialization works as expected.
Let’s run those tests:
The next class I want to create is for binding lambdas to the APIGateway. This class is going to help me keep my stack invocation class tidy and clear later on.
This bind method could be improved to better handle the possibility of the resource field being undefined, however I am going to keep it leaner for the purpose of this tutorial. Finally under the services/todo.item/infra folder create a todo.item.stack.ts file and pop in the following for now:
Implementing Lambdas
First things first, I am going to create under services/todo.item a file named todo.item.ts and use it to define a todo item type. This is the type of object that I will be storing into the DynamoDB table.
The object is simple, a todo item will have an id and a text field. Let’s go ahead and install uuid as a dependency so we can use it to populate the id field. We will also need to install the aws-sdk
Now to the lambdas part. Create create.ts and read.ts under the services/todo.item/lambdas folder. Let’s look at the create lambda first:
The lambda is pretty simple, it reads the text from the request body and use it to create a TodoItem that is then inserted into the DynamoDB table. I didn’t add any error handling to keep the code simple enough. Now let’s put together the stack and wire this lambda to an api gateway. Go back to the todo.item.stack.ts file:
Right after calling the parent constructor I am creating a RestApi a DynamoDB table and a CreateTodoItemLambda. I have to grant write permission to the lambda to make sure it can successfully write the todo item to the table. Finally I am using the api binder to create a todo resource on the RestApi and binding a POST method with the create todo lambda. If you remember during the setup we have created a cdk.json file from where we are invoking the services/infra/main.ts file. Let’s go back there and call the TodoItemStack:
Alright we are almost there, time to deploy!
Deploying the services
To deploy the lambda, make sure you have the aws-cdk npm package installed globally and then from your root folder run
$ cdk deploy
Once deployed you will see a url output to your terminal use that url to test your /todo post request:
You can navigate to the DyanmoDB service in your aws console to verify that the item has been stored:
That’s it! You can check out the repository to see the read lambda implementation and how it has been wired in the TodoItemStack class.