Select your cookie preferences

We use cookies and similar tools to enhance your experience, provide our services, deliver relevant advertising, and make improvements. Approved third parties also use these tools to help us deliver advertising and provide certain site features.

API Gateway REST API to Lambda function and AWS Translate and Amazon Polly

Created with SnapAPI GatewayAWS LambdaAmazon TranslateAmazon PollyAmazon S3

Creates an Amazon API Gateway REST API and an AWS Lambda function that calls AWS Translate and Amazon Polly.

API Gateway REST API is integrated to AWS Lambda function which calls AWS Translate and Amazon Polly services.

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/"

< Back to all patterns


GitHub icon Visit the GitHub repo for this pattern.

Download

git clone https://github.com/aws-samples/serverless-patterns/ cd serverless-patterns/apigw-lambda-translate-polly-s3

Deploy

sam deploy --guided


Testing

See the GitHub repo for detailed testing instructions.

Cleanup

Delete the stack: sam delete.

Sudheer Yalamanchili

Presented by Sudheer Yalamanchili

Sudheer is a cloud application architect working with AWS ProServ.