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
- Host a static website
- Manage users
- Build serverless backend
- 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.
Give your table a name, assign a partition key, it can be anything, use default setting for this lab and create it.
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.
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.
Since the role is meant for Lambda, so we choose Lambda.
Key in “basic” to search for AWS managed policy AWSLambdaBasicExecutionRole, which has permissions to write logs to CloudWatch Logs, click Next: Tags
Tags are optional so I will skip it. Last page is review, give your role a name and click “Create role”.
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.
Click “Add inline policy”
Click on “Choose a service”, key in “dy”, click DynamoDB.
Input “Put” for Filter actions, tick PutItem, click the arrow at step 3.
Click Add ARN an paste the ARN copied when creating DynamoDB.
Pasted DynamoDB ARN and click Add.
Lastly click Review policy at the right bottom corner.
Give your policy a name and click Create policy.
Finish view.
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.
Choose Author from scratch.
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.
Copy the code provided by AWS, paste it to Lambda Function code area and save it.
Lastly, let’s check if Lambda function is working as expected. Click and select “Configure test events”.
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}}"
}
Click “Test” to verify if everything is setup correctly. You should see code 201 if everything is correct.
If you see error like this
Make sure in your function code, you have the same name used when creating partition key for your DynamoDB.
You can check your partition key here.
Don’t forget to click “Save” if you made any changes to the code! Then test the code again!
If everything works correctly, we can also verify that test data has been put into DynamoDB from Items tag.