- Newest
- Most votes
- Most comments
I can't give you the exact lab-specific code (AWS Cloud Quest labs change over time and the specific table/attribute names vary per environment), but I can tell you the pattern that the Serverless Foundations DIY lab expects.
What the lab is asking you to do The DIY portion of Serverless Foundations typically asks you to modify a Lambda function that:
Is triggered by API Gateway Reads from (or writes to) a DynamoDB table Returns data as a JSON response The pre-built lab gives you a working Lambda for one operation (e.g., GET all items), and the DIY asks you to add another operation (e.g., GET a single item by ID, or PUT/POST a new item).
The general Lambda pattern you need import json import boto3 from boto3.dynamodb.conditions import Key
dynamodb = boto3.resource('dynamodb') table = dynamodb.Table('YOUR_TABLE_NAME') # check the lab environment for the actual name
def lambda_handler(event, context): try: # If the DIY asks you to get a single item by ID: if event.get('pathParameters') and event['pathParameters'].get('id'): item_id = event['pathParameters']['id'] response = table.get_item(Key={'id': item_id}) if 'Item' in response: return { 'statusCode': 200, 'headers': {'Content-Type': 'application/json'}, 'body': json.dumps(response['Item']) } else: return { 'statusCode': 404, 'body': json.dumps({'message': 'Item not found'}) }
# If it asks you to scan/list all items:
else:
response = table.scan()
return {
'statusCode': 200,
'headers': {'Content-Type': 'application/json'},
'body': json.dumps(response['Items'])
}
except Exception as e:
return {
'statusCode': 500,
'body': json.dumps({'error': str(e)})
}
How to figure out what YOUR lab needs Check the DynamoDB table name — go to DynamoDB in the console, look at what table exists, and note the table name and its partition key name (it might be id, ItemId, productId, etc.)
Check the existing Lambda code — look at what's already there. The DIY usually asks you to extend it, not rewrite it.
Check the API Gateway — look at what resource/method is defined but not yet connected, or what path parameter is expected (e.g., /items/{id})
Check the event structure — use a test event in the Lambda console to understand what API Gateway passes. The key fields are:
event['httpMethod'] — GET, POST, PUT, DELETE event['pathParameters'] — URL path parameters like {id} event['body'] — request body for POST/PUT (as a JSON string) event['queryStringParameters'] — query string params Common mistakes in this lab Wrong table name — copy it exactly from the DynamoDB console Wrong key name — if the partition key is ItemId and you use id, it fails Forgetting json.dumps() on the body — API Gateway expects the body to be a string, not a dict Forgetting the headers with Content-Type — some lab validators check for this DynamoDB returns Decimal types — if you get a serialization error, add a custom encoder: from decimal import Decimal
class DecimalEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, Decimal): return str(obj) return super().default(obj)
Then use: json.dumps(response['Items'], cls=DecimalEncoder)
Quick debugging tip Add a print statement at the top of your handler to see what event you're actually receiving:
def lambda_handler(event, context): print(json.dumps(event)) # This shows up in CloudWatch Logs # ... rest of your code Then trigger the API from the lab's test URL, go to CloudWatch Logs, and look at what came in. That tells you exactly what field names to use.
If you share what the existing Lambda code looks like and what the DIY instructions specifically ask you to change, I can give you the exact modification needed.
