Skip to main content
In this lesson you’ll learn how to connect Amazon Bedrock with AWS Lambda to automatically run generative AI tasks when new data arrives. The pattern is event-driven: Lambda reacts to incoming data (for example, files uploaded to Amazon S3), calls Bedrock to perform AI processing (summaries, Q&A, sentiment, etc.), and persists the results. This lesson follows this sequence:
  • Present the real-world problem of handling continuous data.
  • Introduce the Lambda + Bedrock solution.
  • Walk through the implementation workflow (with diagrams).
  • Share expected results and operational benefits.
  • Provide a hands-on implementation example.
Let’s begin with the real-world problem.

Real-world problem

Data arrives continuously from many sources: IoT devices, batch jobs, customer API uploads, reviews, surveys, and arbitrary file uploads. Manually reacting to each notification (for example, reading an email and triggering an AI workflow by hand) is slow, error-prone, and does not scale. An event-driven architecture removes human latency: the system responds automatically whenever relevant data appears. That enables near-real-time insights, consistent processing, and easier scaling.

Solution: Lambda + Bedrock

Use AWS Lambda as an event-driven compute layer to call Amazon Bedrock whenever new data needs generative AI processing. Lambda is a managed service that runs code in response to events, scales automatically, and uses execution roles for secure access to AWS services. Typical flow:
  • An S3 ObjectCreated event triggers a Lambda function.
  • The Lambda handler reads the object, builds a prompt, invokes Bedrock, and writes the result to a destination (S3, DynamoDB, etc.).
A diagram titled "Solution: Use Lambda With Bedrock" showing an event triggering an AWS Lambda function that executes code, reads a file, and sends a prompt. The Lambda then calls Bedrock to generate a response, with security handled by IAM roles.

Lambda permissions and IAM roles

Lambda functions assume an execution role that grants permissions for the function to read S3 objects and call Bedrock. Because the role is attached to the Lambda function, the application code does not need to manage credentials—the AWS SDK in the Lambda environment uses the function’s execution role.
Ensure your Lambda execution role follows least-privilege principles: grant only the permissions required to read the S3 bucket(s), write output, and call the Bedrock runtime API.

Workflow overview

A common S3-triggered Bedrock processing flow:
  1. A file is uploaded to S3 (ObjectCreated event).
  2. S3 event notification invokes the configured Lambda function.
  3. Lambda reads the S3 object, constructs the prompt, and calls Amazon Bedrock.
  4. Lambda stores the Bedrock-generated result back to S3 (or another destination).
A diagram titled "Workflow: Use Bedrock With Lambda" showing a four-step flow. It illustrates: file uploaded to Amazon S3 → AWS Lambda reads Python code → AI model processing by Amazon Bedrock → save generated response back to S3.

S3 event notifications

S3 supports event notifications on a per-bucket basis. You can subscribe to object creation events (Put, Post, Copy, CompleteMultipartUpload) and filter by prefix or suffix to restrict which objects trigger the event (for example, only .txt files or a specific folder). Destinations include Lambda functions, SNS topics, or SQS queues. When configuring the S3 bucket event, choose:
  • Event types to monitor (e.g., s3:ObjectCreated:*)
  • Optional key filters (prefix / suffix)
  • The destination Lambda function as the event target
A screenshot of an AWS console page titled "Workflow: S3 Events and Lambda" showing the Destination settings where a Lambda function (selected as "bedrock-summarize") is chosen as the event target. Below the screenshot is a caption instructing to link the S3 bucket event directly to the target Lambda function.

Lambda function and handler (Python example)

A Lambda function bundles your code and configuration (memory, timeout, execution role). For Python Lambdas, the handler signature is def lambda_handler(event, context):. The event parameter contains the S3 JSON payload; typical access to the object information is:
  • bucket = event['Records'][0]['s3']['bucket']['name']
  • key = event['Records'][0]['s3']['object']['key']
Below is an example Python Lambda handler that demonstrates:
  • Parsing the S3 event
  • Reading the object from S3
  • Invoking Bedrock to run a model
  • Writing the generated summary back to S3
Notes:
  • Model response schemas vary across providers and models. Adjust MODEL_ID and how you extract the generated content from the response accordingly.
  • Use environment variables to configure model IDs, output suffixes, and other behavior.
  • Keep Lambda timeouts and memory settings appropriate for the expected model latency. The Lambda maximum execution time is 15 minutes.

S3 event types and common IAM permissions

Example IAM policy statements (attach to Lambda execution role):
  • s3:GetObject for arn:aws:s3:::example-bucket/uploads/*
  • s3:PutObject for arn:aws:s3:::example-bucket/processed/*
  • Bedrock runtime permission (policy name and action may vary by account/region; follow AWS docs)

Expected results and benefits

Adopting this event-driven Lambda + Bedrock pattern delivers several advantages:
  • Near-real-time processing: Files uploaded to S3 are handled immediately without scheduled batches.
  • Automatic scaling: Lambda scales to process bursts of uploads concurrently.
  • Reduced operational overhead: No servers or containers to manage; you pay only while code runs.
  • Reusable pattern: The same approach works for other AWS event sources (API Gateway, SQS, SNS, DynamoDB streams, EventBridge).

Key takeaway

Lambda enables Bedrock workflows to run automatically in response to events. In this lesson we used S3 object creation as the trigger example, but any AWS event source can invoke the same serverless, event-driven architecture—eliminating manual steps and simplifying scale.
Be mindful of model invocation costs, latency, and data privacy. Tune Lambda timeouts and memory, and validate model response schemas before productionizing. Ensure sensitive data is handled according to your compliance requirements.

Next steps

Implement the pattern in your AWS account:
  1. Create or reuse an S3 bucket and configure event notifications with a key filter.
  2. Create a Lambda function with the execution role granting minimal S3 and Bedrock permissions.
  3. Deploy the Python handler (or a language of your choice), set MODEL_ID as an environment variable, and test with sample files.
  4. Monitor Lambda logs (CloudWatch) and track Bedrock usage and cost.
References and further reading: Good luck—build and iterate on the handler to suit your data schema and the Bedrock model you choose.

Watch Video

Practice Lab