Let's think about it this way for a moment
An EC2-based architecture (Project 1) has to keep a server running, so you pay even during idle time — a serverless architecture (Lambda + API Gateway + DynamoDB) only runs when a request comes in, with zero cost while idle (which makes it especially cost-effective for low-traffic side projects/MVPs). API Gateway receives the HTTP route and triggers a Lambda function, and the Lambda function connects to DynamoDB via an IAM Role (no access key needed) to read/write data.
Let's connect it to a real scenario
Define two endpoints in API Gateway — `POST /todos` (create a new todo item) and `GET /todos/{id}` (read a todo item) — and wire them up to two Lambda functions (create-todo, get-todo). Attach an IAM Role (with DynamoDB read/write permission) to each Lambda, and create the DynamoDB table with `todoId` as the Partition Key.
Let's walk through it together
# create_todo.py (Lambda function)
import json, boto3, uuid
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Todos')
def lambda_handler(event, context):
body = json.loads(event['body'])
todo_id = str(uuid.uuid4())
table.put_item(Item={'todoId': todo_id, 'title': body['title'], 'done': False})
return {'statusCode': 201, 'body': json.dumps({'todoId': todo_id})}$ curl -X POST https://abc123.execute-api.us-east-1.amazonaws.com/todos -d '{"title":"Learn AWS"}'
{"todoId": "a1b2c3d4-..."}Try it in 5 minutes
Build the todo app API yourself (create + get endpoints, DynamoDB table) — send POST/GET requests via `curl` and confirm it works end to end.
A quick word of caution
Serverless architecture is cost-effective, but don't assume that means 'costs never go up' — if traffic spikes (say, a viral post), Lambda invocation count and DynamoDB read/write units will scale up accordingly too. Keep the Billing Alert from the Cost Management chapter set up.