Creating an API with AWS: Lambda, DynamoDB, and API Gateway

Mahakdeep Kaur
4 min readJun 17, 2021

This tutorial will walk you through how to set up endpoints using API Gateway to trigger Lambda functions that interact with your DynamoDB database.

. . .

Why are we using these tools?

  • DynamoDB is a fully managed, highly available distributed database. It takes care of replicating your data across many regions without a lot of administration. It offers fast performance and scalability without hiccups in your service, which makes it an excellent tool for our use in an application.
  • Lambda is so well-known for Serverless Functions, sometimes people say Lambda when they mean Functions. :) Lambda lets us execute event-based logic without having to manage or provision servers. We’ll be using Lambda to express in a function what shape of data we’d like to retrieve from DynamoDB.
  • API Gateway is pretty well named :) It integrates with Lambda or other backend services to provide a proxy to accept API calls, aggregate the services to fulfill them, and return the appropriate result. It provides tools for the requests, such as testing and modeling.

Set up DynamoDB

After logging in to the AWS Console, search for DynamoDB from the list of services. Click “Create table” on the Getting Started screen.

Note: DynamoDB instances are specific to the AWS region selected in the top right of the AWS console window. Ensure you have the correct region selected before creating your table (this is typically the region that is geographically closest to you).

In the Create DynamoDB table window, enter a name for your table and a primary key. You can also optionally specify a sort key if you want a composite primary key for your table. For instance, you can create a Playlist table with SongName as the primary key and Artist as the sort key. You can accept the default settings.

Once your table is created, select the Items to tab for your table and create a few items to be stored in your database table. When creating an item in your table, you can also specify more fields on the item in addition to the two key fields. You have now successfully created your DynamoDB table which we can query from your Lambda function.

Lambda Functions

Now head over to the console page and search for Lambda and go to functions. Select create function and choose an author from scratch. Input your function name it could get, put, delete, or update which one you want first. Select “Create function” and give your function a name. Use Python 3.8 as the Runtime and leave “Create a new role with basic Lambda permissions” as the Execution role. Create your function.

API Gateway Trigger

In the Designer section of the Configuration tab of your Lambda function, click the ‘Add Trigger button. Set the Trigger configuration as shown below. The Security option can be changed from API key to Open or IAM depending on your desired security level, but I am choosing API key for this example. This will generate an API key that will be used when calling the API endpoint. Click ‘Add’ to add this trigger for your Lambda function. This will automatically create an API for you in the API Gateway console.

Conclusion

We have now successfully created a simple DynamoDB database and set up an endpoint using an API gateway that can be invoked to get the contents of our database.

--

--