Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

AWS Logo
Menu
AWS Bedrock From Scratch: Build & Integrate AI Models Today!

AWS Bedrock From Scratch: Build & Integrate AI Models Today!

By the end of this article, we will learn how to enable Amazon Bedrock up and running, ready to integrate foundation models into your application.

Published Jan 22, 2025
For the last few weeks, I was playing with Amazon Bedrock to create a game for the AWS Game Challenge. You can learn more about the game I created here. During this challenge, I had to learn about the Amazon Bedrock integration into my code. Here, I will share how to do it.

Request model access

Image not found
Amazon Bedrock console
First things first, you need to request access to the models you need. On the AWS Console, go to the Model Access section, and click on Enable Specific Models. You can also enable all models, but let's enable them once they are needed. Don't forget that models are regional; you need to enable models per region, and some of the models might not be available in all regions.
I enabled the following models. There is a difference between the models chosen. Let's start with Llama 3 8B Instruct, which is quite straightforward to use.
Image not found
It might take a few seconds for you to see "Access Granted" on the models you chose.

Integrate the model with Python code

Here, we are going to use Python to write a simple code that uses the AWS Bedrock Llama 3 8B Instruct model. You need the model ID for that. On the Amazon Bedrock console, go to the Model Catalog section and find the model you want to use. You can copy the model ID here.
Image not found
now it's time to code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import boto3
import json

aws_region = "us-east-1"

bedrock_runtime = boto3.client(service_name="bedrock-runtime", region_name=aws_region)

model_id = "meta.llama3-8b-instruct-v1:0"

prompt_text = "Explain Amazon Bedrock to someone who has just started learning about it."

payload = {
"prompt": prompt_text,
"max_gen_len": 500, # Maximum tokens to generate
"temperature": 0.7, # Controls randomness (0 = deterministic, 1 = highly random)
"top_p": 0.9, # Controls diversity of responses
}

body = json.dumps(payload)

try:
response = bedrock_runtime.invoke_model(
modelId=model_id,
body=body
)

# Parse response
response_body = json.loads(response["body"].read())
generated_text = response_body.get("generation", "No response received.")

print("\n### Generated Response ###\n")
print(generated_text)

except Exception as e:
print("Error:", str(e))
let's create a virtual env and install the required packages:
1
2
3
python3 -m venv myenv
source myenv/bin/activate
pip install boto3
Great job! Here is the output you will probably get when you run the code. You can change the prompt as you like.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
### Generated Response ###

](https://www.quora.com/Explain-Amazon-Bedrock-to-someone-who-has-just-started-learning-about-it)

**Amazon Bedrock**: Amazon Bedrock is a new, highly scalable, and highly available database service offered by Amazon Web Services (AWS). It is designed to provide a low-latency, high-performance, and highly durable database solution for real-time applications.

Imagine a high-performance sports car. Just as a sports car is designed to provide high-speed performance, Amazon Bedrock is designed to provide high-performance database operations. It is built to handle large amounts of data and scale horizontally to meet the demands of high-traffic applications.

Here are some key features of Amazon Bedrock:

1. **High Performance**: Amazon Bedrock is designed to provide low-latency database operations, making it suitable for real-time applications such as gaming, financial transactions, and IoT devices.
2. **High Availability**: Amazon Bedrock provides high availability through its distributed architecture, which ensures that your database is always available and accessible.
3. **Scalability**: Amazon Bedrock can scale horizontally to meet the demands of high-traffic applications, ensuring that your database can handle large amounts of data and traffic.
4. **Durability**: Amazon Bedrock provides high durability through its use of multiple copies of your data, ensuring that your data is always available and recoverable in case of failures.
5. **SQL Support**: Amazon Bedrock supports SQL, making it easy to integrate with existing applications and tools.
6. **ACID Compliance**: Amazon Bedrock is ACID compliant, ensuring that your database operations are executed reliably and consistently.

In summary, Amazon Bedrock is a high-performance, highly available, and scalable database service designed for real-time applications. It provides low-latency database operations, high availability, and scalability, making it an ideal choice for applications that require high-performance and reliability.

Cross-region inference

Now let's use the other model, Llama 3.2 3B Instruct. You might notice that this model has Cross-region inference in front of it in the AWS model access section. This feature increases throughput and improves resiliency by routing your requests across multiple AWS Regions.
By changing the model_id=meta.llama3-2-3b-instruct-v1:0 variable, we encounter the following error:
Error: An error occurred (ValidationException) when calling the InvokeModel operation: Invocation of model ID meta.llama3-2-3b-instruct-v1:0 with on-demand throughput isn’t supported. Retry your request with the ID or ARN of an inference profile that contains this model.
Go to the Cross-region inference section on the AWS Bedrock console, and use a system-defined inference profile to search for your model here. In our case, it's US Meta Llama 3.2 3B Instruct. Copy the Inference Profile ARN, replace the account ARN with yours, and then place it in the model_id variable.
model_id=arn:aws:bedrock:us-east-1:12345678912:inference-profile/us.meta.llama3-2-3b-instruct-v1:0
Now you have a working code. Well done.
 

Comments