Serverless AI on AWS: Running Claude with Amazon Bedrock and Lambda
When a company asks me to add AI to a product, one of the first questions is where it should run. For many teams the best answer is: on the AWS account they already have. With Amazon Bedrock you call Claude through AWS, billed on your AWS invoice, under your own permissions and logs. With AWS Lambda you run the code around it without managing a server.
At Jinn Studios I run AI workloads on Bedrock, Lambda and EC2. This post is the pattern I reach for most often, with a working function you can adapt.
Why Bedrock and Lambda work well together
- No servers to look after. Lambda runs your code when a request arrives and stops when it finishes. There is nothing to patch at 2 a.m.
- You pay for use. Bedrock charges per token and Lambda per request and run time, so a tool that is used ten times a day costs very little.
- Your data stays under your account. Requests go through your AWS account with your IAM permissions, CloudTrail audit logs and region choice. AWS does not use Bedrock inputs and outputs to train models.
- It fits what you already have. The same Lambda can read from S3, write to DynamoDB, and be triggered by API Gateway, an S3 upload, an SQS queue or a schedule.
The architecture
A typical setup for an AI feature such as summarising support messages looks like this:
- API Gateway receives an HTTPS request from your app.
- Lambda checks the input, builds the prompt and calls Claude on Bedrock.
- The result goes back to the app and, if needed, into DynamoDB or S3.
- CloudWatch keeps the logs, the timings and the token counts, so you can see cost per feature.
For work that takes longer than a person wants to wait, put an SQS queue between the trigger and Lambda and process items in the background.
Step 1: Turn on the model in Bedrock
In the AWS console, open Amazon Bedrock in the region you want to use and check that the Claude model you need is enabled for your account. Not every model is available in every region, so check the Bedrock model list for your region before you design around it.
Step 2: Give Lambda only the permissions it needs
Create an IAM role for the function. It needs the basic Lambda logging permissions plus permission to invoke the Bedrock model, scoped to that model rather than to every model in the account. If the function reads or writes other services, add those one by one. Least privilege matters more with AI, because the input often comes from users.
Step 3: Write the function
Here is a Python Lambda that summarises a customer message and suggests a category. It uses the official Anthropic Python SDK, which has a client for Bedrock that signs requests with the Lambda role's AWS credentials, so there are no API keys to store.
import json
import os
from anthropic import AnthropicBedrockMantle
# Created once per container and reused across invocations.
client = AnthropicBedrockMantle(aws_region=os.environ["AWS_REGION"])
MODEL = "anthropic.claude-opus-5-5"
SYSTEM = (
"You summarise customer messages for a support team. "
"Reply with JSON only: {\"summary\": string, \"category\": "
"\"billing\" | \"delivery\" | \"technical\" | \"other\", \"urgent\": boolean}."
)
def handler(event, context):
body = json.loads(event.get("body") or "{}")
message = str(body.get("message", "")).strip()
if not message or len(message) > 20_000:
return {"statusCode": 400, "body": json.dumps({"error": "Send a message under 20,000 characters."})}
response = client.messages.create(
model=MODEL,
max_tokens=4096,
output_config={"effort": "low"}, # a short, simple task: keep it quick and cheap
system=SYSTEM,
messages=[{"role": "user", "content": message}],
)
if response.stop_reason == "refusal":
return {"statusCode": 422, "body": json.dumps({"error": "This message could not be processed."})}
text = next((b.text for b in response.content if b.type == "text"), "")
print(json.dumps({"input_tokens": response.usage.input_tokens, "output_tokens": response.usage.output_tokens}))
return {"statusCode": 200, "headers": {"Content-Type": "application/json"}, "body": text}A few details worth copying:
- The client is created outside the handler, so warm invocations reuse it.
- Input is checked before it reaches the model. Empty or huge requests are rejected for free.
- The effort is set to low. Claude decides how much to think, and for a short classification job a lower effort keeps responses fast and cheaper. Use a higher effort for harder reasoning.
- The stop reason is checked before the content is read.
- Token counts are logged as JSON, so CloudWatch Logs Insights can add them up per day or per feature.
Package the function with the anthropic library (as a Lambda layer or in the deployment package) and set the timeout well above the slowest response you expect. Thirty to sixty seconds is a sensible start for short tasks.
Step 4: Put it behind API Gateway
Create an HTTP API in API Gateway with a POST route to the function. Protect it the same way as the rest of your API: a JWT authoriser for signed-in users, or an API key and usage plan for server-to-server calls. Never leave an AI endpoint open to the internet without authentication and throttling. It is the easiest way to get a surprise bill.
Keeping costs under control
Serverless AI is cheap when it is idle and can become expensive when it is not. What I set up on every project:
- AWS Budgets alerts on the account, so a spike is noticed the same day.
- Throttling in API Gateway and reserved concurrency on the Lambda, so a loop or an attacker cannot run thousands of requests at once.
- Limits on input size and a sensible
max_tokensfor the output. - The right effort for each task. Not every request needs the deepest reasoning.
- Prompt caching when many requests share a long system prompt or reference document. The repeated part is cached and costs much less on later requests.
- Per-feature cost tracking from the token counts in the logs, so you know which feature is worth what it costs.
When to use something else
Lambda is not the right home for everything. Choose a different setup when:
- A single job runs longer than Lambda's 15-minute limit. Use Step Functions to split it up, or run it on ECS or EC2.
- You need a long-lived connection, such as streaming a chat to thousands of users at once. A small container service is often simpler.
- The AI part is one step in a multi-step business process. Then the workflow around it matters more than where it runs. I wrote about that in how to build a Claude automation workflow.
Summary
Bedrock and Lambda give you Claude on your own AWS account, with your permissions, logs and region, and no servers to manage. Start with one small, useful function, log tokens from day one, and add budgets and throttling before you announce it.
If you want help setting this up on your account, see my cloud and AWS services and AI automation services, or send me a message.