🧱How to use Amazon Bedrock with Python (boto3)
Source: Dev.to
🧰 Requirements
- Python 3.9+
- AWS account with Bedrock enabled
- AWS credentials configured (CLI, environment variables, 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)
🔍 What’s happening here?
bedrock-runtimeis 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