How API Gateway routing works
When you create a custom REST API in API Gateway you receive an execute-api endpoint such as:summarize and sentiment resources, their full endpoints become:
Example pattern: one Lambda per resource/method
In this example bothsummarize and sentiment implement the GET method and each is mapped to a dedicated Lambda function:
- Summarize Lambda: constructs a prompt and calls a Bedrock foundation model to produce a concise summary.
- Sentiment Lambda: constructs a prompt and calls a Bedrock foundation model to return sentiment (positive / negative / neutral).
- Client calls API Gateway endpoint.
- API Gateway routes the request to the configured Lambda.
- Lambda constructs a prompt, calls Bedrock, and returns the model response.
- API Gateway returns the response to the client.
When deciding between multiple Lambdas vs. a single Lambda with internal routing, favor multiple Lambdas for clearer separation of concerns and smaller, safer deployments. Use a single Lambda only if you need a consolidated codebase and are willing to add internal routing logic.
Event-driven vs API-based on-demand
The two common architectural patterns for invoking Bedrock are:- Event-driven (asynchronous): Infrastructure events (for example, S3 object-created notifications) trigger a Lambda that reads the object, builds a prompt, calls Bedrock, and stores or forwards the result.
- API-based on-demand (synchronous): A user or client calls an API Gateway endpoint; API Gateway routes the request to Lambda, which calls Bedrock and returns the response to the client.

Both patterns ultimately call Bedrock, but they differ in latency expectations, error handling, and operational considerations.
Benefits of exposing Bedrock via API Gateway + Lambda
- Faster integration into existing systems: Consumers call a single REST API to access generative features.
- Reuse: Front ends, mobile apps, and third-party services share the same backend logic.
- Lower maintenance overhead: Fixes and model tuning happen server-side and benefit all clients.
- Interface flexibility: Expose simple REST routes (summarize, sentiment, classify) while encapsulating model details.

Key implementation notes (practical tips)
- Use structured request and response schemas (JSON) so clients know the expected payload shape.
- Choose the right API Gateway integration:
- Lambda proxy integration forwards the full HTTP request to Lambda (headers, path, query string, body) — implement parsing in Lambda.
- Mapped integration lets API Gateway transform requests/responses before they reach the Lambda.
- Keep configuration and secrets out of client code. Use Lambda environment variables, AWS Secrets Manager, or a configuration service for model IDs and sensitive parameters.
- Monitor costs and latency. Foundation models can vary in performance and cost — track usage with Amazon CloudWatch and use AWS X-Ray to trace end-to-end latency.
- Implement retries and graceful error handling in Lambda to handle transient model invocation failures.
- Secure your API Gateway endpoints with authentication and authorization: API keys, IAM, Amazon Cognito, or custom authorizers.
Protect your generative endpoints. Enforce authentication/authorization, validate and sanitize inputs to reduce prompt injection risks, and limit the rate of requests to control costs.
Quick checklist before production
- Define clear input/output JSON contracts for each route.
- Store model identifiers and invocation parameters in a secure configuration store.
- Apply least-privilege IAM roles to Lambdas and API Gateway.
- Set up CloudWatch alarms and X-Ray tracing for observability.
- Add request throttling and quotas in API Gateway to avoid runaway costs.
Takeaway
Using API Gateway and Lambda to expose Amazon Bedrock capabilities gives you a familiar, scalable pattern for adding generative AI to applications. The API acts as the stable contract for clients, while Lambda centralizes prompt construction, model selection, and response handling. This approach simplifies integration, promotes reuse, and reduces the client-side complexity of working directly with foundation models. This completes the short introduction to using Amazon API Gateway with Lambda to call Amazon Bedrock. You can combine Bedrock with Amazon Lex or other AWS services to build conversational or hybrid generative experiences.Links and references
- Amazon API Gateway documentation
- AWS Lambda documentation
- Amazon Bedrock documentation
- Amazon S3 documentation
- AWS Secrets Manager
- Amazon CloudWatch
- AWS X-Ray
- Amazon Cognito