AWS Logo
Menu
Language detection using Lambda, Comprehend, API Gateway

Language detection using Lambda, Comprehend, API Gateway

language detection: Lambda+Comprehend+API Gateway

Published Feb 5, 2025

Introduction

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.
___________

Prerequisites

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
___________

Step 1: Create an AWS Lambda Function

1.1 Navigate to AWS Lambda

  1. Go to the AWS Management Console.
  2. Navigate to Lambda.
  3. Click Create Function.
  4. Choose Author from scratch.
  5. Enter a function name, e.g., LanguageDetector.
  6. Select Python 3.x as the runtime.
  7. Choose an existing execution role or create a new one (we will update permissions later).
  8. Click Create Function.
___________

1.2 Add Code to the Lambda Function

Once the function is created, replace the default code with the following Python script:

1.3 Deploy the Function

Click Deploy to save and apply the changes.

Step 2: Update IAM Role Permissions

Your Lambda function needs permissions to use Amazon Comprehend. Update the IAM role assigned to the Lambda function by adding the following policy:

2.1 Open IAM Roles

  1. Go to IAM in the AWS Management Console.
  2. Click Roles and select the role assigned to your Lambda function.
  3. Click Attach Policies and select AmazonComprehendFullAccess.
  4. If you prefer a minimal policy, use the following JSON policy instead:
  1. Click Save Changes.

Step 3: Expose Lambda via API Gateway

3.1 Create an API Gateway

  1. Go to AWS Lambda → Click "Add trigger"
  2. Choose API Gateway → Select "Create an API".
  3. Choose HTTP API (recommended for serverless apps)
  4. Enable CORS and Deploy
  5. Copy the API Endpoint URL
  1. Go to your API
  2. Under Routes, set up a new POST method for /detect.
  3. Click Next.
  4. Deploy the API.

Step 4: Enable CORS for API Gateway

To allow requests from external clients, configure CORS.
  1. In API Gateway, go to Routes.
  2. Select the POST /detect route.
  3. Click on CORS.
  4. Configure it as follows:
  1. Click Save Changes.

Step 5: Deploy and Test the API

5.1 Deploy the API

  1. Click on Deploy API.
  2. Copy the Invoke URL from the API Gateway console.

5.2 Test with Postman

  1. Open Postman.
  2. Create a POST request to https://your-api-id.execute-api.us-east-1.amazonaws.com/detect.
  3. Go to Body and select raw JSON.
  4. Enter the following JSON payload:
  1. Click Send.
  2. You should receive a response like:

Conclusion

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! 🚀
 

Comments