Creating a REST API using Serverless Framework in TypeScript [Part 2]

Aaditya Diwan
Towards AWS
Published in
4 min readMay 26, 2021

--

Photo by Michael on Unsplash

In the first part of this series, we did a simple REST API endpoint for creating a blog post. We didn’t save this data into the database and just sent that as a response. In this part, we’ll connect our backend to the database and save the blog post onto it.

We’re going to use DynamoDB as our database. Before jumping right into coding, I’d like to say a few things about the Database since I feel that it can be daunting for people who come from an RDBMS background to jump into a non-relational database like DynamoDB.

In DynamoDB we can only query data based on the Primary Key. A primary key in DynamoDb can be made up of either the partitioning key or the combination of partition and sort key.

Sounds weird, doesn’t it? But that’s how things work in dynamo land. And since we don’t want ourselves stuck while in the middle of developing our app, it’s better to have at least a basic understanding of how DynamoDB works.

I’ll share some resources in the end that you can checkout

Coming back to DynamoDB,

Since the primary key is unique, we have to choose the partition-key and sort-key in such a way that the combination results in a unique record.

But what if we need to query data based on some other attributes? Sure we can scan the entire data based on some filters, but is not recommended because of how slow it works. So what can we do? Secondary Indexes to Rescue. So I’ll refrain from talking about secondary indexes since we won’t need them right now. But I recommend you go and read something about them after reading this article.

Now that we know the basics of DynamoDB, Let’s get back to the development process.

In this last article, I told you to set up the AWS CLI on your pc, we’ll use it in this article.

Go to the serverless.ts file and make the following changes

We’re basically adding DynamoDB as a resource and giving the IAM policies that the table can do. In our case, since we’re just writing data to the table, we’ll just give “putItem”. We’re taking author as our partitioning key and createdAt as our sort key. Make sure you add your AWS ACCOUNT ID on line 34.

Now let’s edit the handler.ts in postBlog,

blogParams are parameters to DynamoDB. It mentions the Items and the table name. We pass these parameters to the DynamoDB’s put().

Now that we have made all the changes, let’s try and deploy the backend to AWS.

Type the following command in your command line

sls deploy

If everything goes well, you’ll see the following output

The output of SLS deploy

You can see in the endpoints there are 2 endpoints that relate to the hello function and the postBlog function respectively. We have successfully deployed our backed API on AWS. Nicely Done!

We can also see that a table is created in our AWS account named blog-table.

Sending post request to the API endpoint

Now if we hit the endpoint and give the appropriate JSON body, we’ll be able to see the item in our table. So let’s do just that in Postman.

Item successfully added in DynamoDB

Now that we’ve sent our request let’s head over to the AWS console and check whether an item is really added to the database. As we can see, the Item is successfully added in our DyanmoDB Table.

That’s it for this article, In the next one, we’ll create a REST API endpoint for retrieving items. Hope you had fun doing this.

Let’s meet at the next one!

--

--