No-code serverless Rest-API with AWS and Dynamo DB

Anand Bhaskaran
5 min readDec 3, 2021

--

As a full-stack developer, in many cases, what we need is just a logical API mapping with the ORM. In this tutorial, we see step-by-step instructions on how to create such an API using AWS.

What are we going to build?

We are going to build a Jobs API that can fetch a list of open jobs for a company.

Our Database: We decided to use Amazon’s Dynamo DB as our database. We will run it in the serverless model and pay on demand (on a per-request basis)

What do you need?

  • An AWS account with access to the AWS UI

Let's get started

Step 1: Prepare DynamoDB Table

Here I am creating a new table called jobs. The primary key of the table is the employer_id + job_id and it should be always unique for every item in the table. Retrieving data using the primary key or just using partition Key is very efficient as DynamoDB automatically creates the hash of them internally.

Click on Create Table it to create a new DynamoDB table.

You can now create a couple of items on this table directly from the user interface.

Step 2: Create an IAM policy & role to Query this table

Copy the ARN code of the table

ARN of the table

Now navigate to IAM -> Policies -> Create Policy

Filll policy details. You can give restrictive access in the policy just to make sure that your data is produced. Plaste the ARN copied in the Resources.

Give it a nice name. I call it DynamoDBJobsTableReadAccessand click Create

Now lets create a IAM Role with this policy.

  • Navigate to IAM -> Roles -> New Role
  • Select API Gateway in the AWS Services
  • Leave the default policy ( AmazonAPIGatewayPushToCloudWatchLogs)as it is
  • Give a nice name. I named it as APIGatewayRoleForReadingDynamoDB
  • Now go to the created role and click Attach Policies ans select the previously created policy (DynamoDBJobsTableReadAccess in my case)

Step 3: Create a Rest API

  • Now navigate to API Gateway and select Create API
  • Select `Rest API` and click Build
  • On the new page, select Restas a protocol and New API
  • I call my API as JobService
  • Click Create API

Creating Resurces

What we need for our API is to fetch the list of open jobs stored in DynamoDB for the specific employer using the following URL

GET /{employer-id}/jobs

Follow the steps to create such a URL

  • From Actions select New Resource and name the resource. I name it employer-id
  • In the resource path type {employer-id} to pass the employer id as a parameter. We can access this later by `$input.params(‘employer-i’)`
  • To create a sub-resource, select the {employer-id} on the left and create a New Resource from Actions
  • In the Resource Path type jobs to

Creating API(s)

  • Select the newly created sub-resource and select Create Method from Actions menu
  • Select GET in the options and click on the tick mark
  • Fill out the options as shown below and click Save

With the above configuration, whenever we get a GET request on the specified URL ( /{employer-id}/jobs ) we send a POST request to the DynamoDB and passthrough the response from the DynamoDB as the response to the GET Request.

Data Transformation

For the above URL to work we need to transform the POST request such that the DynamoDB can process it as a Query. Follow the steps below to transform the data.

  • Click on Integration Request and expand Mapping Templates
  • Select When there are no templates defined (recomended) in the Request Body Passthrough
  • Click on Add mapping template
  • Type application/json in the Content-Type and click on the check mark
  • In the template type in the following JSON
{
"TableName":"jobs",
"KeyConditionExpression":"#kn0 = :kv0",
"ExpressionAttributeNames":{
"#kn0":"employer_id"
},
"ExpressionAttributeValues":{
":kv0":{"N":"$input.params('employer-id')"}
}
}

Basically this is the POST request’s body you need to make when querying the DynamoDB. While the JSON is self explanatory. What it means is that we request DynamoDB to return all the jobs from the jobs table that matches the attribute name employer_id to the attribute value passed in the {employer-id}

  • Click on Save

Testing

  • Now go back and click Test
  • Type the {employer-id} field and press Test
  • BOOM. You see the list of jobs matching this criteria

Deploy the API

Now that everything is good we can deploy this API:

  • Click Actions -> Deploy API
  • Name the stage. I directly call it as production
  • Now grab the invoke URL append it with /{employer-id}/jobs and you should the response directly on the response
  • You can also export the API in postman and test it

--

--

Anand Bhaskaran
Anand Bhaskaran

Written by Anand Bhaskaran

I am a software engineer and a investment enthusiast

Responses (1)