Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions lambda-translate-tf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Translate Text in real-time using AWS Lambda and Amazon Translate

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.

Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/lambda-translate-tf

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.

## Requirements

* [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.
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
* [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started) installed

## Deployment Instructions

1. Create a new directory, navigate to that directory in a terminal and clone the repository:
```
git clone https://github.com/aws-samples/serverless-patterns
```
2. Change directory to the pattern directory:
```
cd serverless-patterns/lambda-translate-tf
```
3. From the command line, initialize Terraform to downloads and install the providers defined in the configuration:
```
terraform init
```
4. From the command line, apply the configuration in the main.tf file:
```
terraform apply
```
5. Note the outputs from the deployment process. These contain the resource names and/or ARNs which are used for testing.

## How it works

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

"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)."


## Testing

Replace "{LambdaFunctionName}" with the function name as seen in the output of terraform template

```
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
```

After running the command, open the generated response.json file to see the translated output:
```
Ich freue mich sehr
```

## Cleanup

1. Delete all created resources by Terraform
```bash
terraform destroy
```
2. Confirm all created resources has been deleted
```bash
terraform show
```
----
Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0
59 changes: 59 additions & 0 deletions lambda-translate-tf/example-pattern.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"title": "Text translation with AWS Lambda and Amazon Translate",
"description": "Create an AWS Lambda function that calls Amazon Translate for text translation via the Boto3 SDK.",
"language": "python",
"level": "200",
"framework": "Terraform",
"introBox": {
"headline": "How it works",
"text": [
"This pattern shows how to deploy a Terraform template that creates a Lambda function which calls Amazon Translate for text translation."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/lambda-translate-tf",
"templateURL": "serverless-patterns/lambda-translate-tf",
"projectFolder": "lambda-translate-tf",
"templateFile": "main.tf"
}
},
"resources": {
"bullets": [
{
"text": "Translation processing modes",
"link": "https://docs.aws.amazon.com/translate/latest/dg/processing.html"
},
{
"text": "Lambda",
"link": "https://docs.aws.amazon.com/lambda/latest/dg/welcome.html"
}
]
},
"deploy": {
"text": [
"terraform init",
"terraform apply"
]
},
"testing": {
"text": [
"See the Github repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"<code>terraform destroy</code>",
"<code>terraform show</code>"
]
},
"authors": [
{
"name": "Makendran G",
"image": "https://drive.google.com/file/d/1mUObnbmn52UWL-Zn39EpgpneiBNv3LCN/view?usp=sharing",
"bio": "Cloud Support Engineer @ AWS",
"linkedin": "makendran",
"twitter": "@MakendranG"
}
]
}
44 changes: 44 additions & 0 deletions lambda-translate-tf/iam.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Get current AWS account ID
data "aws_caller_identity" "current" {}

# IAM role for Lambda
resource "aws_iam_role" "lambda_role" {
name = "translate_text_lambda_role"

assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
}
]
})
}

# IAM policy for the Lambda role
resource "aws_iam_role_policy" "lambda_policy" {
name = "translate_text_lambda_policy"
role = aws_iam_role.lambda_role.id

policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Sid = "LambdatranslateText"
Effect = "Allow"
Action = ["translate:TranslateText"]
Resource = "arn:aws:translate:${var.aws_region}:${data.aws_caller_identity.current.account_id}:*"
}
]
})
}

# Attach basic Lambda execution role
resource "aws_iam_role_policy_attachment" "lambda_basic_execution" {
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
role = aws_iam_role.lambda_role.name
}
78 changes: 78 additions & 0 deletions lambda-translate-tf/lambda-translate-tf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"title": "Text translation with AWS Lambda and Amazon Translate",
"description": "Create an AWS Lambda function that calls Amazon Translate for text translation via the Boto3 SDK.",
"language": "Python",
"level": "200",
"framework": "Terraform",
"introBox": {
"headline": "How it works",
"text": [
"This pattern shows how to deploy a Terraform template that creates a Lambda function which calls Amazon Translate for text translation."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/lambda-translate-tf",
"templateURL": "serverless-patterns/lambda-translate-tf",
"projectFolder": "lambda-translate-tf",
"templateFile": "main.tf"
}
},
"resources": {
"bullets": [
{
"text": "Translation processing modes",
"link": "https://docs.aws.amazon.com/translate/latest/dg/processing.html"
},
{
"text": "Lambda",
"link": "https://docs.aws.amazon.com/lambda/latest/dg/welcome.html"
}
]
},
"deploy": {
"text": [
"terraform init",
"terraform apply"
]
},
"testing": {
"text": [
"See the Github repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"<code>terraform destroy</code>",
"<code>terraform show</code>"
]
},
"authors": [
{
"name": "Makendran G",
"image": "https://drive.google.com/file/d/1mUObnbmn52UWL-Zn39EpgpneiBNv3LCN/view?usp=sharing",
"bio": "Cloud Support Engineer @ AWS",
"linkedin": "makendran",
"twitter": "@MakendranG"
}
],
"patternArch": {
"icon1": {
"x": 20,
"y": 50,
"service": "lambda",
"label": "AWS Lambda"
},
"icon2": {
"x": 80,
"y": 50,
"service": "translate",
"label": "Amazon Translate"
},
"line1": {
"from": "icon1",
"to": "icon2",
"label": ""
}
}
}
23 changes: 23 additions & 0 deletions lambda-translate-tf/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
provider "aws" {
region = var.aws_region
}

# Archive the Python source code
data "archive_file" "lambda_zip" {
type = "zip"
source_dir = "${path.module}/src"
output_path = "${path.module}/lambda_function.zip"
}

# Create Lambda function
resource "aws_lambda_function" "translate_text" {
filename = data.archive_file.lambda_zip.output_path
function_name = var.function_name
role = aws_iam_role.lambda_role.arn
handler = "index.lambda_handler"
runtime = "python3.13"
memory_size = 128
timeout = 10

source_code_hash = data.archive_file.lambda_zip.output_base64sha256
}
9 changes: 9 additions & 0 deletions lambda-translate-tf/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
output "lambda_function_name" {
description = "Lambda Function Name"
value = aws_lambda_function.translate_text.function_name
}

output "lambda_function_arn" {
description = "Lambda Function ARN"
value = aws_lambda_function.translate_text.arn
}
18 changes: 18 additions & 0 deletions lambda-translate-tf/src/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import boto3
import json

client = boto3.client('translate')

def lambda_handler(event, context):
text = event['text']
source_language = event['sl']
target_language = event['tl']

response = client.translate_text(
Text=text,
SourceLanguageCode=source_language,
TargetLanguageCode=target_language
)

# Return only the translated text
return response['TranslatedText']
11 changes: 11 additions & 0 deletions lambda-translate-tf/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
variable "aws_region" {
description = "AWS region"
type = string
default = "us-east-1"
}

variable "function_name" {
description = "Name of the Lambda function"
type = string
default = "TranslateTextFunction"
}