
Language detection using Lambda, Comprehend, API Gateway
language detection: Lambda+Comprehend+API Gateway
Published Feb 5, 2025
Language detection is an essential feature for many applications that work with multilingual text. AWS Comprehend provides a simple way to detect the dominant language of a given text. In this blog, we'll walk through building a serverless language detector using AWS Lambda and exposing it via API Gateway.
___________
Before we start, make sure you have:
- An AWS account
- AWS CLI installed and configured
- Python 3.x installed locally
- Postman or any REST client for testing
___________
- Go to the AWS Management Console.
- Navigate to Lambda.
- Click Create Function.
- Choose Author from scratch.
- Enter a function name, e.g.,
LanguageDetector
. - Select Python 3.x as the runtime.
- Choose an existing execution role or create a new one (we will update permissions later).
- Click Create Function.
___________
Once the function is created, replace the default code with the following Python script:
Click Deploy to save and apply the changes.

Your Lambda function needs permissions to use Amazon Comprehend. Update the IAM role assigned to the Lambda function by adding the following policy:
- Go to IAM in the AWS Management Console.
- Click Roles and select the role assigned to your Lambda function.
- Click Attach Policies and select AmazonComprehendFullAccess.
- If you prefer a minimal policy, use the following JSON policy instead:

- Click Save Changes.
- Go to AWS Lambda → Click "Add trigger"
- Choose API Gateway → Select "Create an API".
- Choose HTTP API (recommended for serverless apps)
- Enable CORS and Deploy
- Copy the API Endpoint URL

- Go to your API
- Under Routes, set up a new POST method for
/detect
. - Click Next.
- Deploy the API.
To allow requests from external clients, configure CORS.
- In API Gateway, go to Routes.
- Select the POST /detect route.
- Click on CORS.
- Configure it as follows:
- Click Save Changes.
- Click on Deploy API.
- Copy the Invoke URL from the API Gateway console.
- Open Postman.
- Create a POST request to
https://your-api-id.execute-api.us-east-1.amazonaws.com/detect
. - Go to Body and select raw JSON.
- Enter the following JSON payload:
- Click Send.
- You should receive a response like:

Congrats 🥳 You have successfully built a language detection service using AWS Lambda and API Gateway. This serverless approach allows you to detect languages efficiently without managing any infrastructure. You can now integrate this API with any frontend or client applications.
Happy Cloud Learning! 🚀