Step by Step: Setting Up AWS Bedrock for Your AI Projects
We’re building an AI application that leverages AWS Bedrock for machine learning capabilities. It’s crucial because it simplifies model access and management, eliminating many headaches you’d normally face with AI deployments.
Prerequisites
- AWS Account — Make sure you have an active AWS account. Sign up here.
- AWS CLI — Version 2.0 or higher. Check your version by running
aws --version. - Python 3.11+ — Ensure you have the correct version installed.
- SDK for Python (Boto3) — Install it with
pip install boto3.
Step 1: Set Up the AWS CLI
First, you’ll configure your AWS CLI. This step is critical because your CLI will be your main tool for interacting with AWS Bedrock.
aws configure
This command prompts you for your AWS Access Key, Secret Key, region, and output format. If you run into issues here, make sure your keys are correct. You can get your Access Keys from the IAM console in AWS. And if you’re still stuck, double-check if you have permission to access the service!
Step 2: Create a New IAM Role
An IAM role grants permissions to your AWS Bedrock service. Assigning the right roles is critical — trust me, I’ve had apps crash because I forgot permissions.
import boto3
iam = boto3.client('iam')
role_name = 'BedrockServiceRole'
trust_relationship_policy = {
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {
"Service": "bedrock.amazonaws.com"
},
"Action": "sts:AssumeRole"
}]
}
response = iam.create_role(
RoleName=role_name,
AssumeRolePolicyDocument=json.dumps(trust_relationship_policy)
)
print(f'Created Role: {response["Role"]["Arn"]}')
If you encounter the error An error occurred (EntityAlreadyExists), it’s likely because you already created this role. Check your IAM console for existing roles.
Step 3: Attach Policies to Your Role
Next, you need to attach the appropriate policies to allow your role to access Bedrock. This is where you can really get into trouble if you choose the wrong permissions.
policy_arns = [
'arn:aws:iam::aws:policy/service-role/AWSBedrockServicePolicy'
]
for policy_arn in policy_arns:
iam.attach_role_policy(
RoleName=role_name,
PolicyArn=policy_arn
)
print('Attached Policies to Role')
For errors, if you see Access Denied, verify your own permissions. Someone should have given you admin access, right?
Step 4: Launch a Bedrock Model
With the IAM setup, it’s time to deploy a model. We’ll use a built-in model provided by Bedrock. The choice of model matters; different models perform differently based on your specific needs.
bedrock = boto3.client('bedrock')
model_response = bedrock.start_model(
modelId='your_model_id_here', # Replace with actual model ID
modelParameters={
# Include any model parameters required by the model
},
roleArn=f'arn:aws:iam::your_account_id:role/{role_name}' # Adjust your account ID
)
print('Model Launched:', model_response)
Common issues can arise here, especially if your model ID doesn’t exist. Double-check the Bedrock documentation for the available models.
Step 5: Sending Data to Your Model
Once the model is running, you need to send it some data for inference. This step is often overlooked in tutorials, but getting it right can save you tons of debugging time.
data_to_send = {"input": "Your input data here"}
inference_response = bedrock.invoke_model(
modelId='your_model_id_here', # Same model ID as before
body=json.dumps(data_to_send)
)
print('Inference Response:', inference_response)
If you hit a Validation Error, the data format likely isn’t what the model expects. Check the model specs to make sure you’re sending it correctly!
The Gotchas
- Cost Surprises: AWS services aren’t free, and Bedrock can build up costs quickly if you’re not careful. Monitor your billing dashboard closely.
- Permission Denied: Not giving adequate permissions to your IAM role could result in a long debugging session. Always define the minimum permissions your application needs.
- Model Selection: Choosing the wrong Bedrock model can impact your results significantly. Don’t just go with the most complex option; test several and compare outputs.
- Data Formats: Each model expects different types of input data. Be crystal clear on what’s needed to prevent runtime errors. Check twice before sending!
Full Code
import boto3
import json
# Step 1: Configure the AWS CLI
# aws configure
# Step 2: Create IAM role
iam = boto3.client('iam')
role_name = 'BedrockServiceRole'
trust_relationship_policy = {
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {
"Service": "bedrock.amazonaws.com"
},
"Action": "sts:AssumeRole"
}]
}
response = iam.create_role(
RoleName=role_name,
AssumeRolePolicyDocument=json.dumps(trust_relationship_policy)
)
print(f'Created Role: {response["Role"]["Arn"]}')
# Step 3: Attach policies
policy_arns = [
'arn:aws:iam::aws:policy/service-role/AWSBedrockServicePolicy'
]
for policy_arn in policy_arns:
iam.attach_role_policy(
RoleName=role_name,
PolicyArn=policy_arn
)
print('Attached Policies to Role')
# Step 4: Launch a Bedrock Model
bedrock = boto3.client('bedrock')
model_response = bedrock.start_model(
modelId='your_model_id_here',
modelParameters={ }
)
print('Model Launched:', model_response)
# Step 5: Send Data to Model
data_to_send = {"input": "Your input data here"}
inference_response = bedrock.invoke_model(
modelId='your_model_id_here',
body=json.dumps(data_to_send)
)
print('Inference Response:', inference_response)
What’s Next
Your next step? Really get into testing your AI app. Set up a test suite with various inputs and determine how the model behaves under different conditions. This is where the real learning begins!
FAQ
- Q: Can I run Bedrock models locally?
A: No, Bedrock is a managed service and runs in the AWS cloud. - Q: What kind of models are available in Bedrock?
A: Bedrock offers various pre-trained models depending on your needs, such as NLP or image analysis. Check the documentation for details. - Q: How do I clean up resources after I’m done?
A: Remember to terminate your models and delete any IAM roles or managed policies you’ve created to avoid incurring charges.
| Service | Cost per hour | Average training time | Usage type |
|---|---|---|---|
| AWS Bedrock Generic Model | $0.10 | Varies | Pay-as-you-go |
| AWS Bedrock Custom Model | $1.00 | Varies | Pay-as-you-go |
Last updated April 18, 2026. Data sourced from official docs and community benchmarks.
🕒 Published: