Real Example: Process an Upload

Build the core of NoteSnap: when a note lands in S3, summarize it with Bedrock and save the result.

1 min read

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:

  1. Read the bucket and key from the S3 event
  2. Download the note
  3. Ask Amazon Bedrock for a short summary
  4. Save the summary to DynamoDB
How it flows
  1. 1S3

    notes/todo.txt uploaded

  2. 2Lambda

    Triggered with the event

  3. 3Bedrock

    Writes the summary

  4. 4DynamoDB

    Summary saved

Try it

lambda_function.py
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_plus is a classic gotcha. Without it, any filename with a space or special character fails.
  • Init Duration in 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:GetObject on the bucket, bedrock:InvokeModel on the model, and dynamodb:PutItem on the table.
  • Timeout: the default timeout is 3 seconds. Model calls can take longer, so raise it, to 30 seconds for example.
Watch out

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. 1. Your function processes uploads to bucket X and writes results back to the same prefix in bucket X. What happens?

  2. 2. Why use urllib.parse.unquote_plus on the S3 key?