Using AWS Bedrock to Query a MySQL Database
In this blog, we will connect AWS Bedrock to our RDS MySQL database and help Bedrock understand our database structure. Then, we will ask Bedrock in plain English what we ...
Mahmood
2024-10-04

In this blog, we will connect AWS Bedrock to our RDS MySQL database and help Bedrock understand our database structure. Then, we will ask Bedrock in plain English what we need from the database, and Bedrock will write the query for us. Lambda will run the query against the database and display the results to the user.
We will need an RDS MySQL database, VPC, and a lambda function in VPC. If you don't already have a VPC, use the SimpleVPC Terraform code by going to GitHub and cloning the Simple-VPC repository. Update the variables.tf and change the ENV on line 32. The terraform code will create a VPC with six Subnets, three public and three private. It will create an IGW with three NATs.
git clone https://github.com/mahmoodr786/simple-vpc.git
cd simple-vpc
terraform init
terraform apply



Next, follow the first part of Using ElastiCache with AWS RDS to create the RDS MySQL Database.
Next, follow the Slack AI Bot with AWS Bedrock blog and do the Lambda part to create the Lambda function.
Once both are done, you can move our Lambda function into the VPC. Remember to add IAM permissions to your lambda role so it can be added to a VPC. Go to configuration, add it to a VPC, and pick the private subnets. Also, remember to allow access to the database from the lambda function using security groups.

Since we will use PyMySQL, we will need to add this to the layer or to the lambda package itself. If you don't have time to create your own layer, head to my GitHub: https://github.com/mahmoodr786/boto3-bedrock-lambda-layer. Download the bedrock-pymysql.zip and upload it as a layer to your lambda function.

Now, we will create a function URL to access our function in the browser.

Feel free to use IAM authentication or other means to protect your function. For this blog, I will be using NONE.

Access your function in the browser to see if it works, and it should.

Now, we need to connect to our database and create two tables with fake data. If you already have a database and a way to connect, go for it. If you do not, you can create an EC2 instance in one of the private subnets, use SSM to SSH to the EC2, and connect to MySQL.
apt update
apt install mysql-client
mysql -u admin -p -h bedrockdb.cluster-cifbe2qttmpd.us-east-1.rds.amazonaws.com

Now that you are connected, run the queries below to create your database tables and data.
Show databases;
use bedrock
CREATE TABLE users (
user_id INT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL
);
CREATE TABLE fruits (
id INT PRIMARY KEY,
user_id INT,
fruit_name VARCHAR(50) NOT NULL,
quantity INT,
FOREIGN KEY (user_id) REFERENCES users(user_id)
);
That should create two tables for us. Now, we need to add some data to it.
INSERT INTO users (user_id, username, email) VALUES
(1, 'john_doe', 'john@example.com'),
(2, 'jane_smith', 'jane@example.com'),
(3, 'bob_jones', 'bob@example.com'),
(4, 'alice_wonderland', 'alice@example.com');
INSERT INTO fruits (id, user_id, fruit_name, quantity) VALUES
(101, 1, 'Apple', 5),
(102, 1, 'Banana', 8),
(103, 2, 'Orange', 12),
(104, 3, 'Grapes', 20),
(105, 4, 'Strawberry', 15),
(106, 4, 'Pineapple', 7);
Mahmood
Engineer
Read More
View all posts
AI/ML
Why Enterprise AI Must Be Application-Led, Not Agent-Led
A deep dive by Todd Bernson, CTO and Chief AI Officer, on why enterprise AI systems should be architected as application-led, deterministic platforms with embedded agentic AI—not fully autonomous agents. This article explains how API-first, governed, multi-channel architectures deliver higher reliability, compliance, scalability, and business value in real-world Fortune-500 environments.

Todd Bernson
2025-12-02

AI/ML
Application-First Agentic AI
Application-first agentic AI is emerging as the only reliable path to real enterprise ROI. In this in-depth analysis, Todd Bernson, CTO & CAIO, breaks down why most generative AI initiatives stall in production—and how disciplined enterprise architecture, deterministic workflows, and narrowly scoped AI agents can finally unlock repeatable business value. Using a real sprint-intelligence system as a case study, the article shows how organizations can combine serverless engineering, structured orchestration, and constrained LLM reasoning to reduce reporting effort, increase trust, eliminate hallucinations, and deliver actionable insights across engineering, operations, compliance, and customer experience.

Todd Bernson
2025-11-28
AI/ML
Why 95% of AI Projects Fail and How to Be Among the 5% That Succeed

Lee Hylton
2025-08-22