DEV Community

Cover image for 🧱How to use Amazon Bedrock with Python (boto3)
Fernando Gutierrez
Fernando Gutierrez

Posted on

🧱How to use Amazon Bedrock with Python (boto3)

Amazon Bedrock makes it incredibly easy to work with foundation models like Claude without worrying about infrastructure.

In this post, I’ll show you how to invoke a Bedrock model using Python and boto3 in a clean and production-ready way.

🧰 Requirements

  • Python 3.9+
  • AWS account with Bedrock enabled
  • AWS credentials configured (CLI, env vars, or IAM role)

📦 Install boto3
pip install boto3

🧠 Python example: Claude on Amazon Bedrock

import boto3
import json

client = boto3.client(
    "bedrock-runtime",
    region_name="us-east-1"
)

payload = {
    "prompt": "Human: Explain what Amazon Bedrock is in simple terms.\nAssistant:",
    "max_tokens_to_sample": 200
}

response = client.invoke_model(
    modelId="anthropic.claude-v2",
    body=json.dumps(payload)
)

result = response["body"].read().decode("utf-8")
print(result)

Enter fullscreen mode Exit fullscreen mode

🔍 What’s happening here?

  • bedrock-runtime is used to call foundation models
  • invoke_model() sends a prompt to Claude
  • The response is streamed back and decoded

🚀 Final thoughts

Amazon Bedrock + Python is a powerful combo for building AI-powered applications quickly and securely.

Perfect for:

  • AI assistants
  • Knowledge bots
  • Cloud-native AI projects

Top comments (0)