This project sets up an Amazon Bedrock agent with an action group that translates natural language to SQL queries. It uses Amazon Redshift Serverless for data storage and querying, with a Streamlit frontend for user interaction.
- An active AWS Account
- Familiarity with AWS services like Amazon Bedrock, Amazon S3, AWS Lambda, Amazon Redshift Serverless, and Amazon EC2
- Access granted to the Anthropic: Claude 3 Sonnet model from the Amazon Bedrock console
- Create an SSH key pair
- Create a Redshift Serverless workgroup
- Load sample data into Redshift Serverless
- Prepare the Lambda function code (if modifications are needed)
- Update the Streamlit app credentials
- Create an S3 bucket and upload project files
- Create CloudFormation stack
- Access the Streamlit application
- Navigate to the EC2 console in your preferred region
- Go to "Network & Security" > "Key Pairs"
- Click "Create Key Pair"
- Name your key pair and download the .pem file
- Open the Amazon Redshift console
- Navigate to "Serverless dashboard" > "Workgroups"
- Click "Create workgroup"
- Follow the wizard to set up your workgroup, noting the name for later use
- Connect to your Redshift Serverless instance
- Use the Redshift query editor or a SQL client to load sample data (Specific instructions for loading data will depend on your dataset)
If you need to modify the Lambda function:
- Navigate to
Setup-Amazon-Bedrock-Agent-for-Text2SQL-Using-Amazon-Redshift-Serverless-with-Streamlit/function/
- Make your changes to
lambda_function.py
and/orredshift_serverless_functions.py
- Zip both files together:
zip -j lambda_function.zip lambda_function.py redshift_serverless_functions.py
- Open
Setup-Amazon-Bedrock-Agent-for-Text2SQL-Using-Amazon-Redshift-Serverless-with-Streamlit/streamlit_app/credentials.json
- Add or modify user credentials as needed for frontend access
- Also ACL mapping is hard-coded in the AWS Lambda function to illustrate ACL logic. For production system, this would require Authorization logic.
- Create a new S3 bucket in your preferred region
- Upload the entire
Setup-Amazon-Bedrock-Agent-for-Text2SQL-Using-Amazon-Redshift-Serverless-with-Streamlit/
folder to the root of this bucket using the following AWS CLI command:Replacecd Setup-Amazon-Bedrock-Agent-for-Text2SQL-Using-Amazon-Redshift-Serverless-with-Streamlit/ aws s3 sync . s3://your-bucket-name/Setup-Amazon-Bedrock-Agent-for-Text2SQL-Using-Amazon-Redshift-Serverless-with-Streamlit/
your-bucket-name
with the name of the S3 bucket you created.
- Open the CloudFormation console
- Click "Create stack" > "With new resources (standard)"
- Upload the template file:
Setup-Amazon-Bedrock-Agent-for-Text2SQL-Using-Amazon-Redshift-Serverless-with-Streamlit/cloudformation-template/ec2-streamlit-template.yaml
- Fill in the required parameters:
- InstanceType: Choose an appropriate EC2 instance type
- KeyPair: Select the SSH key pair created in Step 1
- AgentName: Name for your Bedrock Agent
- RedshiftWorkgroupName: Name of the Redshift Serverless workgroup created in Step 2
- S3BucketName: Name of the S3 bucket created in Step 6
- AllowedIP: Your public IP address for SSH access to the EC2 instance
- Review and create the stack
- Once the CloudFormation stack is complete, find the EC2 instance public IP in the Outputs
- Open a web browser and navigate to
http://<EC2-Public-IP>:8501
If you need to restart the Streamlit application:
- Connect to your EC2 instance via SSH:
ssh -i /path/to/your-key.pem ec2-user@<EC2-Public-IP>
- Find the running Streamlit process:
ps aux | grep streamlit
- Note the process ID (PID) of the Streamlit application
- Kill the process:
kill <PID>
- Navigate to the application directory:
cd /home/ubuntu/app/streamlit_app
- Restart the Streamlit application:
nohup streamlit run app.py &
- Exit the SSH session:
exit
- The Streamlit application should now be restarted and accessible at
http://<EC2-Public-IP>:8501
- Open the AWS Lambda console
- Select your function
- Go to the "Configuration" tab
- Click on "Environment variables"
Key environment variables to check:
REDSHIFT_WORKGROUP_NAME
: The name of your Redshift Serverless workgroup
-
SSH into your EC2 instance
-
Check the environment variables:
cat /etc/profile.d/bedrock_env.sh
This will show you the following environment variables:
AGENT_ID
: The ID of your Bedrock agentAGENT_ALIAS_ID
: The alias ID of your Bedrock agentAWS_REGION
: The AWS region where your resources are deployed
You can also check if these variables are set in your current session:
env | grep AGENT env | grep AWS_REGION
If these variables are not set or have incorrect values, you may need to source the environment file or restart your EC2 instance:
source /etc/profile.d/bedrock_env.sh
These environment variables are crucial for the Streamlit application to interact with the Bedrock agent correctly. If you're experiencing issues with the application, verifying these variables is a good first step in troubleshooting.
If you encounter issues with the application, follow these steps to diagnose and potentially resolve the problem:
- Open the AWS EC2 console
- Locate your instance and check its status
- Ensure it's in a "running" state with all status checks passed
- SSH into your EC2 instance
- Check the environment variables:
cat /etc/profile.d/bedrock_env.sh
- Ensure
AGENT_ID
,AGENT_ALIAS_ID
, andAWS_REGION
are set correctly - If not, source the file:
source /etc/profile.d/bedrock_env.sh
- SSH into your EC2 instance
- View the user data execution log:
sudo cat /var/log/cloud-init-output.log
- Look for any error messages or failures during the initial setup
- SSH into your EC2 instance
- View the Streamlit log:
cat /home/ubuntu/app/streamlit_app/streamlit.log
- Look for any error messages or warnings
- Open the AWS Lambda console
- Select your function
- Check the "Configuration" tab for correct environment variables
- Review recent invocations in the "Monitor" tab for any errors
- Open the Amazon Bedrock console
- Go to "Agents" and select your agent
- Verify the agent's status is "Active"
- Check the associated Lambda function is correct
- Open the CloudWatch console
- Navigate to "Log groups"
- Check logs for:
- EC2 instance:
/var/log/cloud-init-output.log
- Lambda function:
/aws/lambda/<YourFunctionName>
- Bedrock agent:
/aws/bedrock/agents/<YourAgentName>
- EC2 instance:
- Open the Amazon Redshift console in the AWS Management Console
- In the navigation pane, choose "Query editor v2"
- If prompted, select your Redshift Serverless workgroup
- Try running a simple query, such as:
SELECT current_date;
- If the query fails, check:
- The status of your Redshift Serverless workgroup
- VPC and security group configurations
- IAM permissions for your user/role
This method is more appropriate for Redshift Serverless and doesn't require direct SSH access to the EC2 instance. It also aligns better with AWS best practices for serverless database access.
Let's also add a note about checking the Redshift Serverless workgroup status:
- Open the Amazon Redshift console
- In the navigation pane, choose "Serverless dashboard"
- Select your workgroup
- Check the status - it should be "Available"
- If not available, check the recent events for any errors or configuration issues
If all else fails, try restarting the Streamlit application:
- SSH into your EC2 instance
- Run the restart script:
/home/ubuntu/app/restart_streamlit.sh
- Open the CloudFormation console
- Select your stack
- Go to the "Events" tab
- Look for any failed resource creations or updates
If you're still experiencing issues after following these steps, you may need to review the CloudFormation template for any misconfigurations or consult AWS support for further assistance.
To avoid unnecessary charges, remember to delete the following resources when you're done:
- CloudFormation stack
- S3 bucket
- Redshift Serverless workgroup
- SSH key pair (if no longer needed)
See CONTRIBUTING for more information.
This project is licensed under the MIT-0 License. See the LICENSE file for details.