AWS Cloud Practitioner — Lab 04 of 08

Lab 04 — Contact Form + DynamoDB

Extend the contact form to store every submission in DynamoDB — email + database in one Lambda.

Intermediate~$0 Free Tier2–3 HoursRequires Lab 03

Lab Overview

PREREQ: Complete Lab 03 before this lab. You will update the Lambda function and IAM role from Lab 03.

Extend the contact form to also write every submission to an Amazon DynamoDB table. Each entry is emailed via SES AND permanently stored in a NoSQL database you can query, export, and search at any time.

ServicePurposeFree Tier
Amazon DynamoDBFully managed NoSQL database — stores every form submission25 GB + 25 WCU/RCU free forever
AWS LambdaUpdated function writing to DynamoDB AND sending email via SES1M requests/mo free
Amazon SESSends email notification — unchanged from Lab 0362,000 emails/mo free
API GatewayHTTPS endpoint — unchanged from Lab 031M calls/mo free
AWS IAMUpdated role granting Lambda permission to write to DynamoDBAlways free

Step-by-Step Instructions

1
Amazon DynamoDB
Create the ContactFormSubmissions Table
  1. Search for DynamoDB and click it
  2. Left sidebar → TablesCreate table
  3. Table name: ContactFormSubmissions
  4. Partition key: submissionId — Type: String
  5. Table settings: Customize settings
  6. Capacity mode: On-demand
  7. Click Create table → wait for status: Active
TIP: On-demand capacity means you pay only per request. At contact form volumes this is effectively free.
2
AWS IAM
Add DynamoDB Permission to LambdaSESRole
  1. Search for IAM and click it
  2. Left sidebar → Roles → search for LambdaSESRole → click it
  3. Permissions tab → Add permissionsAttach policies
  4. Search for AmazonDynamoDBFullAccess → check it → Add permissions
3
AWS Lambda
Update the Lambda Function Code

Open ContactFormHandler → Code tab. Delete all existing code and paste the following. Replace both email addresses.

import json, boto3, uuid
from datetime import datetime, timezone

ses      = boto3.client('ses', region_name='us-east-1')
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
table    = dynamodb.Table('ContactFormSubmissions')

SENDER_EMAIL    = 'your-email@example.com'  # replace
RECIPIENT_EMAIL = 'your-email@example.com'  # replace

def lambda_handler(event, context):
    try:
        body    = json.loads(event.get('body', '{}'))
        name    = body.get('name', 'Unknown')
        email   = body.get('email', 'Unknown')
        message = body.get('message', 'No message')
        sub_id  = str(uuid.uuid4())
        ts      = datetime.now(timezone.utc).isoformat()

        # Write to DynamoDB
        table.put_item(Item={
            'submissionId': sub_id, 'timestamp': ts,
            'name': name, 'email': email, 'message': message
        })

        # Send email via SES
        ses.send_email(
            Source=SENDER_EMAIL,
            Destination={'ToAddresses': [RECIPIENT_EMAIL]},
            Message={
                'Subject': {'Data': f'New contact from {name}'},
                'Body': {'Text': {'Data':
                    f'Name: {name}\\nEmail: {email}\\n'
                    f'Message: {message}\\n\\nID: {sub_id}\\nTime: {ts}'
                }}
            }
        )
        return {'statusCode': 200,
                'headers': {'Access-Control-Allow-Origin': '*'},
                'body': json.dumps({'message': 'Saved!', 'submissionId': sub_id})}
    except Exception as e:
        return {'statusCode': 500,
                'headers': {'Access-Control-Allow-Origin': '*'},
                'body': json.dumps({'error': str(e)})}
  1. Click Deploy → wait for Changes deployed
4
AWS Lambda
Test the Updated Function

Click the Test tab. Create a new test event with this JSON:

{
  "body": "{\"name\": \"Test User\", \"email\": \"test@example.com\", \"message\": \"DynamoDB test\"}",
  "httpMethod": "POST"
}
  1. Click Test
  2. Confirm Execution result: succeeded and a submissionId in the response
  3. Check your email inbox — the email should arrive with the submission ID
5
Amazon DynamoDB
View Submissions in the Console
  1. DynamoDB → Tables → ContactFormSubmissions
  2. Click Explore table items (orange button, top right)
  3. Click on the item to see all 5 attributes: submissionId, timestamp, name, email, message
  4. Submit the live form on your website — new items appear instantly

Verification Checklist

What You Learned

Lab Cleanup

IMPORTANT: Delete these resources when finished.
#ResourceHow to Delete
1DynamoDB TableDynamoDB → Tables → ContactFormSubmissions → Delete
2IAM PolicyIAM → Roles → LambdaSESRole → detach AmazonDynamoDBFullAccess