Skip to content

Commit dd7e665

Browse files
authored
Merge pull request #2670 from MakendranG/makendrang-feature-lambda-translate-tf
New serverless pattern - Text translation with Lambda and Translate (Terraform)
2 parents c8fffd7 + 7745179 commit dd7e665

File tree

8 files changed

+311
-0
lines changed

8 files changed

+311
-0
lines changed

lambda-translate-tf/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Translate Text in real-time using AWS Lambda and Amazon Translate
2+
3+
This pattern contains source code and supporting files for a serverless application deployable with Terraform. The pattern demonstrates how to create an AWS Lambda function that integrates with Amazon Translate to perform real-time text translation between languages.
4+
5+
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/lambda-translate-tf
6+
7+
Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
8+
9+
## Requirements
10+
11+
* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
12+
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
13+
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
14+
* [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started) installed
15+
16+
## Deployment Instructions
17+
18+
1. Create a new directory, navigate to that directory in a terminal and clone the repository:
19+
```
20+
git clone https://github.com/aws-samples/serverless-patterns
21+
```
22+
2. Change directory to the pattern directory:
23+
```
24+
cd serverless-patterns/lambda-translate-tf
25+
```
26+
3. From the command line, initialize Terraform to downloads and install the providers defined in the configuration:
27+
```
28+
terraform init
29+
```
30+
4. From the command line, apply the configuration in the main.tf file:
31+
```
32+
terraform apply
33+
```
34+
5. Note the outputs from the deployment process. These contain the resource names and/or ARNs which are used for testing.
35+
36+
## How it works
37+
38+
The pattern uses an AWS Lambda function that integrates with Amazon Translate to perform real-time text translation. When invoked via the AWS CLI, the Lambda function takes three parameters (source text, source language, and target language) and uses Amazon Translate to convert the text from one language to another (for example English to German).
39+
40+
"The example demonstrates translation from English to German language. For a complete list of supported languages, please refer to the [Amazon Translate documentation](https://docs.aws.amazon.com/translate/latest/dg/what-is-languages.html)."
41+
42+
43+
## Testing
44+
45+
Replace "{LambdaFunctionName}" with the function name as seen in the output of terraform template
46+
47+
```
48+
aws lambda invoke --function-name {LambdaFunctionName} --invocation-type RequestResponse --cli-binary-format raw-in-base64-out --payload "{\"text\":\"I am very happy\", \"sl\":\"en\",\"tl\":\"de\"}" response.json
49+
```
50+
51+
After running the command, open the generated response.json file to see the translated output:
52+
```
53+
Ich freue mich sehr
54+
```
55+
56+
## Cleanup
57+
58+
1. Delete all created resources by Terraform
59+
```bash
60+
terraform destroy
61+
```
62+
2. Confirm all created resources has been deleted
63+
```bash
64+
terraform show
65+
```
66+
----
67+
Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
68+
69+
SPDX-License-Identifier: MIT-0
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"title": "Text translation with AWS Lambda and Amazon Translate",
3+
"description": "Create an AWS Lambda function that calls Amazon Translate for text translation via the Boto3 SDK.",
4+
"language": "python",
5+
"level": "200",
6+
"framework": "Terraform",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This pattern shows how to deploy a Terraform template that creates a Lambda function which calls Amazon Translate for text translation."
11+
]
12+
},
13+
"gitHub": {
14+
"template": {
15+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/lambda-translate-tf",
16+
"templateURL": "serverless-patterns/lambda-translate-tf",
17+
"projectFolder": "lambda-translate-tf",
18+
"templateFile": "main.tf"
19+
}
20+
},
21+
"resources": {
22+
"bullets": [
23+
{
24+
"text": "Translation processing modes",
25+
"link": "https://docs.aws.amazon.com/translate/latest/dg/processing.html"
26+
},
27+
{
28+
"text": "Lambda",
29+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/welcome.html"
30+
}
31+
]
32+
},
33+
"deploy": {
34+
"text": [
35+
"terraform init",
36+
"terraform apply"
37+
]
38+
},
39+
"testing": {
40+
"text": [
41+
"See the Github repo for detailed testing instructions."
42+
]
43+
},
44+
"cleanup": {
45+
"text": [
46+
"<code>terraform destroy</code>",
47+
"<code>terraform show</code>"
48+
]
49+
},
50+
"authors": [
51+
{
52+
"name": "Makendran G",
53+
"image": "https://drive.google.com/file/d/1mUObnbmn52UWL-Zn39EpgpneiBNv3LCN/view?usp=sharing",
54+
"bio": "Cloud Support Engineer @ AWS",
55+
"linkedin": "makendran",
56+
"twitter": "@MakendranG"
57+
}
58+
]
59+
}

