Lambda — API Gateway — DynamoDB

Sharmila S
featurepreneur
Published in
10 min readAug 19, 2021

--

Let’s learn to use these services with a use case — create an API to add a new entry to the database using AWS Services — Lambda, API Gateway and DynamoDB.

What we are going to create?

  • An Amazon DynamoDB table called Book to store book details.
  • An API using API Gateway to add a new book record to the table created.
  • A Lambda function that handles the request from API and adds the details to the DynamoDB Table.

Resources Used:

  • Amazon API Gateway —a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale.
  • AWS Lambda — a serverless compute service by AWS.
  • AWS IAM — enables you to manage access to AWS services and resources securely.
  • Amazon DynamoDB — NoSQL Database service by AWS.
  • Postman (to test our API)

Let’s have a look at the steps:

  1. DynamoDB: Create a table in DynamoDB.
  2. Lambda: Create a Lambda function with necessary permissions.
  3. Lambda: Write the code to add data to the Table (in python) and Test it.
  4. API Gateway: Create an API with a POST method.
  5. API Gateway: Integrate the API with the Lambda function created and configure query parameters (for passing the data to function).
  6. API Gateway: Deploy the API to a new Stage.
  7. Lambda: Modify the lambda function to accept the data sent through the API request from API Gateway.
  8. Postman: Test the API.

Step 1: Create a new table in DynamoDB

  • Go to DynamoDB service from AWS Console, then click ‘Create Table’.
  • Provide a name for the table, along with the partition key attribute (in our example, we use the ‘id’ attribute of Number type, to identify our records). Click Create.

Partition key — A simple primary key, composed of one attribute known as the partition key. Attributes in DynamoDB are similar in many ways to fields or columns in other database systems.

Partition key and Sort key — Referred to as a composite primary key, this type of key is composed of two attributes. The first attribute is the partition key, and the second attribute is the sort key.

Learn more about keys - link1, link2.

  • After the table is created, you can select the table and view its properties. To view the entries in the table, click on the Items tab.
  • You could see the partition key attribute appearing as a column name already. Currently, our table is empty. We will leave it to the Lambda function to handle the insertion process for us.

Step 2: Create a Lambda function with necessary permissions.

  • Before creating the function, we are going to create a role to allow our lambda function to access DynamoDB.

IAM Role: an IAM identity that you can create in your account that has specific permissions. An IAM role is similar to an IAM user, in that it is an AWS identity with permission policies that determine what the identity can and cannot do in AWS.

  • To create a new role, go to the IAM console. Then, select Roles from the menu.
  • In the dashboard, select ‘Create Role’.

Under AWS service, select Lambda (since we are creating role for Lambda)

Select the policies required for our Lambda function, then click Next.

Provide a name for the role and click Create.

Once the policy is created, it can be assigned to any lambda function.

Now, let’s create our lambda function and assign this role to it.

Go to Lambda service from the AWS console and create a new function. Provide a name and choose the language in which the function will be written.

We are gonna use python language in this article. But, few other languages are supported as well. To know about the languages supported and how to use them, visit here.

For Permissions, select the role we created, which gives permission for this function to access and work with DynamoDB. Then, click Create Function.

Our Lambda function is now created.

Now, let’s edit our function.

Step 3: Write the code to add data to the Table (in python) and Test it.

Scroll down to the editor, and open lambda_handler.py ( the main file ) where we are going to add the code for handling the data and adding it to the table.

In python, we can use the boto3 module to work with AWS resources. Learn more on using boto3 here.

Add the following code to ‘lambda_handler.py’.

import json
import boto3 # import the boto3 module
dynamodb = boto3.resource('dynamodb') # get the DynamoDB resource
bookTable = dynamodb.Table('book') # access the table 'Book'
def lambda_handler(event, content): bookTable.put_item(Item=event) # Add item to table return {
'statusCode': 200,
'body': json.dumps('Added Entry Successfully!')
}

The event in bookTable.put_item(Item=event) represents the JSON request that is sent to lambda.

Click the ‘Deploy’ button at the top, to deploy the changes.

Now, we can test our code with test cases by clicking on the 🔻 (inverted triangle) next to ‘Test’ button. And select ‘Configure test event’.

Provide a name for the test event. Edit the JSON data already present, by giving our book data (in JSON format) which will be passed as the event parameter to lambda_handler(event, content)

Now, click ‘Test’ button to test our event. Below, you can see the response that was received back.

