|
| 1 | +# Amazon API Gateway REST API to AWS Lambda which calls AWS Translate and Polly services |
| 2 | + |
| 3 | +This pattern creates an Amazon API Gateway REST API and an AWS Lambda function that calls AWS Translate and Polly services and saves the audio file to S3. |
| 4 | + |
| 5 | +Learn more about this pattern at: https://serverlessland.com/patterns/apigw-lambda-translate-polly-s3. |
| 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 | +* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed |
| 15 | +* [AWS Translate](https://aws.amazon.com/translate/) |
| 16 | +* [AWS Translate Languages](https://docs.aws.amazon.com/translate/latest/dg/pairs.html) |
| 17 | +* [AWS Polly](https://aws.amazon.com/polly/) |
| 18 | +* [AWS S3](https://aws.amazon.com/s3/) |
| 19 | + |
| 20 | +## Deployment Instructions |
| 21 | + |
| 22 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 23 | + ``` |
| 24 | + git clone https://github.com/aws-samples/serverless-patterns |
| 25 | + ``` |
| 26 | +2. Change directory to the pattern directory: |
| 27 | + ``` |
| 28 | + cd apigw-lambda-translate-polly-s3 |
| 29 | + ``` |
| 30 | +3. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: |
| 31 | + ``` |
| 32 | + sam deploy --guided |
| 33 | + ``` |
| 34 | +4. During the prompts: |
| 35 | + * Enter a stack name |
| 36 | + * Enter the desired AWS Region |
| 37 | + * Enter the S3 bucket to store the audio file |
| 38 | +
|
| 39 | + Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults. |
| 40 | +
|
| 41 | +5. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs that were created. |
| 42 | +
|
| 43 | +## How it works |
| 44 | +
|
| 45 | +This pattern deploys an Amazon API Gateway REST API and a default route integrated with an AWS LAMBDA function written in Python. The lambda function calls AWS Translate service using the text and language given in the request and then calls AWS Polly service to convert the translated text into speech which is saved as a audio file in S3. The API response will have the translated text and audio file location or error response if the language code is not supported. |
| 46 | +
|
| 47 | +## Testing |
| 48 | +
|
| 49 | +Once the application is deployed, either use a curl or call the endpoint from Postman. |
| 50 | +
|
| 51 | +Example POST Request to translate text to Spanish: |
| 52 | +``` |
| 53 | + curl -X POST "https://{api-gateway-endpoint}/prod" -H 'Content-Type: application/json' -d '{"OriginalText":"Hello this is serverless","TranslateToLanguage":"es"}' |
| 54 | +``` |
| 55 | +
|
| 56 | +Response: |
| 57 | +``` |
| 58 | + "{"TranslatedText": "Hola, esto es sin servidor", "AudioFile": "s3://{yourS3bucketname}/1692129677356.mp3"}" |
| 59 | +``` |
| 60 | +
|
| 61 | +Example POST Request to translate text to Afrikaans: |
| 62 | +``` |
| 63 | + curl -X POST "https://{api-gateway-endpoint}/prod" -H 'Content-Type: application/json' -d '{"OriginalText":"Hello this is serverless","TranslateToLanguage":"af"}' |
| 64 | +``` |
| 65 | +
|
| 66 | +Response: |
| 67 | +``` |
| 68 | + "{"TranslatedText": "Hallo dit is bedienerloos", "AudioFile": "s3://{yourS3bucketname}/1692129833975.mp3"}" |
| 69 | +``` |
| 70 | +
|
| 71 | +Example POST Request with unsupported language: |
| 72 | +``` |
| 73 | + curl -X POST "https://{api-gateway-endpoint}/prod" -H 'Content-Type: application/json' -d '{"OriginalText":"Hello this is serverless","TranslateToLanguage":"ef"}' |
| 74 | +``` |
| 75 | +
|
| 76 | +Response: |
| 77 | +``` |
| 78 | + "An error occurred (UnsupportedLanguagePairException) when calling the TranslateText operation: Unsupported language pair: en to ef. Target language 'ef' is not supported" |
| 79 | +``` |
| 80 | +
|
| 81 | +## Documentation |
| 82 | +- [Working with REST APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-rest-api.html) |
| 83 | +- [Working with AWS Lambda proxy integrations for REST APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html) |
| 84 | +- [AWS Lambda - the Basics](https://docs.aws.amazon.com/whitepapers/latest/serverless-architectures-lambda/aws-lambdathe-basics.html) |
| 85 | +- [Lambda Function Handler](https://docs.aws.amazon.com/whitepapers/latest/serverless-architectures-lambda/the-handler.html) |
| 86 | +- [Function Environment Variables](https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html) |
| 87 | +
|
| 88 | +## Cleanup |
| 89 | +
|
| 90 | +Please empty the S3 bucket to delete the audio files before running "sam delete". |
| 91 | +
|
| 92 | +1. Delete the stack |
| 93 | + ```bash |
| 94 | + sam delete |
| 95 | + ``` |
| 96 | +
|
| 97 | +This pattern was contributed by Sudheer Yalamanchili. |
| 98 | +
|
| 99 | +---- |
| 100 | +Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 101 | +
|
| 102 | +SPDX-License-Identifier: MIT-0 |
0 commit comments