
Stop Building Chatbots: Build Your First AI Agent on AWS with Amazon Bedrock and Lambda
Chatbots answer questions, but agents get work done. Learn how to build a small serverless agent on AWS that reasons, calls tools, and takes action, with a working code example and the guardrails that keep it safe.
Stop Building Chatbots: Build Your First AI Agent on AWS
Most generative AI demos follow the same script: a text box, a prompt, a clever answer. Useful, but it stops at talking.
AI agents are different. You give them a goal, and they decide which steps to take, call your tools or APIs, check the results, and keep going until the job is done. That shift from "answering" to "acting" is why agentic AI is the hottest topic in cloud development right now.
The good news: you don't need a huge platform to start. A model on Amazon Bedrock plus a few AWS Lambda functions as tools is enough for a working agent.
Chatbot vs. Agent
| Chatbot | Agent |
|---|---|---|
Output | Text answers | Actions and outcomes |
Data access | Whatever is in the prompt | Live data via tools and APIs |
Flow | One request, one response | A loop: think, act, observe, repeat |
Risk | Wrong answer | Wrong action, so it needs guardrails |
The Architecture
The whole idea is one loop:
- The user sends a goal ("Why did my orders drop yesterday?").
- The model decides it needs data and requests a tool call.
- Your code (Lambda) runs the tool and returns the result.
- The model reads the result and either calls another tool or gives the final answer.
Nothing exotic: Bedrock for reasoning, Lambda for tools, IAM for permissions, CloudWatch for visibility.
Step 1: Define a Tool
A tool is just a function plus a description the model can read. Here's a simple one that looks up an order status:
def get_order_status(order_id: str) -> dict: # Replace with a real DynamoDB or API lookup return {"order_id": order_id, "status": "SHIPPED", "eta_days": 2}Step 2: Let the Model Use It
The Bedrock Converse API supports tool use directly:
import boto3, json
bedrock = boto3.client("bedrock-runtime")MODEL_ID = "YOUR_MODEL_ID" # choose a tool-capable model enabled in your account
tool_config = { "tools": [{ "toolSpec": { "name": "get_order_status", "description": "Look up the shipping status of an order by its ID.", "inputSchema": {"json": { "type": "object", "properties": {"order_id": {"type": "string"}}, "required": ["order_id"] }} } }]}
messages = [{"role": "user", "content": [{"text": "Where is order A-1042?"}]}]
for _ in range(5): # hard cap on loop iterations response = bedrock.converse(modelId=MODEL_ID, messages=messages, toolConfig=tool_config) message = response["output"]["message"] messages.append(message)
if response["stopReason"] != "tool_use": print(message["content"][0]["text"]) # final answer break
results = [] for block in message["content"]: if "toolUse" in block: call = block["toolUse"] output = get_order_status(**call["input"]) results.append({"toolResult": { "toolUseId": call["toolUseId"], "content": [{"json": output}] }}) messages.append({"role": "user", "content": results})That
for loop is the agent. Everything else is refinement.Step 3: Add Guardrails (Don't Skip This)
An agent that can act can also act wrongly. Build safety in from day one:
- Least privilege: give each tool its own narrow IAM role. A read-only tool should never have write access.
- Cap the loop: always limit iterations (like
range(5)above) so a confused agent can't spin forever and run up costs.
- Human approval for risky actions: refunds, deletions, and emails should require confirmation.
- Validate tool inputs: never trust model output blindly. Check types and ranges before executing.
- Use Amazon Bedrock Guardrails to filter harmful content and sensitive data like PII.
- Log everything: send every tool call and result to CloudWatch so you can audit what the agent did and why.
Step 4: Deploy Serverless
Wrap the loop in a Lambda function behind API Gateway (or a Lambda function URL). You pay only when the agent runs, it scales to zero, and each tool can be its own Lambda with its own permissions.
When you outgrow hand-rolled loops, look at managed options such as Amazon Bedrock Agents and Amazon Bedrock AgentCore for memory, identity, and observability. Starting with the raw loop first helps you understand what those services are doing for you.
Lessons From Building Agents
- Tool descriptions matter more than prompts. Clear, specific descriptions dramatically improve tool selection.
- Keep tools small and single-purpose. Five focused tools beat one "do everything" tool.
- Start read-only. Earn trust before giving an agent write access.
- Test with adversarial inputs. Ask it things it shouldn't do and see what happens.
- Watch cost and latency. Every loop iteration is another model call.
What to Build Next
- Add a second tool (inventory lookup, ticket creation) and watch the agent chain them.
- Store conversation state in DynamoDB for multi-turn memory.
- Add retrieval from your own documents with Knowledge Bases for Amazon Bedrock.
- Put an approval step (Step Functions or SNS) in front of any action that changes data.
Wrapping Up
Agents aren't magic. They're a model, a loop, and tools you control. Start small, keep permissions tight, and log everything. Once you've built one that works, you'll see opportunities for them everywhere in your own workflows.
What would you automate first with an agent? Share it in the comments.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article