Ask Your Data in Plain English: Build a Text-to-SQL Helper with Amazon Bedrock
A beginner-friendly guide to Amazon Bedrock. Learn the key parts of the service, then build a small Python tool that turns plain-English questions into SQL, with simple safety rules.
Writing SQL takes time. Many people in a business have questions about data, but they do not know SQL. What if they could just ask in plain English?
In this article, we build a small text-to-SQL helper with Amazon Bedrock. You type a question. Bedrock writes the SQL. It takes about 30 lines of Python.
What is Amazon Bedrock?
Amazon Bedrock is a fully managed AWS service for generative AI. It gives you one API to use many foundation models from different providers, such as Anthropic, Amazon, Meta, Mistral and OpenAI.
You do not manage any servers or GPUs. You pay for what you use.
The key parts of Bedrock are:
- Models – pick the right model for each task and switch easily.
- Knowledge Bases – connect your own documents for RAG (retrieval-augmented generation).
- Guardrails – block harmful content and protect private data.
- Agents and AgentCore – build AI agents that can call tools and take actions.
Before you start
- An AWS account with permission to call
bedrock:InvokeModel.
- Python 3 and
boto3installed (pip install boto3).
- A model you can use in your Region. Check the Model catalog in the Bedrock console and copy its model ID.
Step 1: Describe your table
The model needs to know your table structure. We give it a short schema:
SCHEMA = """Table: salesColumns: order_id (string) order_date (date) region (string) product (string) amount (double)"""Step 2: Call Bedrock with the Converse API
The Converse API works the same way for every model on Bedrock. This makes it easy to try different models.
import boto3
client = boto3.client("bedrock-runtime", region_name="us-east-1")MODEL_ID = "<your-model-id>" # copy from the Bedrock model catalog
def question_to_sql(question): system_prompt = ( "You are a SQL expert. Write one Amazon Athena (Trino) SQL query " "that answers the question. Use only this schema:\n" + SCHEMA + "\nReturn only the SQL. No explanation." ) response = client.converse( modelId=MODEL_ID, system=[{"text": system_prompt}], messages=[{"role": "user", "content": [{"text": question}]}], inferenceConfig={"maxTokens": 500, "temperature": 0}, ) return response["output"]["message"]["content"][0]["text"]
print(question_to_sql("What was total revenue by region last month?"))We set
temperature to 0. This makes the answer more stable, which is what we want for SQL.Step 3: Check the result
The model will return something like this:
SELECT region, SUM(amount) AS revenueFROM salesWHERE order_date >= date_trunc('month', current_date) - INTERVAL '1' MONTH AND order_date < date_trunc('month', current_date)GROUP BY regionORDER BY revenue DESC;You can now run this query in Amazon Athena.
Make it safe for real use
AI-written SQL must be checked. Some simple rules:
- Read-only access – run queries with an IAM role that can only
SELECT. Never let it change or delete data.
- Check before running – reject anything that is not a
SELECTstatement.
- Limit results – add a
LIMITto protect cost and speed.
- Use Guardrails – Amazon Bedrock Guardrails can filter unsafe input and hide private data such as emails or phone numbers.
- Keep a human in the loop – show the SQL to the user before it runs.
Ideas to go further
- Use a Knowledge Base with your data dictionary, so the model knows what each column means.
- Build an agent that writes the SQL, runs it in Athena and explains the result in plain English.
- Compare a small, fast model with a larger one. Often the small one is good enough and much cheaper.
Summary
Amazon Bedrock makes generative AI easy to add to data work. With the Converse API, a short prompt and a few safety rules, you can help anyone in your team ask questions about data in plain English.
What would you build with Bedrock? Share your ideas in the comments!
Learn more
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article