Build an AI Chatbot with Amazon Bedrock

Use Amazon Bedrock and Python to build an AI chatbot in your browser

Introduction

⚡️ 30 Second Summary

Every time you chat with an AI assistant, there is a powerful cloud service working behind the scenes. But how does that actually work under the hood?

In this project, you will build an AI-powered chatbot using Amazon Bedrock and Python, starting from exploring foundation models in the AWS Console, making your first API call with boto3, and extending a simple script into a multi-turn chatbot with conversation history, system prompts, and tunable inference parameters.

What You'll Build

You'll build a Python chatbot in AWS CloudShell that uses Amazon Bedrock's Converse API to chat with the Amazon Nova 2 Lite foundation model, maintaining conversation history across multiple messages.

By the end of this project, you'll have:

  • 🧠 A hands-on exploration of the Amazon Bedrock Chat playground to interact with AI foundation models.
  • 🐍 A Python script that calls the Bedrock Converse API from AWS CloudShell.
  • 💬 A multi-turn chatbot that maintains conversation history across messages.
  • 🎭 A custom system prompt that gives your chatbot a specific personality and role.
  • 💎 Secret Mission: Add Guardrails for Responsible AI to filter harmful content and enforce safety policies.

Want a complete demo of how to do this project, from start to finish? Check out our 🎬 walkthrough:

Do I need to pay to do this project?

This project costs less than $0.01 to complete. You only need an AWS account with access to Amazon Bedrock. All coding is done in AWS CloudShell, which is free and runs directly in your browser with Python and boto3 pre-installed.

Not sure if this project is right for you? Check if it matches your goals

If you're up for a bit of a challenge, quiz yourself on the key concepts up ahead in this project.

Meet Amazon Bedrock

You're about to build an AI-powered chatbot, but before writing any code, you need to understand the service that powers it. Amazon Bedrock gives you access to over 100 foundation models from companies like Amazon, Anthropic, and Meta through a single API. No servers to manage, no infrastructure to provision.

In this step, you'll sign in to the AWS Console, explore the models available in Bedrock, and send your first prompt to an AI model using the built-in Chat playground.

In this step, get ready to:

  • Navigate to Amazon Bedrock in the AWS Console.
  • Explore foundation models in the Model catalog.
  • Chat with Amazon Nova 2 Lite in the playground.

Sign In and Navigate to Amazon Bedrock

What do you see?

✔️ I see the AWS Console dashboard

You're already signed in. Continue on to the next instruction.

🔑 I see a sign-in page

That's great! Let's enter your account details.

  • Enter your Account ID (or alias), IAM username, and password, then click Sign in.

Need help finding your credentials?

If you completed the Set Up An AWS Account for Free project, your credentials are in the .csv file you downloaded when you created your IAM user. If you set up AWS on your own, sign in with whichever credentials you normally use. If you're unsure, try signing in as a Root user with the email address you used to create your AWS account.

🆕 I don't have an account

This project requires an AWS account. Complete the Set Up An AWS Account for Free project first, then come back here. It takes about 10 minutes.

  • Check the region selector in the top right corner of the console.
  • Select US East (N. Virginia) us-east-1 if it is not already selected.

Why us-east-1?

Not every AWS region supports the same Bedrock models. The us-east-1 (N. Virginia) region has the widest availability of foundation models, so you'll always start here when working with Bedrock.

  • Click the search bar at the top of the console.
  • Type Bedrock.
  • Select Amazon Bedrock from the search results.
  • Check that you see the Amazon Bedrock overview page.

What is Amazon Bedrock?

Amazon Bedrock is a fully managed service that gives you access to foundation models from Amazon, Anthropic, Meta, Cohere, and others through a single API. You don't need to manage any infrastructure. AWS handles all the compute, and you pay only for what you use.

Explore the Model Catalog

  • In the left sidebar, click Model catalog (under Discover).
  • You should see a list of models from different providers, each with different strengths, pricing, and capabilities.

What is a foundation model?

A foundation model is a large AI model that has been pre-trained on massive datasets. Instead of training your own model from scratch (which costs millions of dollars and months of compute time), you use a foundation model through an API. Think of it like renting intelligence instead of building it.

  • Browse through the catalog. What are the differences between these models?

Chat with an AI Model

  • In the left sidebar, click Chat / Text playground (under Test).

What is the Chat playground?

The Chat playground is a built-in testing tool in Amazon Bedrock. It lets you send prompts to any foundation model and see responses in real time, directly in your browser, before writing any code.

  • Click Select model.
  • Under Categories, select Amazon.
  • Select Nova 2 Lite.
  • Click Apply.

Why Amazon Nova 2 Lite?

Amazon Nova 2 Lite is Amazon's own lightweight foundation model. It is fast, costs only $0.00006 per 1,000 input tokens, and is perfect for learning. Since it is an Amazon model, it is automatically available in your account with no third-party approval needed.

  • In the prompt box at the bottom, type the following:
Explain cloud computing in 3 sentences
  • Click the Run button.

The model will generate a response based on your prompt.

You've explored Bedrock and chatted with an AI model through the console. Next, you will write Python code to call the same model through the Converse API.

Your First AI API Call

Now it is time to write code. You will use AWS CloudShell to write a short Python script that sends a question to Amazon Nova 2 Lite and prints the answer.

In this step, get ready to:

  • Open AWS CloudShell and verify your environment.
  • Write a Python script that calls the Bedrock Converse API.
  • Run the script and see an AI-generated response.

Open AWS CloudShell

  • Click the terminal icon in the top navigation bar of the AWS Console.
  • Wait for AWS CloudShell to initialize until you see the terminal prompt.

Why use CloudShell?

CloudShell is a browser-based terminal with Python, boto3, and your AWS credentials pre-configured. Zero local setup required.

💡 What is boto3?

boto3 is the official Python SDK for AWS. It lets your Python scripts interact with AWS services like Bedrock, S3, and EC2 through code instead of clicking through the console.

  • Verify Python is installed by running:
python3 --version

✔️ I see a version number

Python is ready. Move on to the next check.

ⓧ I see an error

No worries, try these steps:

  • Check you are using CloudShell inside the AWS Console, not your local terminal.
  • Click Actions, then Restart AWS CloudShell.
  • Run python3 --version again.

Still stuck?

Get help with your error or share your error with the NextWork community!

  • Verify boto3 is available by running:
python3 -c "import boto3; print(boto3.__version__)"

✔️ I see a version number

Both Python and boto3 are ready. You can start writing code.

ⓧ I see an error

This can happen if CloudShell's environment needs a refresh. Try these steps:

  • Click Actions, then Restart AWS CloudShell.
  • Run the boto3 check command again.
  • If it still fails, try pip3 install boto3 and rerun. Need help?

Still stuck?

Get help with your error or share your error with the NextWork community!

Write Your Bedrock Script

  • In your CloudShell terminal, create a new file:
nano bedrock_chat.py

This opens nano, a text editor that runs directly in your terminal. Think of it like Notepad, but inside the command line. You type and edit code here, then save and exit back to the terminal.

Navigating in nano

Use the arrow keys to move around. You cannot click with your mouse to position the cursor. To save, press Ctrl+O, then Enter to confirm the filename. To exit, press Ctrl+X.

  • Copy and paste the following setup code into nano.
import boto3

# 1. Connect to Amazon Bedrock
client = boto3.client("bedrock-runtime", region_name="us-east-1")

# 2. Choose which AI model to use
model_id = "amazon.nova-lite-v1:0"

# 3. Create your prompt
messages = [
    {
        "role": "user",
        "content": [{"text": "What is cloud computing? Explain in 2 sentences."}]
    }
]

Safe Paste dialog

If a Safe Paste dialog appears, click Paste to confirm. CloudShell shows this warning whenever you paste multiline text from an external source.

  • Double-check your code looks like the following screenshot:

What does this code do?

  • #1 boto3.client(...) uses the boto3 SDK to create a connection to the Bedrock Runtime service in the us-east-1 region.
  • #2 model_id tells Bedrock which foundation model to use.
  • #3 messages is a list that contains your prompt. Each message has a role (who is speaking, e.g. "user") and content (the text of the message). This is the format the Bedrock API expects.
  • Still in nano, add the following code below your messages list to call the API and print the response:
# 4. Send the message and get a response
response = client.converse(modelId=model_id, messages=messages)

# 5. Extract the text and print it
response_text = response["output"]["message"]["content"][0]["text"]
print(response_text)

What is the Converse API?

The Converse API is Amazon Bedrock's unified interface for talking to any foundation model. The same code works whether you use Amazon Nova 2 Lite, Anthropic Claude, or Meta Llama. No model-specific formatting required.

  • #4 client.converse() sends your messages list to the model and returns a response.
  • #5 The response is a nested dictionary. These lines dig into it to extract the text and print it.
  • Double-check your complete script looks like the following screenshot:
  • Save the file:
    • Press Ctrl+O, then Enter to confirm the filename.
  • Exit nano:
    • Press Ctrl+X.

Run Your Script

  • Back in CloudShell, run the script:
python3 bedrock_chat.py

What just happened?

Your Python script used boto3 to send an API request to Amazon Bedrock, which forwarded your prompt to Nova 2 Lite. The model generated a response and sent it back to your terminal. All of this happened in seconds, with no servers to manage.

✔️ I see a response

You just made your first AI API call. Nice work.

ⓧ I see an error

No worries - we'll troubleshoot it!

Select the tab that matches the error message you see:

AccessDeniedException

  • Go back to Step 1 and verify you enabled the Amazon Nova 2 Lite model.
  • Check you are in the us-east-1 region.
  • Re-run python3 bedrock_chat.py.

Could not connect to endpoint

  • Check your script uses region_name="us-east-1".
  • Check your CloudShell session is in us-east-1 (visible in the tab at the top).
  • Re-run python3 bedrock_chat.py.

Something else...

  • Check your script matches the code above exactly.
  • Look for missing quotes, extra spaces, or typos.
  • Save the file and re-run python3 bedrock_chat.py.

Still stuck?

Get help with your error or share your error with the NextWork community!

You have just sent your first request to an AI model through code. Next, you will turn this script into a chatbot that holds a multi-turn conversation.

Build a Chatbot

Right now your script is a one-shot interaction: you send a message, get a response, and the program ends. In this step, you will turn it into an interactive chatbot with conversation memory, a custom personality, and tunable response controls.

In this step, get ready to:

  • Add a system prompt to give your chatbot a personality.
  • Build a conversation loop with conversation history.
  • Tune your chatbot's responses with inference parameters.

Create Your Chatbot Script

Your previous script (bedrock_chat.py) is finished. You will now create a brand new script for the chatbot.

  • In your CloudShell terminal, create a new file for your chatbot:
nano bedrock_chat_revised.py
  • Copy and paste the following setup code into nano.
import boto3

# 1. Connect to Bedrock and choose the model
client = boto3.client("bedrock-runtime", region_name="us-east-1")
model_id = "amazon.nova-lite-v1:0"

# 2. Give the chatbot a personality
system_prompt = [{"text": "You are a friendly cloud computing tutor. Explain concepts simply and use analogies."}]

# 3. Store conversation history
messages = []
  • Double-check your code looks like the following screenshot:

What changed from Step 2?

This is similar to your first script, but with two new additions:

  • #2 system_prompt is a system prompt that shapes how the AI responds. It gets passed to every API call but never appears in the conversation itself. Think of it like giving the chatbot a job description before it starts talking to users.
  • #3 messages is an empty list that will store the full conversation history. Each time you or the chatbot sends a message, it gets appended here. Since every API call is stateless, your script sends this entire list each time so the model can read the full context.
  • Still in nano, add the conversation loop below the setup code:
# 4. Start the conversation loop
print("Chatbot ready! Type 'quit' to exit.")

while True:
    user_input = input("You: ")
    if user_input.lower() in ["quit", "exit"]:
        print("Goodbye!")
        break

    # 5. Add the user's message to conversation history
    messages.append({"role": "user", "content": [{"text": user_input}]})

    # 6. Send the full conversation to Bedrock
    response = client.converse(
        modelId=model_id,
        messages=messages,
        system=system_prompt,
        inferenceConfig={
            "temperature": 0.7,   # 0.0 = predictable, 1.0 = creative
            "topP": 0.9,          # 0.0-1.0, filters word choices
            "maxTokens": 512      # max response length
        }
    )

    # 7. Save the response and print it
    assistant_message = response["output"]["message"]
    messages.append(assistant_message)

    print(f"Bot: {assistant_message['content'][0]['text']}")

What does this loop do?

  • #4 Prints a welcome message and starts a loop that waits for your input.
  • #5 Appends your message to the messages list so the model can see it.
  • #6 Calls the Converse API with the full conversation history, the system prompt, and an inferenceConfig that controls how the model generates text.
  • #7 Appends the assistant's response to messages too, so the next turn has the full context.

💡 What are inference parameters?

The inferenceConfig controls how the model generates responses. Find these three values in your code:

  • temperature (range: 0.0 to 1.0): Controls creativity. A value of 0.1 gives predictable, factual answers. A value of 0.9 gives more creative, varied responses. Your script uses 0.7.
  • topP (range: 0.0 to 1.0): Controls diversity by filtering which words the model considers. Works alongside temperature.
  • maxTokens: The maximum length of the response, roughly measured in words. Your script caps it at 512.
  • Save the file:
    • Press Ctrl+O, then Enter to confirm.
    • Press Ctrl+X to exit nano.

Test Your Chatbot

  • Back in CloudShell, run your chatbot:
python3 bedrock_chat_revised.py

Now we're going to test multi-turn conversation by asking a question, then a follow-up that references the previous answer.

  • Type the following prompt:
What is an S3 bucket?
  • Then send the same follow up prompt:
Can you give me an analogy for that?

The chatbot should reference its previous answer about S3 in the analogy. This works because your script sends the full messages list on every call, giving the model context to understand follow-up questions.

Experiment with Temperature

Your chatbot currently uses a temperature of 0.7. What happens if you turn that down?

  • Type quit to exit your chatbot if it is still running.
  • Open the script again:
nano bedrock_chat_revised.py
  • Use the arrow keys to find the inferenceConfig section in your code. Look for the line that says "temperature": 0.7.
  • Change 0.7 to 0.1.
  • Save with Ctrl+O, press Enter, and exit with Ctrl+X.
  • Run the chatbot again:
python3 bedrock_chat_revised.py
  • Ask the same question as before:
What is an S3 bucket?

Notice the difference?

With a low temperature, the response is more direct, factual, and predictable. With a higher temperature, the model gets more creative and varied. This is how AI teams fine-tune chatbot behavior in production: customer support bots use low temperatures for consistent answers, while creative writing tools crank it up for variety.

  • Type quit to exit the chatbot when you are finished testing.

✔️ My chatbot is working

Nice work, your chatbot is holding conversations!

ⓧ I see an error

No worries, here are the most common fixes:

  • If you see AccessDeniedException, make sure you enabled model access for Amazon Nova 2 Lite in Step 1.
  • If you see SyntaxError, double-check your indentation. Python is strict about spaces.
  • Make sure you are running python3 bedrock_chat_revised.py (not bedrock_chat.py).
  • Help me troubleshoot this error.

Still stuck?

Get help with your error or share your error with the NextWork community!

Your chatbot is alive and remembering your conversations. Next up, you will take things further in the Secret Mission by adding safety guardrails to make your chatbot production-ready.

Secret mission

Your chatbot is up and running, but what happens if someone asks it to generate harmful content? In this secret mission, you'll create an Amazon Bedrock Guardrail that filters harmful content, then apply it to your chatbot with a single config change.

In this secret mission, get ready to: - [ ] Create a guardrail with content filters. - [ ] Add the guardrail to your Converse API call. - [ ] Test that harmful content requests get blocked.

Add Guardrails for Responsible AI

Clean Up Your Resources

Clean Up Your Resources

Decide whether to keep your resources running, pause them to come back later, or delete them entirely.

Cost warning

This project uses Amazon Bedrock, which charges per API call. Your total cost for this project is under $0.10. There are no running servers, so there are no ongoing costs after you stop making API calls.

Resources you used:

  • Amazon Bedrock model access (pay-per-use, no ongoing cost)
  • Python scripts in AWS CloudShell: bedrock_chat.py, bedrock_chat_revised.py, bedrock_chat_guardrail.py (Secret Mission)
  • Bedrock guardrail (Secret Mission only)

✔️ Keep everything running

No action needed. Choose this if you want to keep experimenting with Amazon Bedrock or modify your chatbot script.

Your bedrock_chat.py script is saved in AWS CloudShell's persistent storage (up to 1 GB in your home directory). It will be there next time you open CloudShell. There are no ongoing costs from Bedrock itself since you only pay per API call.

✋ Pause - I'll come back to this later

There are no running services to pause. Amazon Bedrock only charges when you make API calls, so leaving your setup as-is costs nothing.

Your bedrock_chat.py script remains in AWS CloudShell's persistent storage and will be available next time you open CloudShell.

If you created a guardrail during the Secret Mission, it also has no ongoing cost. You can leave it in place.

ⓧ Delete - I don't want to use this again

Remove all project resources and start fresh if you ever want to rebuild.

Delete your Python script:

  • Open AWS CloudShell in the AWS Management Console.
  • Run:
rm bedrock_chat.py bedrock_chat_revised.py bedrock_chat_guardrail.py

Delete your guardrail (Secret Mission only):

  • In the AWS Console search bar, type Bedrock and select Amazon Bedrock.
  • Select Guardrails in the left sidebar.
  • Select the guardrail you created.
  • Click Delete.
  • Confirm the deletion.

Nice work

Nice work

Nice work! 🚀 You just built an AI-powered chatbot using Amazon Bedrock and the Converse API.

You've learned how to:

  • 🔍 Explore Amazon Bedrock and its catalog of foundation models.
  • 💬 Interact with Amazon Nova 2 Lite in the Chat playground.
  • 🐍 Write a Python script to make your first AI API call using the Converse API.
  • 🔄 Build a multi-turn chatbot that maintains conversation history across messages.
  • 🎭 Add a custom system prompt to give the chatbot a specific personality.
  • 🎛️ Tune inference parameters like temperature, top_p, and max_tokens to control AI responses.
  • 💎 Create a Bedrock Guardrail with content filtering for responsible AI.

p.s. Does it say "Still tasks to complete!" at the bottom of the screen?

This means you still have screenshots left to upload, or questions left to answer!

  1. Press Ctrl+F (Windows) or Command+F (Mac) on your keyboard.
  2. Search for the text Return to later.
  3. Jump straight to your incomplete tasks!
  4. 🙋‍♀️ Still stuck? Ask the community!