In this tutorial, I will walk you through the process of building a serverless API that enables the creates, reads, updates, and deletes of items stored in a DynamoDB table. By following the steps outlined below, you will be able to develop a scalable and efficient API using AWS Lambda, API Gateway, and DynamoDB.
You can do it within the AWS Free Tier.
Here are the high-level steps we’ll follow:
Step 1: Create a DynamoDB table
Step 2: Create a Lambda function
Step 3: Create an API gateway
Step 4: Test your API
Step 5: Monitor your Logs and Insights
Here is a link to my github repo: https://github.com/Uwadon1/AWS_RestLambdaAPI
Step-by-Step Tutorial: Build a REST API (CRUD)
Step ONE: Create a DynamoDB table
Set Up the Environment
Sign in to the AWS Management Console, we will be brought to the aws homepage.
Create a DynamoDB Table
From the AWS dashboard, search for DynamoDb, then click on the DynamoDB service to navigate into the dynamo db dashboard.
Click on the “create table icon
Fill in the necessary details and give your table a name (e.g employee_info
or any name of your choice). You can specify the partition key as employeeid
(string). Let the table setting remain as default.
Leave the remaining options as default and click “create table.”
By following these steps, you will successfully create a DynamoDB table named “employee_info” with the partition key set to “employeeid”.
Step TWO: Create a Lambda function
Create Lambda Functions
For this project, we will be using AWS Lambda for our backend logic:
From the AWS Management Console, search for AWS Lambda service.
Click on “create a function”:
In the “create a function” dashboard, select “author from scratch.” In the basic information section, give your lambda a function name e.g api_processing
, we will select Python 3.13 as our runtime
For Permission, in the execution role section, we will opt for the third/last option: “create a new role from AWS policy templates.”
This new role should give us the opportunity to attach policy that grants full acces to DynamoDb and Cloudwatch or a custom policy with limited access to dynamo db. Afterwards, we will click on ‘create function’
We will be brougth to the lambda dashboard, showing function successfully created.
Right now, we’ll need to tweak the permission for our IAM role. Navigate to the configuration section, and select the permission button. Next you’ll need to click on the link in the role name section.
You will be redirected to the IAM service tab, you will notice by default we already have a policy that has AWSLambdaBasicExecutionRole, we will need to attach more policies like Dynamodb full access and Cloudwatchlog full access
Next we will need to attach our desired policies to this role, first we’ll add “AwsDynamoDBFullAccess”
We will add the next policy to this role, which is: “CloudWatchLogsFullAccess”
Policies have been added, hence our lambda now has the required access to interact with DynamoDB and to get logs from out Cloudwatch.
By following these steps, you will have created a Lambda function named “api_processing” and set up a new role with the appropriate permissions to interact with DynamoDB.
Step THREE: Create an API gateway
In the AWS dashboard, search for API Gateway and click on it.
You will be brought straight to Create API dashboard, choose REST API and click on build
Choose “new API” and give it a name (e.g., serverless-demo`).
Leave the description section blank and select regional as the API endpoint type. Then click on create api
Next, we are going to create resources: Click on “Create Resource”
We’ll need to give a resource a name, in the resource name section, write status
and in the resource path select /
. Ensure to check the button to enable CORS for cross-origin requests. And click create resource.
As seen below, the resource has been successfully created, next we’ll create methods for the status resource:
When you click on the “Create Method” icon, you will be brought to the tab below, select the method “get” and click the checkmark. Select lambda function as the Integration type. Ensure to click on the button to turn on lambda proxy integration.
Specify your Lambda function name (api_processing
), leave the other options as default. Click ‘create method”.
The get method for resource status has been successfully created.
We’ll need to create a new resource and give it the name employee
and in the resource path select /
. Ensure to check the button to enable CORS for cross-origin requests. And click create resource.
The newly created resource should look like this.
We’ll need to create a new resource and give it the name employees
and in the resource path select /
. Ensure to check the button to enable CORS for cross-origin requests. And click create resource.
The newly created resource should look like this, next we’ll create ‘get’ method for the employees resource:
Click on the “Create Method” icon, you will be brought to the tab below, select the method “get” and click the checkmark. Select lambda function as the Integration type. Ensure to click on the button to turn on lambda proxy integration.
Specify your Lambda function name (api_processing
), leave the other options as default. Click ‘create method’, then confirm.
The newly created get method for the /employees resource should look like this.
Lastly we will need to create the CRUD methods for the employee resource. For each HTTP method (GET, POST, DELETE, PATCH):
“Click on create Method” icon, you will be brought to the tab below, select the method “get” and click the checkmark. Select lambda function as the Integration type. Ensure to click on the button to turn on lambda proxy integration.
Specify your Lambda function name (api_processing
), leave the other options as default. Click ‘create method’, then confirm.
The newly created get method should look like this.
In the “Create Method” icon, you will be brought to the tab below, select the method “post” and click the checkmark. Select lambda function as the Integration type. Ensure to click on the button to turn on lambda proxy integration. Specify your Lambda function name (api_processing
), leave the other options as default. Click ‘create method’, then confirm.
The newly created post method should look like this.
In the “Create Method” icon, you will be brought to the tab below, select the method “delete” and click the checkmark. Select lambda function as the Integration type. Ensure to click on the button to turn on lambda proxy integration. Specify your Lambda function name (api_processing
), leave the other options as default. Click ‘create method’, then confirm.
The newly created delete method should look like this.
In the “Create Method” icon, you will be brought to the tab below, select the method “patch” and click the checkmark. Select lambda function as the Integration type. Ensure to click on the button to turn on lambda proxy integration. Specify your Lambda function name (api_processing
), leave the other options as default. Click ‘create method’, then confirm.
The newly created patch method should look like this.
Resources: These are endpoints in your API (e.g., /employee or /status). They represent logical containers for HTTP methods and map directly to backend operations.
Methods: These are the HTTP operations (like GET, POST, PUT, DELETE) applied to a resource. They determine what action is performed on the resource. For example:
GET: Fetch data.
POST: Create data.
PUT: Update existing data.
DELETE: Remove data.
API Gateway integrates these resources and methods with AWS Lambda to execute backend logic or interact with a database like DynamoDB
Click on deploy API
In the deploy API overview, a box would pop up for you to choose a stage (New Stage), and then type in a stage name (dev), you can fill in the description box or leave it blank, it is optional. Then click on deploy.
By following these steps, you will have successfully created a REST API named “serverless-demo” that serves as an HTTP endpoint for your Lambda function. In the subsequent steps. Take note of the invoke URL, we will need it later.
Step FOUR: Test your API
This right here is the code editor overview, and we have the default lambda code, so we will try to run the invoke url code to test its functionality. You can use tools like Postman or a browser to make API requests. We should get the response “Hello from Lambda!” on our screen.
Here is the invoke url code on our browser, we will include the pathway to our status resource (e.g /status) at the end of the URL code. The essence of the status resource is to test the functionality..
This is what we got on browser, “Hello from Lambda!” which shows that everything in our application and code is working fine.
Preferably we will also try the invoke url code on postman, we will also include the pathway to our status resource (e.g /status) at the end of the URL code. We will also select the “get” option and click on send..
This is the output on lambda: “Hello from Lambda!”
Next we will need to open the lambda dashboard and navigate to the code editor’s tab and replace the existing code with the following code snippet:
Here, we will write a logic for each CRUD operation:
POST: Insert a new record in DynamoDB.
GET: Fetch all or a specific record from DynamoDB.
UPDATE: Update a record.
DELETE: Remove a record.
You can write or upload your code on the console editor:
Here is the link to the Code on my github
This function serves as a robust backbone for employee management, leveraging the power of AWS Lambda and DynamoDB in a RESTful architecture.
Next we hit the deploy button to update our new python code.
We will move, right away to test our Lambda function. We will need to choose our test event e.g: “test-crud” and save
We will retest the /status ‘get’ API endpoint with Postman. Using the invoke url code on our browser, we will include the pathway to our status resource (e.g /status) at the end of the URL code. We will get the output below.
Using the browser, we got this output. “Service is operational” which shows that everything in our application and code is working fine.
We will test the /employee ‘post’ API endpoint with Postman. Using the invoke url code on our browser, we will include the pathway to our employee resource (e.g employee) at the end of the URL code. We will choose the following options as shown below, choose ‘post’ option, and select body, then raw, and json format to insert in our command as json.
{
“Employeeid”: “102”,
“job_title”: “CEO”,
“full_name”: “John Tom”,
“salary”: “1000000”
}.
Note we changed the last endpoint from /status to /employee. We should see ‘200 okay’. Which shows everything is functional. We should also see our output in the down box.
We will follow the same steps to input two other values.
We will test the /employee ‘delete’ API endpoint with Postman. Using the invoke url code on our browser, we will include the pathway to our employee resource (e.g /employee) at the end of the URL code. We will choose the following options as shown below, choose ‘delete’ option, and type in our command as json:
{
“Employee”: “102”
}.
We should see the output below “Operation”: “DELETE”, and ‘200 okay’. Which shows everything is functional.
We will test the /employee ‘patch’ API endpoint with Postman. Using the invoke url code on our browser, we will include the pathway to our employee resource (e.g /employee) at the end of the URL code. We will choose the following options as shown below, choose patch’ option, and type in our command as json:
{
“EmployeeId”: “103”,
“updateKey”: “full_name”,
“updateValue”: “Chioma Chukwu”
}.
We should see the output below “Operation”: “UPDATE”, and ‘200 okay’. Which shows everything is functional.
Lastly, we will test the /employees ‘get’ API endpoint with Postman. Using the invoke url code on our browser, we will include the pathway to our employees resource (e.g /employees) at the end of the URL code. We will get the info output of all the employees in the company .
We will go into our Dynamo DB dashboard to verify if all we have created using our API and lambda would reflect inside our database
Step FIVE: Monitoring the logs and insights
Using cloudwatch we can monitor the logs for insights to check for errors and troubleshoot where necessary
Top comments (0)