AWS Builder Center

The Serverless GenAI Pattern: API Gateway, Lambda, and Amazon Bedrock Explained

How a simple serverless architecture lets a web or mobile app call a foundation model securely, with no servers to manage.

Once you can call a model in a notebook, the next question is: how do I let a real app use it? A common answer on AWS is a serverless pattern using Amazon API Gateway, AWS Lambda, and Amazon Bedrock.

The architecture

Client app -> API Gateway -> Lambda -> Amazon Bedrock
|
Response back to client

What each piece does

  • Client app: a website, mobile app, or script that sends a user's question
  • Amazon API Gateway: the public front door. It exposes an HTTPS endpoint and can handle authentication, throttling, and request validation
  • AWS Lambda: runs your code when a request arrives. It builds the prompt, calls Bedrock, and returns the result
  • Amazon Bedrock: hosts the foundation model that generates the answer

Why this pattern is popular

  • No servers to manage. You deploy code, not machines.
  • Pay for use. Lambda and API Gateway charge based on usage, which suits small projects and experiments.
  • Scales automatically. More requests simply mean more function invocations.
  • Keeps secrets off the client. The app never holds your AWS credentials. Lambda uses an IAM role instead.

The role of IAM

Your Lambda function needs permission to call Bedrock. Instead of putting access keys in code, attach an IAM execution role to the function with only the permissions it needs. This follows the principle of least privilege.

What the Lambda code does

In plain steps:
  1. Read the user's message from the incoming request
  1. Optionally add instructions or retrieved context to form a prompt
  1. Call the Bedrock runtime API (the Converse API is a convenient option)
  1. Return the model's reply as the HTTP response

Things to plan for

  • Timeouts. Model calls can take time, so check Lambda and API Gateway timeout limits against your expected response time.
  • Streaming. For long answers, look into streaming responses so users see text as it is generated.
  • Input limits. Validate and cap user input length to control cost and abuse.
  • Logging. Use Amazon CloudWatch to monitor errors and latency.
  • Cost control. Add usage limits, and set budget alerts.

Where to extend it

  • Add Knowledge Bases for RAG
  • Add Guardrails for safety
  • Store conversation history in Amazon DynamoDB
  • Add user login with Amazon Cognito

Conclusion

API Gateway, Lambda, and Bedrock form a simple foundation for many GenAI backends. Understanding how the pieces connect is worth more than memorizing any single service, because the same pattern appears again and again in real projects.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article