Amazon Bedrock provides secure access to cutting-edge foundation models through a single API. This tutorial will guide you from zero to production-ready implementation in 7 clear steps.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"bedrock:ListFoundationModels",
"bedrock:InvokeModel"
],
"Resource": "*"
}
]
}
Attach this policy to your IAM user/role through AWS IAM console.
# Install AWS SDK and CLI tools
pip install boto3 awscli
# Configure AWS credentials
aws configure
Enter your AWS Access Key ID and Secret Access Key when prompted.
import boto3
bedrock = boto3.client(
service_name='bedrock',
region_name='us-east-1'
)
Available regions: us-east-1 (N. Virginia), us-west-2 (Oregon)
response = bedrock.list_foundation_models()
models = response['modelSummaries']
for model in models:
print(f"Model ID: {model['modelId']}")
print(f"Provider: {model['providerName']}")
print(f"Input Modalities: {model['inputModalities']}\n")
Model ID: anthropic.claude-v2 Provider: Anthropic Input Modalities: ['TEXT']
from botocore.exceptions import ClientError
try:
response = bedrock.invoke_model(
modelId='anthropic.claude-v2',
body=json.dumps({
"prompt": "\n\nHuman: Explain quantum computing\n\nAssistant:",
"max_tokens_to_sample": 300,
"temperature": 0.5
}),
contentType="application/json",
accept="application/json"
)
result = json.loads(response['body'].read())
print(result['completion'])
except ClientError as error:
print(f"API Error: {error.response['Error']['Message']}")
Error | Solution |
---|---|
AccessDeniedException | Verify IAM permissions |
ResourceNotFoundException | Check model ID spelling |
Explore advanced features:
By following this guide, you've established a secure Bedrock integration capable of enterprise-scale AI operations. Remember to monitor usage through AWS Cost Explorer and Bedrock's native metrics dashboard.
Category: AWS
Similar Articles