Tools
Building a Production-Ready Text-to-Text API with AWS Bedrock, Lambda & API Gateway
2025-12-31
0 views
admin
Building a Production-Ready Text-to-Text API with AWS Bedrock, Lambda & API Gateway ## Project Overview ## Business Use Case ## Architecture Overview ## ๐ ๏ธ Tools & Services Used ## ๐น AWS Bedrock ## ๐น Amazon Titan Text (amazon.titan-text-express-v1) ## ๐น AWS Lambda ## ๐น Amazon API Gateway ## ๐น Python (Boto3) ## ๐ง Why This Design Matters ## ๐งฉ AWS Lambda: Text-to-Text Processing Logic ## ๐ Required IAM Permissions ## ๐ API Gateway Request Example ## ๐ง Why This Lambda Design Matters ## ๐ฆ Example Use Cases This project demonstrates how to design and deploy a production-ready text-to-text AI API using AWS Bedrock and Amazon Titan Text, exposed securely via Amazon API Gateway and powered by AWS Lambda. The goal is to show how organizations can integrate Generative AI capabilities into real business systems while maintaining security, scalability, cost control, and observability. Many organizations want to leverage Generative AI for: However, directly exposing foundation models to applications can introduce security, cost, and governance risks. This project solves that by:
Abstracting the foundation model behind a controlled API
Enforcing consistent prompts and parameters
Centralizing access, logging, and cost management
The result is a secure AI service layer that can be reused across multiple teams and applications. Client sends text input to an API endpoint API Gateway validates and routes the request Lambda processes the request and invokes AWS Bedrock Amazon Titan generates a text response The response is returned to the client Fully managed service for accessing foundation models No infrastructure to manage Enterprise-grade security Fast, cost-efficient text generation model Ideal for text-to-text use cases Deterministic behavior with low temperature Designed for enterprise workloads Serverless compute for business logic Handles request validation and AI invocation Securely exposes the AI service as a REST API Enables authentication, throttling, and monitoring Acts as the public interface for applications AWS SDK for invoking Bedrock Lightweight and production-friendly Stateless AI calls: Foundation models do not retain memory Explicit control: Prompts and parameters are centrally managed Security-first: IAM-controlled access to Bedrock Cost management: Token limits and model choice enforced Reusability: Multiple applications can consume the same API This mirrors how AI platforms are built in regulated and enterprise environments. Below is an example AWS Lambda function written in Python that receives text from API Gateway, invokes AWS Bedrock (Amazon Titan Text), and returns the generated response. This Lambda acts as the controlled AI service layer between your applications and the foundation model. Let's create the function Go to the management console, and search for AWS lambda. Click on Create function to open the function creation page. Enter a name for the function and choose Python as the runtime. Accept all defaults and click on Create function. Replace the code in the code editor with the code shared in the Github Repo. Increase the time out to 30 sec as shown below. The Lambda execution role must allow invoking Bedrock models:
Lambda by defaults has access to Cloudwatch for log writing. We need to grant lambda access to Bedrock and the foundation model Go to Configurations then Permissions. Click on the Role name and update with the policy below. This grants Lambda access to Bedrock and the foundation Model Let's create the API using AWS API Gateway. In the AWS API Gateway service page click on Create API. Choose REST API as the type and click on Build. In the resources page, click on Create resource. Give the resources a name and click on Create resource Click on Create Method. Choose method type as POST and Integration type as Lambda function. Check Lambda proxy integration and select the created lambda function. Click on Create method. Click on Deploy API. Select New stage and enter a Stage name. Click on Deploy. On the Stage details page, copy the Invoke URL. You can use any API client like Postman to test the API as shown below. Test using any API client. In this demonstration, I have used Postman as shown below. Keeps foundation models behind a secure API Enforces consistent parameters (temperature, token limits) Prevents direct client access to Bedrock Enables logging, monitoring, and governance This pattern is commonly used to build enterprise AI platforms. Text summarization API AI-powered content generation service Analytics explanation engine Internal AI assistant backend Secure GenAI microservice Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse CODE_BLOCK:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowInvokeTitanText", "Effect": "Allow", "Action": [ "bedrock:InvokeModel" ], "Resource": [ "arn:aws:bedrock:us-east-1::foundation-model/amazon.titan-text-express-v1" ] } ]
} Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowInvokeTitanText", "Effect": "Allow", "Action": [ "bedrock:InvokeModel" ], "Resource": [ "arn:aws:bedrock:us-east-1::foundation-model/amazon.titan-text-express-v1" ] } ]
} CODE_BLOCK:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowInvokeTitanText", "Effect": "Allow", "Action": [ "bedrock:InvokeModel" ], "Resource": [ "arn:aws:bedrock:us-east-1::foundation-model/amazon.titan-text-express-v1" ] } ]
} CODE_BLOCK:
POST https://05q0if5orb.execute-api.us-east-1.amazonaws.com/prod/text
{ "text": "what is Amazon Bedrock"
} โ
API Response Example { "response": "\nAmazon Bedrock is the name of AWSโs managed service for managing the underlying infrastructure that powers your intelligent bot. It is a collection of services that you can use to build, deploy, and scale intelligent bots at scale. Amazon Bedrock is a managed service that makes foundation models from leading AI startup and Amazonโs own Titan models available through APIs. For up-to-date information on Amazon Bedrock and how 3P models are approved, endorsed or selected please see the provided documentation and relevant FAQs."
} Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
POST https://05q0if5orb.execute-api.us-east-1.amazonaws.com/prod/text
{ "text": "what is Amazon Bedrock"
} โ
API Response Example { "response": "\nAmazon Bedrock is the name of AWSโs managed service for managing the underlying infrastructure that powers your intelligent bot. It is a collection of services that you can use to build, deploy, and scale intelligent bots at scale. Amazon Bedrock is a managed service that makes foundation models from leading AI startup and Amazonโs own Titan models available through APIs. For up-to-date information on Amazon Bedrock and how 3P models are approved, endorsed or selected please see the provided documentation and relevant FAQs."
} CODE_BLOCK:
POST https://05q0if5orb.execute-api.us-east-1.amazonaws.com/prod/text
{ "text": "what is Amazon Bedrock"
} โ
API Response Example { "response": "\nAmazon Bedrock is the name of AWSโs managed service for managing the underlying infrastructure that powers your intelligent bot. It is a collection of services that you can use to build, deploy, and scale intelligent bots at scale. Amazon Bedrock is a managed service that makes foundation models from leading AI startup and Amazonโs own Titan models available through APIs. For up-to-date information on Amazon Bedrock and how 3P models are approved, endorsed or selected please see the provided documentation and relevant FAQs."
} - Internal copilots
- Automated content generation
- Text summarization
- Data explanations
- Customer support automation - Client sends text input to an API endpoint
- API Gateway validates and routes the request
- Lambda processes the request and invokes AWS Bedrock
- Amazon Titan generates a text response
how-totutorialguidedev.toaiserverpythongitgithub