Guide to the Cloud Resume Challenge — Part 3: DynamoDB, API gateway, and Lambda functions

Shishir Khandelwal
4 min readOct 18, 2021

--

This article series is a walkthrough of the steps & methods I used to create a cloud resume on AWS.

I firmly believe that learning Cloud and DevOps by doing projects is perhaps the best approach to gain meaningful skills. The goal of this article is to help anyone who is trying to complete the Cloud Resume Challenge.

Check out my cloud resume here!

In Part 1 of the guide, we had gone through the setup of S3 & Cloudfront.

In Part 2 - Domain name, Route 53 & Certificate Manager were discussed.

In this article, we will see the setup of the DynamoDB, API gateway & Lambda functions

We will be creating a visitor's count functionality using these services.

The no. of visitors would be updated every time a person opens our website. This no. will be stored and updated inside a DynamoDB table and will be updated & fetched using APIs calling Lambda functions.

Let’s start by creating a DynamoDb instance.

  • Open the DynamoDB service UI page and create a table.
    Our use case requires a very basic setup — just give a name and add a partition key.
  • We need to add a record to this table. I’ll be adding a field called “record_count” which will be the visitor count store.
    Initialize this value as any no. of your choice, I am going with 1.

That’s all the requirements from DynamoDb. Let’s start creating the Lambda function now.

We will need to create two Lambda functions —

First to fetch the present visitor count from Dynamodb

Second to update the visitor count in DynamoDb records.

First Lambda function-

I’ll be walking through my python code — although any language can be used.

  • Create a Lambda function and add your code to it.

The below code uses boto3 library to make requests to DynamoDB. It’s fetching the record with key ‘record_id’ as 0 and then returning the ‘record_count’ field value of the record.

import json
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('sample_table')
def lambda_handler(event, context):
response = table.get_item(Key={
'record_id':'0'
})
return response['Item']['record_count']
  • IAM roles created by Lambda don’t have permissions to access DynamoDb, so permissions for DynamoDB access need to be added manually.

Second Lambda function-

  • Create a lambda function -

The below code uses boto3 library to make requests to DynamoDB. It’s fetching the record with key ‘record_id’ equal to 0 and then adding 1 to the ‘record_count’ value of the record. It’s sending this updated ‘record_count’ as a response.

import json
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('sample_table')
def lambda_handler(event, context):
response = table.get_item(Key={
'record_id':'0'
})
record_count = response['Item']['record_count']
record_count = record_count + 1
print(record_count)
response = table.put_item(Item={
'record_id':'0',
'record_count': record_count
})

return "Records added successfully!"
  • This function also requires access to DynamoDb.

Now, let’s start working on API Gateway. APIs would be used to call the above Lambda functions from the website.

API for fetching the current record counts-

  • Open API gateway and create a REST API.
  • The default settings are fine.

I am going to name the API as ‘records’.

  • Create a GET method API — and add the lambda function here.
    My first lambda function had been named ‘record-get’.
  • Create it and then test. (In case you face a problem, check your lambda functions permissions and code!)

API for updating the current record counts-

  • My second lambda function has been named ‘add-record’.
  • Create and test it.
  • Deploy the API to create an endpoint. We will use this endpoint on our website at a later stage.

Congratulations! Our website’s infrastructure components are now ready for use.

In the next part, we are going to see -
- HTML and JS code to use the APIs.
- Creation of CICD using Github Actions.

The next parts are coming soon.

If you found this article helpful — consider following me here. I talk about Cloud & DevOps tools along with sharing a lot of tips and tutorials.

--

--

Shishir Khandelwal

I spend my day learning AWS, Kubernetes & Cloud Native tools. Nights on LinkedIn & Medium. Work: Engineering @ PayPal.