Skip to content

Commit 8aaee03

Browse files
authored
Merge pull request #2688 from vaidehip86/vaidehp-feature-ApiGateway-Transcribe-Integration
New serverless pattern-apigw-translate
2 parents 37f9c62 + 4653529 commit 8aaee03

File tree

5 files changed

+267
-0
lines changed

5 files changed

+267
-0
lines changed

apigw-translate/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Amazon API Gateway to Amazon Translate Integration
2+
3+
This pattern creates a REST API Gateway that can perform POST API Call to Amazon Translate's TranslateText API
4+
5+
Learn more about this pattern at Serverless Land Patterns: << Add the live URL here >>
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+
16+
## Deployment Instructions
17+
18+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
19+
```
20+
git clone https://github.com/aws-samples/serverless-patterns
21+
```
22+
1. Change directory to the pattern directory:
23+
```
24+
cd apigw-translate
25+
```
26+
1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
27+
```
28+
sam deploy --guided
29+
```
30+
1. During the prompts:
31+
* Enter a stack name
32+
* Enter the desired AWS Region
33+
* Allow SAM CLI to create IAM roles with the required permissions.
34+
35+
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.
36+
37+
1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.
38+
39+
## How it works
40+
41+
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.
42+
43+
## Testing
44+
45+
Once the application is deployed, either use a curl or call the endpoint from Postman.
46+
47+
Example POST Request to translate text to Spanish:
48+
```
49+
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"}'
50+
```
51+
52+
Response:
53+
```
54+
{"SourceLanguageCode":"en","TargetLanguageCode":"es","TranslatedText":"¡Hola, mundo!"}
55+
```
56+
57+
## Cleanup
58+
59+
1. In your command line, from the sam application project directory, run the following:
60+
```bash
61+
sam delete
62+
63+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"title": "REST API Gateway to Amazon Translate",
3+
"description": "Create a REST API Gateway that can perform POST API Call to Amazon Translate's TranslateText API",
4+
"language": "Python",
5+
"level": "200",
6+
"framework": "SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This Pattern creates a REST API Gateway. The API Gateway endpoint uses service integration to directly connect to Amazon Translate."
11+
]
12+
},
13+
"gitHub": {
14+
"template": {
15+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-translate",
16+
"templateURL": "serverless-patterns/apigw-translate",
17+
"projectFolder": "apigw-translate",
18+
"templateFile": "template.yaml"
19+
}
20+
},
21+
"resources": {
22+
"bullets": [
23+
{
24+
"text": "Amazon Translate API Actions List",
25+
"link": "https://docs.aws.amazon.com/translate/latest/APIReference/API_TranslateText.html"
26+
}
27+
]
28+
},
29+
"deploy": {
30+
"text": [
31+
"sam deploy --guided"
32+
]
33+
},
34+
"testing": {
35+
"text": [
36+
"See the GitHub repo for detailed testing instructions."
37+
]
38+
},
39+
"cleanup": {
40+
"text": [
41+
"Delete the stack: <code>sam delete</code>."
42+
]
43+
},
44+
"authors": [
45+
{
46+
"name": "Vaidehi Patel",
47+
"image": "https://avatars.githubusercontent.com/u/23283229?v=4",
48+
"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",
49+
"linkedin": "vaidehi-patel08"
50+
}
51+
],
52+
"patternArch": {
53+
"icon1": {
54+
"x": 20,
55+
"y": 50,
56+
"service": "apigw",
57+
"label": "API Gateway REST API"
58+
},
59+
"icon2": {
60+
"x": 80,
61+
"y": 50,
62+
"service": "translate",
63+
"label": "Amazon Translate"
64+
},
65+
"line1": {
66+
"from": "icon1",
67+
"to": "icon2",
68+
"label": ""
69+
}
70+
}
71+
}

apigw-translate/events/event.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"text": "Hello, world!",
3+
"sourceLanguageCode": "en",
4+
"targetLanguageCode": "es"
5+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"title": "REST API Gateway to Amazon Translate",
3+
"description": "Create a REST API Gateway that can perform POST API Call to Amazon Translate's TranslateText API",
4+
"language": "Python",
5+
"level": "200",
6+
"framework": "AWS SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This Pattern creates a REST API Gateway. The API Gateway endpoint uses service integration to directly connect to Amazon Translate."
11+
]
12+
},
13+
"gitHub": {
14+
"template": {
15+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-translate",
16+
"templateURL": "serverless-patterns/apigw-translate",
17+
"projectFolder": "apigw-translate",
18+
"templateFile": "apigw-translate/template.yaml"
19+
}
20+
},
21+
"resources": {
22+
"bullets": [
23+
{
24+
"text": "Amazon Translate API Actions List",
25+
"link": "https://docs.aws.amazon.com/translate/latest/APIReference/API_TranslateText.html"
26+
}
27+
]
28+
},
29+
"deploy": {
30+
"text": [
31+
"sam deploy --guided"
32+
]
33+
},
34+
"testing": {
35+
"text": [
36+
"See the GitHub repo for detailed testing instructions."
37+
]
38+
},
39+
"cleanup": {
40+
"text": [
41+
"Delete the stack: <code>sam delete</code>."
42+
]
43+
},
44+
"authors": [
45+
{
46+
"name": "Vaidehi Patel",
47+
"image": "https://avatars.githubusercontent.com/u/23283229?v=4",
48+
"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",
49+
"linkedin": "https://www.linkedin.com/in/vaidehi-patel08/"
50+
}
51+
]
52+
}

apigw-translate/template.yaml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: API Gateway with direct integration to Amazon Translate's TranslateText API.
4+
5+
Resources:
6+
TranslateApi:
7+
Type: AWS::Serverless::Api
8+
Properties:
9+
Name: TranslateAPI
10+
StageName: Prod
11+
OpenApiVersion: 3.0.2
12+
DefinitionBody:
13+
openapi: 3.0.1
14+
info:
15+
title: TranslateAPI
16+
version: 1.0
17+
paths:
18+
/translate:
19+
post:
20+
x-amazon-apigateway-integration:
21+
type: aws
22+
uri:
23+
Fn::Sub: >-
24+
arn:aws:apigateway:${AWS::Region}:translate:action/TranslateText
25+
httpMethod: POST
26+
credentials:
27+
Fn::GetAtt: [ApiGatewayIAMRole, Arn]
28+
requestParameters:
29+
integration.request.header.Content-Type: "'application/x-amz-json-1.1'"
30+
integration.request.header.X-Amz-Target: "'AWSShineFrontendService_20170701.TranslateText'"
31+
requestTemplates:
32+
application/json: |
33+
{
34+
"Text": $input.json('$.text'),
35+
"SourceLanguageCode": $input.json('$.sourceLanguageCode'),
36+
"TargetLanguageCode": $input.json('$.targetLanguageCode')
37+
}
38+
responses:
39+
default:
40+
statusCode: 200
41+
responses:
42+
"200": # Wrapped in quotes to avoid YAML parsing errors
43+
description: Translate API response
44+
content:
45+
application/json:
46+
schema:
47+
type: object
48+
properties:
49+
TranslatedText:
50+
type: string
51+
52+
ApiGatewayIAMRole:
53+
Type: AWS::IAM::Role
54+
Properties:
55+
AssumeRolePolicyDocument:
56+
Version: '2012-10-17'
57+
Statement:
58+
- Effect: Allow
59+
Principal:
60+
Service: apigateway.amazonaws.com
61+
Action: sts:AssumeRole
62+
Policies:
63+
- PolicyName: ApiGatewayTranslatePolicy
64+
PolicyDocument:
65+
Version: '2012-10-17'
66+
Statement:
67+
- Effect: Allow
68+
Action: "translate:TranslateText"
69+
Resource: "*"
70+
71+
Outputs:
72+
ApiEndpoint:
73+
Description: API Gateway Endpoint URL
74+
Value: !Sub "https://${TranslateApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/translate"
75+
76+

0 commit comments

Comments
 (0)