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
63 changes: 63 additions & 0 deletions apigw-translate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Amazon API Gateway to Amazon Translate Integration

This pattern creates a REST API Gateway that can perform POST API Call to Amazon Translate's TranslateText API

Learn more about this pattern at Serverless Land Patterns: << Add the live URL here >>

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)
* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed

## Deployment Instructions

1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
```
git clone https://github.com/aws-samples/serverless-patterns
```
1. Change directory to the pattern directory:
```
cd apigw-translate
```
1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
```
sam deploy --guided
```
1. During the prompts:
* Enter a stack name
* Enter the desired AWS Region
* Allow SAM CLI to create IAM roles with the required permissions.

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.

1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.

## How it works

This pattern deploys an Amazon API Gateway REST API with a /translate endpoint integrated with Amazon Translate’s TranslateText API. The service processes the provided text and language codes in the request body, returning the translated text or an error response if the specified language code is unsupported.

## Testing

Once the application is deployed, either use a curl or call the endpoint from Postman.

Example POST Request to translate text to Spanish:
```
curl -X POST "https://YOUR_API_ID.execute-api.YOUR_AWS_REGION.amazonaws.com/Prod/translate" -H "Content-Type: application/json" -d '{"text": "Hello, world!", "sourceLanguageCode": "en", "targetLanguageCode": "es"}'
```

Response:
```
{"SourceLanguageCode":"en","TargetLanguageCode":"es","TranslatedText":"¡Hola, mundo!"}
```

## Cleanup

1. In your command line, from the sam application project directory, run the following:
```bash
sam delete

```
71 changes: 71 additions & 0 deletions apigw-translate/apigw-translate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"title": "REST API Gateway to Amazon Translate",
"description": "Create a REST API Gateway that can perform POST API Call to Amazon Translate's TranslateText API",
"language": "Python",
"level": "200",
"framework": "SAM",
"introBox": {
"headline": "How it works",
"text": [
"This Pattern creates a REST API Gateway. The API Gateway endpoint uses service integration to directly connect to Amazon Translate."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-translate",
"templateURL": "serverless-patterns/apigw-translate",
"projectFolder": "apigw-translate",
"templateFile": "template.yaml"
}
},
"resources": {
"bullets": [
{
"text": "Amazon Translate API Actions List",
"link": "https://docs.aws.amazon.com/translate/latest/APIReference/API_TranslateText.html"
}
]
},
"deploy": {
"text": [
"sam deploy --guided"
]
},
"testing": {
"text": [
"See the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"Delete the stack: <code>sam delete</code>."
]
},
"authors": [
{
"name": "Vaidehi Patel",
"image": "https://avatars.githubusercontent.com/u/23283229?v=4",
"bio": "I'm a Solutions Architect at Amazon Web Services based in Dallas,TX. I'm a serverless enthusiast, I like to build and architect serverless applications",
"linkedin": "vaidehi-patel08"
}
],
"patternArch": {
"icon1": {
"x": 20,
"y": 50,
"service": "apigw",
"label": "API Gateway REST API"
},
"icon2": {
"x": 80,
"y": 50,
"service": "translate",
"label": "Amazon Translate"
},
"line1": {
"from": "icon1",
"to": "icon2",
"label": ""
}
}
}
5 changes: 5 additions & 0 deletions apigw-translate/events/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"text": "Hello, world!",
"sourceLanguageCode": "en",
"targetLanguageCode": "es"
}
52 changes: 52 additions & 0 deletions apigw-translate/example-pattern.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"title": "REST API Gateway to Amazon Translate",
"description": "Create a REST API Gateway that can perform POST API Call to Amazon Translate's TranslateText API",
"language": "Python",
"level": "200",
"framework": "AWS SAM",
"introBox": {
"headline": "How it works",
"text": [
"This Pattern creates a REST API Gateway. The API Gateway endpoint uses service integration to directly connect to Amazon Translate."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-translate",
"templateURL": "serverless-patterns/apigw-translate",
"projectFolder": "apigw-translate",
"templateFile": "apigw-translate/template.yaml"
}
},
"resources": {
"bullets": [
{
"text": "Amazon Translate API Actions List",
"link": "https://docs.aws.amazon.com/translate/latest/APIReference/API_TranslateText.html"
}
]
},
"deploy": {
"text": [
"sam deploy --guided"
]
},
"testing": {
"text": [
"See the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"Delete the stack: <code>sam delete</code>."
]
},
"authors": [
{
"name": "Vaidehi Patel",
"image": "https://avatars.githubusercontent.com/u/23283229?v=4",
"bio": "I'm a Solutions Architect at Amazon Web Services based in Dallas,TX. I'm a serverless enthusiast, I like to build and architect serverless applications",
"linkedin": "https://www.linkedin.com/in/vaidehi-patel08/"
}
]
}
76 changes: 76 additions & 0 deletions apigw-translate/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: API Gateway with direct integration to Amazon Translate's TranslateText API.

Resources:
TranslateApi:
Type: AWS::Serverless::Api
Properties:
Name: TranslateAPI
StageName: Prod
OpenApiVersion: 3.0.2
DefinitionBody:
openapi: 3.0.1
info:
title: TranslateAPI
version: 1.0
paths:
/translate:
post:
x-amazon-apigateway-integration:
type: aws
uri:
Fn::Sub: >-
arn:aws:apigateway:${AWS::Region}:translate:action/TranslateText
httpMethod: POST
credentials:
Fn::GetAtt: [ApiGatewayIAMRole, Arn]
requestParameters:
integration.request.header.Content-Type: "'application/x-amz-json-1.1'"
integration.request.header.X-Amz-Target: "'AWSShineFrontendService_20170701.TranslateText'"
requestTemplates:
application/json: |
{
"Text": $input.json('$.text'),
"SourceLanguageCode": $input.json('$.sourceLanguageCode'),
"TargetLanguageCode": $input.json('$.targetLanguageCode')
}
responses:
default:
statusCode: 200
responses:
"200": # Wrapped in quotes to avoid YAML parsing errors
description: Translate API response
content:
application/json:
schema:
type: object
properties:
TranslatedText:
type: string

ApiGatewayIAMRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: apigateway.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: ApiGatewayTranslatePolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: "translate:TranslateText"
Resource: "*"

Outputs:
ApiEndpoint:
Description: API Gateway Endpoint URL
Value: !Sub "https://${TranslateApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/translate"