AWS Builder Center

Build a Serverless Notes API on AWS with Lambda, API Gateway, and DynamoDB

Learn how to build a serverless REST API on AWS using Lambda, API Gateway, and DynamoDB. This beginner-friendly tutorial covers table setup, Python code, least-privilege IAM permissions, testing with curl, and cost-saving tips, all within the Free Tier.

B-tech CSE Student | Aspiring Cloud Security Engineer | AWS & Cloud Computing | Cyber security

Build a Serverless Notes API on AWS with Lambda, API Gateway, and DynamoDB

Serverless lets you run backend code without managing servers. In this tutorial, you will build a simple REST API that saves and retrieves notes using three AWS services.

Architecture

Client → API Gateway → Lambda → DynamoDB
  • API Gateway receives HTTP requests.
  • Lambda runs your Python code.
  • DynamoDB stores the data.

Prerequisites

  • An AWS account (the Free Tier is enough)
  • Basic Python knowledge

Step 1: Create the DynamoDB table

  1. Open the DynamoDB console and click Create table.
  1. Table name:  notes 
  1. Partition key:  id  (String)
  1. Keep the default settings and create the table.

Step 2: Create the Lambda function

  1. Open the Lambda console and click Create function.
  1. Choose Author from scratch, name it  notes-api , and select Python 3.12.
  1. Paste the following code:
python
import json
import os
import uuid

import boto3

dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table(os.environ["TABLE_NAME"])

def response(status, body):
return {
"statusCode": status,
"headers": {"Content-Type": "application/json"},
"body": json.dumps(body),
}

def lambda_handler(event, context):
method = event.get("httpMethod")

# Create a new note
if method == "POST":
data = json.loads(event.get("body") or "{}")
if "text" not in data:
return response(400, {"error": "Field 'text' is required"})

item = {"id": str(uuid.uuid4()), "text": data["text"]}
table.put_item(Item=item)
return response(201, item)

# Fetch a note by id if method == "GET":
note_id = (event.get("queryStringParameters") or {}).get("id")
if not note_id:
return response(400, {"error": "Query parameter 'id' is required"})

result = table.get_item(Key={"id": note_id})
item = result.get("Item")
if not item:
return response(404, {"error": "Note not found"})
return response(200, item)

return response(405, {"error": "Method not allowed"})
  1. Go to Configuration → Environment variables and add  TABLE_NAME = notes .

Step 3: Grant permissions (least privilege)

Your Lambda needs permission to talk to DynamoDB. In its execution role, add this policy, replacing the region and account ID:
json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["dynamodb:PutItem", "dynamodb:GetItem"],
"Resource": "arn:aws:dynamodb:<region>:<account-id>:table/notes"
}
]
}
Granting only the actions and resources you need is a core security best practice. Avoid  AdministratorAccess  or wildcard permissions.

Step 4: Create the API

  1. Open API Gateway and create a REST API.
  1. Create a resource named  /notes .
  1. Add  POST  and  GET  methods and connect both to the  notes-api  Lambda function (enable Lambda proxy integration).
  1. Click Deploy API and create a stage named  prod .
You will get an invoke URL like  https://<api-id>.execute-api.<region>.amazonaws.com/prod .

Step 5: Test the API

Create a note:
bash
curl -X POST https://<api-id>.execute-api.<region>.amazonaws.com/prod/notes \
-H "Content-Type: application/json" \
-d '{"text": "Hello from AWS Lambda"}'
Response:
json
{"id": "b1c2d3e4-...", "text": "Hello from AWS Lambda"}
Fetch it back:
bash
curl "https://<api-id>.execute-api.<region>.amazonaws.com/prod/notes?id=b1c2d3e4-..."

Cost

All three services have generous Free Tier limits, and you pay only for what you use. A small project like this typically costs close to nothing. Set up an AWS Budget alert to be safe.

Cleanup

To avoid unexpected charges, delete the API Gateway API, the Lambda function, and the DynamoDB table when you are done.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article