Build a Serverless Web Application Part 3— Lambda & DynamoDB

Yst@IT
5 min readNov 28, 2019
AWS Build a Serverless Web Application

This is a walk through lab based on AWS project. For more details please refer here. This walk through breaks into four parts which are

  1. Host a static website
  2. Manage users
  3. Build serverless backend
  4. Deploy a RESTful API

Build serverless backend

In part 3, we will setup AWS Lambda and Amazon DynamoDB to handle requests from frontend.

How it works:

User can request an unicorn from the frontend we deploy in Part 1. Request will trigger Amazon API Gateway which will implement in Part 4. Amazon API Gateway will trigger Lambda, which we will be building in this part, sending an unicorn to the location chosen by the user on the map and record such request in DynamoDB, which we will be setting it up in this part too.

Let’s start by building DynamoDB table.

AWS Build a Serverless Web Application

Give your table a name, assign a partition key, it can be anything, use default setting for this lab and create it.

AWS Build a Serverless Web Application

Finish view of DynamoDB scrolled to the bottom. You can change window view from red box on the right side. Copy the ARN at the bottom, we will need it later.

AWS Build a Serverless Web Application

Create Role

Next, let’s create the Lambda function. First we need to create a role for Lambda so that it has permission to write logs to CloudWatch Logs. First we go to IAM and create a role.

AWS Build a Serverless Web Application

Since the role is meant for Lambda, so we choose Lambda.

AWS Build a Serverless Web Application

Key in “basic” to search for AWS managed policy AWSLambdaBasicExecutionRole, which has permissions to write logs to CloudWatch Logs, click Next: Tags

AWS Build a Serverless Web Application

Tags are optional so I will skip it. Last page is review, give your role a name and click “Create role”.

AWS Build a Serverless Web Application

Next, since Lambda function needs to put records into DynamoDB, so we must assign appropriate permission to this role too. Once role is attach to Lambda, Lambda will then have all the privileges assigned to the role. From role list, find the role created in previous step.

AWS Build a Serverless Web Application

Click “Add inline policy”

AWS Build a Serverless Web Application

Click on “Choose a service”, key in “dy”, click DynamoDB.

AWS Build a Serverless Web Application

Input “Put” for Filter actions, tick PutItem, click the arrow at step 3.

AWS Build a Serverless Web Application

Click Add ARN an paste the ARN copied when creating DynamoDB.

AWS Build a Serverless Web Application

Pasted DynamoDB ARN and click Add.

AWS Build a Serverless Web Application

Lastly click Review policy at the right bottom corner.

AWS Build a Serverless Web Application

Give your policy a name and click Create policy.

AWS Build a Serverless Web Application

Finish view.

AWS Build a Serverless Web Application

Create Lambda Function

Now we need to create a Lambda function which will be triggered by API Gateway, dispatches unicorn to the choice of user on the map and records the event in the DynamoDB.

AWS Build a Serverless Web Application

Choose Author from scratch.

AWS Build a Serverless Web Application

Named the function, pick Node.js as runtime since the code provided by AWS is Node.js, apply the role we created in the previous step to this Lambda function so it has permission to write logs to CloudWatch Logs and records to DynamoDB.

AWS Build a Serverless Web Application

Copy the code provided by AWS, paste it to Lambda Function code area and save it.

AWS Build a Serverless Web Application

Lastly, let’s check if Lambda function is working as expected. Click and select “Configure test events”.

AWS Build a Serverless Web Application

Name whatever you like for Event name, remove everything in the code box and paste code provided by AWS, click “Create”

{
"path": "/ride",
"httpMethod": "POST",
"headers": {
"Accept": "*/*",
"Authorization": "eyJraWQiOiJLTzRVMWZs",
"content-type": "application/json; charset=UTF-8"
},
"queryStringParameters": null,
"pathParameters": null,
"requestContext": {
"authorizer": {
"claims": {
"cognito:username": "the_username"
}
}
},
"body": "{\"PickupLocation\":{\"Latitude\":47.6174755835663,\"Longitude\":-122.28837066650185}}"
}
AWS Build a Serverless Web Application

Click “Test” to verify if everything is setup correctly. You should see code 201 if everything is correct.

AWS Build a Serverless Web Application

If you see error like this

AWS Build a Serverless Web Application

Make sure in your function code, you have the same name used when creating partition key for your DynamoDB.

AWS Build a Serverless Web Application

You can check your partition key here.

AWS Build a Serverless Web Application

Don’t forget to click “Save” if you made any changes to the code! Then test the code again!

AWS Build a Serverless Web Application

If everything works correctly, we can also verify that test data has been put into DynamoDB from Items tag.

AWS Build a Serverless Web Application

Reference

Module 3: Serverless Service Backend

--

--

Yst@IT

Cloud Solution Architect, focusing on Oracle Cloud Infrastructure currently.