Back

Creating a Translation Service

January 10, 2020

2 min read

This document describes how to create a translation service using AWS Services

Create an AWS Account

Go to https://aws.amazon.com/free/

Fill out the following form with your information Screen Shot 2020-01-12 at 9 28 04 PM

Part 1: Create an AWS Lambda

Screen Shot 2020-01-12 at 9 42 42 PM
import json
import boto3

def lambda_handler(event, context):
    # This creates the translation service client
    # This lambda has permission to call this service because we allowed it in IAM
    client = boto3.client(service_name='translate', region_name='us-west-2', use_ssl=True)

    # We call the client with the text we want translated 
    # Pass the source and target language
    # https://docs.aws.amazon.com/translate/latest/dg/what-is.html#what-is-languages
    response = client.translate_text(
        Text= "Hello",
        SourceLanguageCode='en',
        TargetLanguageCode='es'
        )
    
    print(json.dumps(response))

    # Return the translated text in the response
    return {
        'statusCode': 200,
        'body': json.dumps(response["TranslatedText"])
    }

Part 2: Add Permissions for Lambda to Call Translation Service

Screen Shot 2020-01-12 at 9 43 25 PM Screen Shot 2020-01-12 at 9 44 38 PM Screen Shot 2020-01-12 at 9 45 11 PM

Part 3: Create an API

Screen Shot 2020-01-12 at 10 30 45 PM Screen Shot 2020-01-12 at 10 05 26 PM

Part 5: Get Input from API

import json
import boto3

def lambda_handler(event, context):
    # This creates the translation service client
    # This lambda has permission to call this service because we allowed it in IAM
    client = boto3.client(service_name='translate', region_name='us-west-2', use_ssl=True)

    # We call the client with the text we want translated 
    # Pass the source and target language
    # https://docs.aws.amazon.com/translate/latest/dg/what-is.html#what-is-languages
    response = client.translate_text(
        Text= event["queryStringParameters"]["q"],
        SourceLanguageCode='en',
        TargetLanguageCode='es'
        )

    # Return the translated text in the response
    return {
        'statusCode': 200,
        'body': json.dumps(response["TranslatedText"])
    }

Test It!

https://YOUR_API.execute-api.us-west-2.amazonaws.com/?q=library