Creates an Amazon API Gateway REST API and an AWS Lambda function that calls AWS Translate and Amazon Polly.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: An Amazon REST API gateway integrated with a AWS Lambda function that calls AWS Translate service to translate given text, then call AWS Polly service to convert the text into speech and save audio file to s3. (uksb-1tthgi812) (tag:apigw-lambda-translate-polly-s3)
Parameters:
s3BucketName:
Type: String
Description: s3 bucket to save audio files generated by polly
Resources:
# S3 bucket to store audio file created by polly service
AudioFileBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref s3BucketName
# API Gateway REST API
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
Name: apigw-translate-polly-s3
StageName: prod
EndpointConfiguration:
Type: REGIONAL
# Lambda Function to call AWS Translate and Polly services
ApiFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: lambda-translate-polly-s3
Environment:
Variables:
S3_BUCKET_NAME: !Ref s3BucketName
Runtime: python3.9
Handler: index.lambda_handler
CodeUri: src/
MemorySize: 128
Policies:
- TranslateReadOnly
- AmazonPollyReadOnlyAccess
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- "s3:GetObject"
- "s3:PutObject"
Resource: "*"
Events:
ApiEvent:
Type: Api
Properties:
Path: /
Method: post
RestApiId:
Ref: ApiGatewayApi
Outputs:
RestApiEndpoint:
Description: "The endpoint for the REST API Gateway"
Value: !Sub "https://${ApiGatewayApi}.execute-api.${AWS::Region}.amazonaws.com/prod/"
Visit the GitHub repo for this pattern.
git clone https://github.com/aws-samples/serverless-patterns/ cd serverless-patterns/apigw-lambda-translate-polly-s3
sam deploy --guided
sam delete
.