Project: Build a Contact Form API with Lambda, DynamoDB and SNS (Source Code)
Every business site has a Contact form. Save the message in DynamoDB and email yourself with SNS — serverless, region ap-south-1.
What you'll learn in this chapter
- Wire contact POST API
- Persist in DynamoDB
- SNS email alert
- Test and clean up
1. Everyday example
Why do we need this?
A coaching-class site: visitor sends name, email, message. You want the row saved and a mail ping — without an EC2 running all night.
What is it?
POST /contact → Lambda → PutItem + sns.publish to a topic with your email subscribed.
Download: contact-form-api.zip
2. Lab
How to do it (step by step)
Region ap-south-1.
A. DynamoDB + SNS
- Table
yourname-contacts, PKmessageIdString, on-demand. - SNS topic
yourname-contact-alerts. Email subscription → confirm in inbox.
B. IAM + Lambda
- Role: basic logs + zip inline (PutItem + sns:Publish on that topic).
- Function
yourname-contact-api, Python 3.12, handler.py. Env TABLE_NAME, TOPIC_ARN. Deploy.
C. API + test
- HTTP API POST /contact → Lambda. CORS *. Copy API_BASE.
curl -s -X POST "$API_BASE/contact" -H 'content-type: application/json' \
-d '{"name":"Riya","email":"riya@example.com","message":"Need AWS batch timing"}'
Expect {"ok":true,"messageId":"..."}, then email + DynamoDB row.
Cleanup
- Delete API, Lambda, table, SNS subscription and topic, IAM role.
- Remove the topic so lab mail does not keep arriving.
3. Interview
Q. Why SNS not SES here?
One alert email: SNS is enough. SES for branded bulk (sandbox).
Q. CORS why?
Browser page origin differs from API.
Q. PII?
Messages are personal — tight IAM, delete lab data.
Q. If SNS fails after PutItem?
Lab order saves first; production uses outbox/transactions.
Q. Cleanup?
API, Lambda, table, topic, role.
Try at home
- Add a honeypot field; reject if filled.
- Admin GET with API key.
In short
- Contact = API GW + Lambda + DynamoDB + SNS.
- Confirm SNS email.
- Validate input.
- Clean up.
Practice Tasks
Nashik Vineyards is a wine and farming business in Nashik. Meera Joshi looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.
0 / 12
Easy List functions
Why: See which functions exist in this Region.
What to do: List the Lambda functions in ap-south-1.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium Invoke a function
Why: Invoke runs the function now and saves its answer in a file.
What to do: Invoke nashikvine-orders-fn and save the response to out.json.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium Trust policy for a Lambda role
Why: The trust policy says WHO may take the role.
What to do: Write a trust policy that lets lambda.amazonaws.com assume the role.
- Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
- In each statement write Effect, Action and Resource.
- Give only what the task needs (least privilege).
- Use double quotes and commas correctly. Click Check answer.
Medium Put an item
Why: DynamoDB JSON shows the type of each value: S for string, N for number.
What to do: Put an item into nashikvine-orders with orderId "o-101" (string) and amount 2500 (number).
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium Change timeout and memory
Why: More memory also gives more CPU; timeout stops runaway runs.
What to do: Set nashikvine-orders-fn to timeout 30 seconds and memory 512 MB.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium Let a function write logs
Why: Without these actions, Lambda logs never appear in CloudWatch.
What to do: Allow logs:CreateLogGroup, logs:CreateLogStream, logs:PutLogEvents on arn:aws:logs:*:*:* only.
- Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
- In each statement write Effect, Action and Resource.
- Give only what the task needs (least privilege).
- Use double quotes and commas correctly. Click Check answer.
Hard Let S3 call your function
Why: A resource-based permission is needed before S3 can invoke the function.
What to do: Add permission statement s3invoke to nashikvine-orders-fn so s3.amazonaws.com can call lambda:InvokeFunction from bucket arn:aws:s3:::nashikvine-uploads.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Hard Create a Lambda function
Why: Lambda needs the code zip, a runtime, a handler name and an execution role.
What to do: Create function nashikvine-orders-fn: runtime python3.12, handler app.handler, role arn:aws:iam::107079156755:role/lambda-basic, code in function.zip.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Hard Set environment variables
Why: Keep settings like table names out of the code.
What to do: Set the environment variable TABLE=nashikvine-orders on nashikvine-orders-fn.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Hard Invoke with a JSON payload
Why: In CLI v2, raw JSON needs --cli-binary-format raw-in-base64-out.
What to do: Invoke nashikvine-orders-fn with payload {"orderId":"o-101"} (raw JSON) and save the reply in out.json.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Hard Lambda GB-seconds
Why: Lambda bills memory × time.
What to do: 1,000,000 invocations, 512 MB memory, 200 ms each. How many GB-seconds?
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Hard Connect an SQS queue to Lambda
Why: An event source mapping makes Lambda poll the queue and process messages in batches.
What to do: Create an event source mapping from queue arn:aws:sqs:ap-south-1:107079156755:nashikvine-orders to nashikvine-orders-fn with batch size 10.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Revision, comparison and interview cards
Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.






More cards: Ravi Sir's Cards.