Skip to main content
This lesson walks through a compact, end-to-end Python example that:
  • reads a text file from Amazon S3,
  • uses Amazon Bedrock to generate a concise summary,
  • writes the summary back to S3.
The sample is intentionally small and focused so you can reuse the same pattern in scheduled jobs, event-driven Lambda functions, or batch pipelines.
Ensure your environment has valid AWS credentials and IAM permissions for S3 and Amazon Bedrock Runtime. Install boto3 (for example, pip install boto3) and set AWS_REGION if necessary. See the Amazon Bedrock overview for service details.
Overview (high-level)
  • Configure constants: region, bucket, S3 keys, and model identifier.
  • Initialize boto3 clients for S3 and Bedrock Runtime.
  • Read input text from S3.
  • Build a concise summarization prompt and call Bedrock’s runtime.
  • Parse the model response and write the generated summary back to S3.
Quick step summary Complete Python script
  • Save as read_generate_write_s3.py.
  • The script is minimal but includes robust parsing for common Bedrock response shapes.
How the response parsing maps to common Bedrock outputs Terminal example (trimmed)
Post-run: view the generated summary A typical generated summary might look like:
Best practices and why this pattern is useful
  • Scales from single-file processing to batch/stream workflows.
  • Use S3 as a persistent, cost-effective storage layer for raw inputs and processed outputs.
  • Trigger options: scheduled jobs, S3 events + Lambda, containerized consumers, or stream processors.
  • Tune prompt engineering and model parameters (temperature, maxTokensToSample) to adjust style and length.
Common pitfalls and troubleshooting
Check your IAM policies: you need permissions to read/write the S3 objects and permission to call Bedrock Runtime APIs. If you see access denied errors, verify the role or credentials used by your environment (CLI, EC2, Lambda, or container).
Additional notes and references
  • If you need finer-grained control over streaming outputs or different Bedrock integrations, some SDKs or model adapters may expose other method names (for example, converse) or different response envelopes—adapt the parsing logic accordingly.
  • Useful links:
Why this integration matters
  • Programmatic summarization and extraction enable teams to unlock insights from existing text data without manual aggregation.
  • The pattern is adaptable to many text-processing tasks (classification, extraction, QA) and integrates naturally with analytics or downstream automation.
Key takeaway:
A slide titled "Key Takeaway" with a blue left panel and a numbered badge. The takeaway reads: "AI becomes valuable when applied directly to data stored in systems such as S3."
S3 provides a simple, programmatic storage layer that, when combined with Bedrock, lets you unlock the insights hidden in your existing data. Next steps
  • Run the sample script against your own bucket and input files.
  • Experiment with different prompts, model IDs, and inferenceConfig parameters.
  • Integrate the pattern into a production flow: S3 events + Lambda, step functions, or batch jobs.
A presentation slide titled "What's Next? Using Bedrock with Amazon S3 Lab" with a teal AI/circuit brain icon on a dark curved background. The slide also shows a small "© Copyright KodeKloud" in the corner.
Build on this basic pattern to create production-ready automation and scalable text processing pipelines with Amazon Bedrock and Amazon S3.

Watch Video

Practice Lab