We got the success message! We can observe that our DynamoDB table is updated with the new record.

Step 4: Create an API in API Gateway with a POST method.

Head to API Gateway service.

Among the categories displayed, select REST API, click Build.

Select REST as the protocol. Then select New API and enter a name, and finally click Create API.

We created our new API.

Let’s create a new post method, click on Actions -> Create Method

Select POST from the drop-down list

Step 5: Integrate the API with the Lambda function created and configure query parameters (for passing the data to function).

After the Post method is created, select the Integration type as Lambda function (Since our method is going to communicate with a lambda function that handles the request).
Enable the ‘Use lambda proxy integration’ check box.
Provide the name of the lambda function we created to integrate it.
Click Save.

A dialog will pop up asking about giving permissions to API gateway to access lambda, click OK to update the permissions (You can also do this manually, as we did previously for providing DynamoDB access for our lambda).

Now, select Method Request to edit the query parameters using which we are going to send the data.

Edit URL Query String Parameters, mention the parameters that are to be sent along with the request.

We add the parameters, ‘id’, ‘title’ and ‘author’. In our case, all three are required.

Step 6: Deploy the API to a new Stage

Our API is ready to be deployed. But before that, we have to make enable CORS for the domain that will be using this API.

In this example, we will be trying to call this API from Postman. So, I will be allowing all origins and domains here.

Select ‘Enable CORS’ from Actions.

Enable Default 4XX and Default 5XX for gateway responses.
Enable the method (here the method is POST).

You can modify the ‘Access-Control-Allow-Methods’ and ‘Access-Control-Allow-Headers’ based on your application requirements. (Here, I’m not adding any restrictions in order to keep it easier for testing.)

Then click Enable CORS and replace existing CORS headers

A confirm box pops up. Select ‘Yes, replace existing values’ button.

Wait for all the processes to complete. Once you can see a tick mark ✔ for all of those, our API is ready to be deployed.

To deploy our API, Select Deploy API from Actions menu.

Select New Stage for the deployment stage.

Provide a name for our new stage. Description for both stage and deployment is optional. Then click Deploy.

Our API is deployed and ready to use now. The invoke URL for our new stage is generated and we can use this URL to make a call to our API.

Now that API is ready, we have to modify our lambda function so that it can take the data from query parameters that are sent along with the API request and then add it to the DynamoDB table.

Step 7: Modify the lambda function to accept the data sent through the API request from API Gateway.

The JSON request from the API Gateway will be in a specific format.

So, let’s create a new test event. Click on the inverted triangle button next to Test and select ‘Click new test event’ to create a new event.

In the Event Template, start typing apigateway and select ‘Amazon API Gateway AWS Proxy’ from the list which updates the JSON event below that.

Provide a name for our new test case.

The JSON event represents a sample request from API Gateway. The data sent through the URL can be accessed from the "queryStringParameters" property. There we have to specify our book details to mimic the functionality for testing our function to take data from query parameters.

Edit the query string parameters with our data as shown below.

While creating the table, we mentioned the type of ‘id’ (partition key) as a Number. But the data received from a URL will be a string. So, we have to change the id attribute to Number(int) type before adding it to the table.

Let’s modify the code to convert the ‘id’ attribute to a Number and send only the queryStringParameters to the table.

event['queryStringParameters']['id'] = int(event['queryStringParameters']['id'])bookTable.put_item(Item=event['queryStringParameters'])

Click on the Test button to test our modified code.

We get success response! Now let us try making a request from the API gateway.

Go to our API in the API Gateway service. Select the POST method and Click ‘Test’.

Enter the query strings with new data. And Click Test. Query Strings should be given in the following format.

key1=value1&key2=value2&key3=value3eg, 
id=4&title=Becoming&author=MichelleObama

We get success response! Our API is working perfectly.

In this way, we can create a REST API using Amazon API Gateway with different methods and can also customise the properties based on our needs.

Now I can now try giving a POST request to the API from Postman software. (Note: I have allowed ‘all access’ to my API for testing only)

Test the API

Send a POST request to the invoke URL with the query parameters.

We received success response.

Follow for more! Happy Learning!

If you liked this article, you can support me by buying me a coffee. I’d appreciate support in any way!

--

--

Sharmila S
featurepreneur

Software Engineer | Writes about Full Stack Web Development | 1 X AWS | CKA http://sharmilas.bio.link/