Real Example: Process an Upload
Build the core of NoteSnap: when a note lands in S3, summarize it with Bedrock and save the result.
Here's the most common Lambda pattern of all: a file arrives, code reacts. No server polls the bucket. S3 calls Lambda the instant an upload finishes.
Our function will:
- Read the bucket and key from the S3 event
- Download the note
- Ask Amazon Bedrock for a short summary
- Save the summary to DynamoDB
- 1S3
notes/todo.txt uploaded
- 2Lambda
Triggered with the event
- 3Bedrock
Writes the summary
- 4DynamoDB
Summary saved
Try it
import urllib.parse
import boto3
# Created once per cold start, then reused by every warm invocation
s3 = boto3.client("s3")
bedrock = boto3.client("bedrock-runtime")
table = boto3.resource("dynamodb").Table("notesnap-summaries")
MODEL_ID = "anthropic.claude-opus-5-5" # copy the exact ID from your Bedrock console
def lambda_handler(event, context):
record = event["Records"][0]["s3"]
bucket = record["bucket"]["name"]
# Keys arrive URL-encoded ("my note.txt" -> "my+note.txt"), so decode them first
key = urllib.parse.unquote_plus(record["object"]["key"])
note = s3.get_object(Bucket=bucket, Key=key)["Body"].read().decode("utf-8")
reply = bedrock.converse(
modelId=MODEL_ID,
messages=[{"role": "user", "content": [{"text": f"Summarize in one sentence:\n\n{note}"}]}],
inferenceConfig={"maxTokens": 200},
)
summary = reply["output"]["message"]["content"][0]["text"]
table.put_item(Item={"noteId": key, "summary": summary})
print(f"Summarized {key}: {summary}")
return {"noteId": key, "summary": summary}Things to notice
unquote_plusis a classic gotcha. Without it, any filename with a space or special character fails.Init Durationin the REPORT line only appears on a cold start. It's the time spent on your module-level setup.- Permissions: the function's role needs
s3:GetObjecton the bucket,bedrock:InvokeModelon the model, anddynamodb:PutItemon the table. - Timeout: the default timeout is 3 seconds. Model calls can take longer, so raise it, to 30 seconds for example.
Never have the function write its output back into the same bucket and prefix that triggers it. Each write fires a new event, and you get an infinite loop with a surprise bill. Use a different bucket or prefix.
Key takeaways
- "File arrives → code reacts" is the most common Lambda pattern.
- Always URL-decode S3 keys from events with unquote_plus.
- Don't write output back to the prefix that triggers the function.
Check your understanding
Pick an answer to see the explanation.
1. Your function processes uploads to bucket X and writes results back to the same prefix in bucket X. What happens?
2. Why use urllib.parse.unquote_plus on the S3 key?