lambda-translate-tf/iam.tf

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Get current AWS account ID
2+
data "aws_caller_identity" "current" {}
3+
4+
# IAM role for Lambda
5+
resource "aws_iam_role" "lambda_role" {
6+
name = "translate_text_lambda_role"
7+
8+
assume_role_policy = jsonencode({
9+
Version = "2012-10-17"
10+
Statement = [
11+
{
12+
Action = "sts:AssumeRole"
13+
Effect = "Allow"
14+
Principal = {
15+
Service = "lambda.amazonaws.com"
16+
}
17+
}
18+
]
19+
})
20+
}
21+
22+
# IAM policy for the Lambda role
23+
resource "aws_iam_role_policy" "lambda_policy" {
24+
name = "translate_text_lambda_policy"
25+
role = aws_iam_role.lambda_role.id
26+
27+
policy = jsonencode({
28+
Version = "2012-10-17"
29+
Statement = [
30+
{
31+
Sid = "LambdatranslateText"
32+
Effect = "Allow"
33+
Action = ["translate:TranslateText"]
34+
Resource = "arn:aws:translate:${var.aws_region}:${data.aws_caller_identity.current.account_id}:*"
35+
}
36+
]
37+
})
38+
}
39+
40+
# Attach basic Lambda execution role
41+
resource "aws_iam_role_policy_attachment" "lambda_basic_execution" {
42+
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
43+
role = aws_iam_role.lambda_role.name
44+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"title": "Text translation with AWS Lambda and Amazon Translate",
3+
"description": "Create an AWS Lambda function that calls Amazon Translate for text translation via the Boto3 SDK.",
4+
"language": "Python",
5+
"level": "200",
6+
"framework": "Terraform",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This pattern shows how to deploy a Terraform template that creates a Lambda function which calls Amazon Translate for text translation."
11+
]
12+
},
13+
"gitHub": {
14+
"template": {
15+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/lambda-translate-tf",
16+
"templateURL": "serverless-patterns/lambda-translate-tf",
17+
"projectFolder": "lambda-translate-tf",
18+
"templateFile": "main.tf"
19+
}
20+
},
21+
"resources": {
22+
"bullets": [
23+
{
24+
"text": "Translation processing modes",
25+
"link": "https://docs.aws.amazon.com/translate/latest/dg/processing.html"
26+
},
27+
{
28+
"text": "Lambda",
29+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/welcome.html"
30+
}
31+
]
32+
},
33+
"deploy": {
34+
"text": [
35+
"terraform init",
36+
"terraform apply"
37+
]
38+
},
39+
"testing": {
40+
"text": [
41+
"See the Github repo for detailed testing instructions."
42+
]
43+
},
44+
"cleanup": {
45+
"text": [
46+
"<code>terraform destroy</code>",
47+
"<code>terraform show</code>"
48+
]
49+
},
50+
"authors": [
51+
{
52+
"name": "Makendran G",
53+
"image": "https://drive.google.com/file/d/1mUObnbmn52UWL-Zn39EpgpneiBNv3LCN/view?usp=sharing",
54+
"bio": "Cloud Support Engineer @ AWS",
55+
"linkedin": "makendran",
56+
"twitter": "@MakendranG"
57+
}
58+
],
59+
"patternArch": {
60+
"icon1": {
61+
"x": 20,
62+
"y": 50,
63+
"service": "lambda",
64+
"label": "AWS Lambda"
65+
},
66+
"icon2": {
67+
"x": 80,
68+
"y": 50,
69+
"service": "translate",
70+
"label": "Amazon Translate"
71+
},
72+
"line1": {
73+
"from": "icon1",
74+
"to": "icon2",
75+
"label": ""
76+
}
77+
}
78+
}

lambda-translate-tf/main.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
provider "aws" {
2+
region = var.aws_region
3+
}
4+
5+
# Archive the Python source code
6+
data "archive_file" "lambda_zip" {
7+
type = "zip"
8+
source_dir = "${path.module}/src"
9+
output_path = "${path.module}/lambda_function.zip"
10+
}
11+
12+
# Create Lambda function
13+
resource "aws_lambda_function" "translate_text" {
14+
filename = data.archive_file.lambda_zip.output_path
15+
function_name = var.function_name
16+
role = aws_iam_role.lambda_role.arn
17+
handler = "index.lambda_handler"
18+
runtime = "python3.13"
19+
memory_size = 128
20+
timeout = 10
21+
22+
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
23+
}

lambda-translate-tf/outputs.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "lambda_function_name" {
2+
description = "Lambda Function Name"
3+
value = aws_lambda_function.translate_text.function_name
4+
}
5+
6+
output "lambda_function_arn" {
7+
description = "Lambda Function ARN"
8+
value = aws_lambda_function.translate_text.arn
9+
}

lambda-translate-tf/src/index.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import boto3
2+
import json
3+
4+
client = boto3.client('translate')
5+
6+
def lambda_handler(event, context):
7+
text = event['text']
8+
source_language = event['sl']
9+
target_language = event['tl']
10+
11+
response = client.translate_text(
12+
Text=text,
13+
SourceLanguageCode=source_language,
14+
TargetLanguageCode=target_language
15+
)
16+
17+
# Return only the translated text
18+
return response['TranslatedText']

lambda-translate-tf/variables.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
variable "aws_region" {
2+
description = "AWS region"
3+
type = string
4+
default = "us-east-1"
5+
}
6+
7+
variable "function_name" {
8+
description = "Name of the Lambda function"
9+
type = string
10+
default = "TranslateTextFunction"
11+
}

0 commit comments

Comments
 (0)