Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Serverless CRUD API with API Gateway, Lambda, and DynamoDB

A fully serverless REST API for managing tasks, built entirely on AWS. This project demonstrates how to wire together API Gateway, AWS Lambda, and DynamoDB into a working CRUD backend, with IAM handling permissions and CloudWatch handling observability.

Architecture

image

API Gateway receives HTTP requests and routes them to a single Lambda function based on the HTTP method. The Lambda function uses the AWS SDK v3 to perform CRUD operations against a DynamoDB table, and every invocation is logged to CloudWatch for monitoring and debugging.

Tech Stack

Service Purpose
Amazon API Gateway Exposes REST endpoints (GET, POST, PATCH, DELETE)
AWS Lambda Runs the business logic (Node.js, AWS SDK v3)
Amazon DynamoDB NoSQL table storing task records
AWS IAM Grants Lambda least-privilege access to DynamoDB
Amazon CloudWatch Logs and monitors API/Lambda execution
Postman Used to test and validate the deployed API

Step-by-Step Setup

1. Create a DynamoDB Table

Create a table named tasks with id as the partition key (String).

image

2. Create a Lambda Function

Create a new Lambda function (Node.js runtime) that will handle all CRUD requests coming from API Gateway.

image

3. Attach IAM Permissions

The Lambda function needs permission to read/write to the DynamoDB table. Attach an IAM policy to the Lambda's execution role granting the necessary DynamoDB actions (e.g. Scan, PutItem, UpdateItem, DeleteItem), along with the basic Lambda execution policy for CloudWatch Logs.

image

4. Add the Lambda Code

Paste the code from crudcode.mjs into the Lambda function and deploy it. It routes incoming requests to the appropriate handler based on event.httpMethod.

5. Create the REST API in API Gateway

Create a new REST API in API Gateway that will sit in front of the Lambda function.

image

6. Create a Resource

Add a resource (e.g. /tasks) under the REST API.

image

7. Create the Methods

Add each HTTP method to the resource and integrate them with the Lambda function.

GET method

image

POST method

image

PATCH method

image

DELETE method

image

8. Deploy the API

Deploy the API to a stage (e.g. prod) to get a public invoke URL.

image

9. Test the GET Endpoint in the Browser

Hitting the deployed GET URL directly in the browser returns the current list of tasks from DynamoDB.

image

Testing with Postman

10. Set Up a Postman Workspace

Create a workspace in Postman to organize and test all four endpoints (GET, POST, PATCH, DELETE).

image

11. Verify the Table in DynamoDB

After sending a POST request, check the DynamoDB console to confirm the new task item was created.

image

12. Test the PATCH Request

Send a PATCH request with updated task data.

image

13. Verify the Update in DynamoDB

Confirm the table reflects the updated task data.

image

14. Test the DELETE Request

Send a DELETE request and confirm the task is removed from the table.

image

API Endpoints Summary

Method Description Body
GET Returns all tasks
POST Creates a new task { "name": "string", "completed": boolean }
PATCH Updates an existing task { "id": "string", "name": "string", "completed": boolean }
DELETE Deletes a task { "id": "string" }

Monitoring

CloudWatch Logs is enabled on the Lambda function, so every invocation — including errors — can be traced in the CloudWatch console for debugging and monitoring API health. image

Project Structure

.
├── README.md
├── lambda/
│   └── index.mjs          # Lambda handler code
└── screenshots/           # Setup and testing screenshots

Getting Started (Reproducing This Project)

  1. Create a DynamoDB table named tasks with id as the partition key.
  2. Create a Lambda function and paste in the handler code above.
  3. Attach an IAM policy to the Lambda execution role granting DynamoDB and CloudWatch Logs permissions.
  4. Create a REST API in API Gateway with a /tasks resource.
  5. Add GET, POST, PATCH, and DELETE methods, each integrated with the Lambda function.
  6. Deploy the API to a stage to get your invoke URL.
  7. Test each endpoint using Postman (or curl/browser for GET).

About

Build a Serverless CRUD API with API Gateway, Lambda, and DynamoDB

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages