Introduction
In this tutorial-style video (Part 3 of the Amazon Bedrock series), we walk through the steps of creating a simple chatbot UI using Python, Streamlit, and Amazon Bedrock. You'll learn how to:
- Build a quick, interactive UI without HTML/CSS using Streamlit
- Configure your Bedrock agent with version control via aliases
- Securely authenticate with AWS using IAM, API keys, and AWS CLI
- Bring it all together to launch a functional chatbot powered by an intelligent Bedrock agent
Ready to dive in? Let's go!
1. Key Definitions
- Streamlit
A Python library that allows you to create interactive web-based interfaces—dashboards, chat UIs, and more—quickly and without needing traditional front-end code. It offers built-in UI elements and live reloading. -
Amazon Bedrock Agent
A logic-enabled interface within Amazon Bedrock that uses foundational models (FMs) to process user prompts and return generated responses. Agents help orchestrate AI tasks behind the scenes. -
Alias (Agent Alias)
A lightweight version control mechanism for Bedrock agents. An alias points to a specific version (or draft) of an agent and makes it easy to update or roll back behavior. Useful for managing iterations. -
IAM (Identity and Access Management)
AWS’s service for managing authentication and authorization. In the tutorial, a dedicated IAM user with scoped permissions is created for safe access to Bedrock via API. -
AWS CLI (aws configure)
A command-line tool to configure credentials (API keys), default region, and output format for AWS SDKs. It stores them locally so applications (like Streamlit apps) can make authorized AWS calls.
2. Step-by-Step Walkthrough
2.1 Setting Up the Streamlit Application
You start from within an IDE like VS Code, creating a Python script (app.py). Here's the foundation:
import streamlit as st import boto3
-
Install dependencies with:
pip install streamlit boto3
- Initialize your Bedrock agent runtime in Python using boto3.client('bedrock-runtime'), setting up agent_id and agent_alias_id.
-
Use Streamlit’s UI features:
- st.set_page_config() to give your app a title (e.g., "Amazon Bedrock Agent Chat")
- st.text_input() to receive user input
- st.button() for submission
- st.spinner() to show “Fetching response…” while waiting
After submission, your app invokes the Bedrock agent via:
response = bedrock_agent_runtime.invoke_agent(
agentId=AGENT_ID,
agentAliasId=AGENT_ALIAS_ID,
sessionId=SESSION_ID,
inputText=user_input
)
Then extract the response payload and st.markdown() it to display in the UI.
To run:
streamlit run app.py
Streamlit even supports switching to dark mode or wide layout easily—no extra styling needed.
2.2 Configuring the Amazon Bedrock Agent
- In the AWS Bedrock console, locate your agent (e.g., YouTube tutorial agent) and notice the absence of an alias.
- Create a new alias (e.g., YouTubeAliasAgent) with on-demand throughput.
- Copy the Agent ID and Alias ID—these are essential to plug into your Python code for invocation.
2.3 Securely Authenticating via AWS IAM
- Navigate to AWS IAM and create a new user (e.g., bedrock_user) without console access.
-
Attach appropriate policies:
- Start with AmazonBedrockLimitedAccess; if you face permission errors, escalate temporarily to AmazonBedrockFullAccess (but revert afterward for least privilege).
- Generate Access Keys for this user.
-
Locally run:
aws configure
Paste in the Access Key ID, Secret, and default region (us-east-1, for example).
This securely stores credentials, allowing your Streamlit app to invoke Bedrock correctly.
2.4 Testing it All—Run and Iterate
With everything in place:
- Run streamlit run app.py.
- Type a message (like "Hi") and click Send.
- Initially, you may hit permissions errors ("not authorized to invoke agent"). That’s expected—back in IAM, add necessary permissions or full access to resolve it.
-
Once resolved, your chatbot UI responds with something like:
“I'm doing well, thank you! How can I assist you today?”
Behind the scenes:
- Streamlit captures your input.
- The Bedrock agent (via agent and alias IDs) processes it.
- The response is sent back and rendered in your UI.
3. Benefits of this Method
- Rapid prototyping: Streamlit lets you spin up a working UI in minutes.
- API-first approach: Everything leverages AWS services (Bedrock, IAM), making it cloud-native and scalable.
- Modular and safe: Using aliases lets you iterate on your agent safely, while scoped IAM users keep your environment secure.
4. What’s Next?
In the next part of the series, the video teaches: multi-agent orchestration using Bedrock’s orchestrator, supervisor agents, and multi-agent collaboration features. That means your chatbot could soon involve multiple agents cooperating to answer complex queries!
Additional Resources
📺 Watch the full series at the Armizone youtube channel:
Full Source Code:
app.py
import streamlit as st
import boto3
import uuid
# ---------- AWS Bedrock Runtime Client ----------
bedrock_agent_runtime = boto3.client(
service_name="bedrock-agent-runtime",
region_name="us-east-1" # Change based on your agent's region
)
# ---------- Agent Config ----------
AGENT_ID = "YOUR_AGENT_ID"
AGENT_ALIAS_ID = "YOUR_AGENT_ALIAS_ID"
SESSION_ID = str(uuid.uuid4()) # New session for each run
# ---------- Streamlit UI ----------
st.set_page_config(page_title="Amazon Bedrock Agent Chat")
st.title("🤖 Chat with Amazon Bedrock Agent")
st.markdown("Ask me anything from my Knowledge Base!")
user_input = st.text_input("Your question:", placeholder="Ask a question...")
if st.button("Send") and user_input.strip():
try:
# Create an empty placeholder for streaming text
output_area = st.empty()
streamed_text = ""
# Invoke agent with streaming response
response = bedrock_agent_runtime.invoke_agent(
agentId=AGENT_ID,
agentAliasId=AGENT_ALIAS_ID,
sessionId=SESSION_ID,
inputText=user_input
)
for event in response["completion"]:
if "chunk" in event:
chunk_text = event["chunk"]["bytes"].decode("utf-8")
streamed_text += chunk_text
output_area.markdown(f"**Agent:** {streamed_text}▌")
# Final update (remove cursor)
output_area.markdown(f"**Agent:** {streamed_text}")
except Exception as e:
st.error(f"Error: {e}")
How to create an AI Chatbot using Amazon Bedrock Agents and Streamlit