Skip to content

Update invoke_agent.py #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cfn/3-ec2-streamlit-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ Resources:
apt-get update -y
apt-get upgrade -y
apt-get install -y python3-pip git ec2-instance-connect
git clone https://github.com/build-on-aws/bedrock-agents-streamlit.git /home/ubuntu/app
git clone https://github.com/jossai87/bedrock-agents-streamlit-1.git /home/ubuntu/app
pip3 install -r /home/ubuntu/app/streamlit_app/requirements.txt
cd /home/ubuntu/app/streamlit_app

Expand Down
22 changes: 12 additions & 10 deletions streamlit_app/invoke_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@
from botocore.credentials import Credentials
from requests import request

ssm = boto3.client('ssm')
# ---------------------------------------------------------------------
# REGION CONFIGURATION:
# ---------------------------------------------------------------------
theRegion = "us-west-2"
os.environ["AWS_REGION"] = theRegion

# Configure boto3 to use the specified region
boto3.setup_default_session(region_name=theRegion)

# Now create the SSM client with the region explicitly specified
ssm = boto3.client('ssm', region_name=theRegion)

# ---------------------------------------------------------------------
# Replace with your actual Agent ID and Alias ID below:
Expand All @@ -24,13 +34,6 @@
#agentId = ssm.get_parameter(Name='/agent-id', WithDecryption=True)['Parameter']['Value'] #valid if CFN infrastructure templates were ran
#agentAliasId = ssm.get_parameter(Name='/alias-id', WithDecryption=True)['Parameter']['Value'] #valid if CFN infrastructure templates were ran


# ---------------------------------------------------------------------
# REGION CONFIGURATION:
# ---------------------------------------------------------------------
theRegion = "us-west-2"
os.environ["AWS_REGION"] = theRegion

# ---------------------------------------------------------------------
# HELPER FUNCTION TO GET AWS CREDENTIALS SAFELY
# ---------------------------------------------------------------------
Expand All @@ -39,7 +42,7 @@ def get_frozen_credentials():
Safely obtain frozen AWS credentials from the current Boto3 Session.
Raise an error if credentials are not found to clarify what's missing.
"""
session = Session()
session = Session(region_name=theRegion)
creds = session.get_credentials()
if not creds:
raise EnvironmentError(
Expand Down Expand Up @@ -226,4 +229,3 @@ def lambda_handler(event, context):
"status_code": 500,
"body": json.dumps({"error": str(e)})
}