Yeh poora article English mein hai, seedha copy-paste kar sakte ho.
---
# 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**.
2. Table name: `notes`
3. Partition key: `id` (String)
4. Keep the default settings and create the table.
## Step 2: Create the Lambda function
1. Open the Lambda console and click **Create function**.
2. Choose **Author from scratch**, name it `notes-api`, and select **Python 3.12**.
3. 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"})
```
4. 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**.
2. Create a resource named `/notes`.
3. Add `POST` and `GET` methods and connect both to the `notes-api` Lambda function (enable Lambda proxy integration).
4. 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
Loading article