S3 to Lambda to Polly

Automating Text-to-Speech with Lambda and S3 Triggers

Amazon S3AWS LambdaAmazon Polly
from aws_cdk import (
    # Duration,
    Stack,
    aws_s3 as s3,
    aws_lambda as lambda_,
    aws_iam as iam,
    Duration,
    aws_lambda_event_sources as eventsources
)
from constructs import Construct

class AmazonPollyStack(Stack):

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)
        
        #Input Bucket
        self.input_bucket = s3.Bucket(self, "input-bucket",
                            versioned=True,
                            encryption=s3.BucketEncryption.S3_MANAGED,
                            block_public_access=s3.BlockPublicAccess.BLOCK_ALL,
                            enforce_ssl=True)

        # Destination Bucket
        self.destination_bucket = s3.Bucket(self, 'destination-bucket',
                                versioned=True,
                                encryption=s3.BucketEncryption.S3_MANAGED,
                                block_public_access=s3.BlockPublicAccess.BLOCK_ALL,
                                enforce_ssl=True)
        
        python_lambda_kwargs = {
            'handler': 'lambda_function.lambda_handler',
            'runtime': lambda_.Runtime.PYTHON_3_9,
            'timeout': Duration.minutes(2)
        }

        # Trigger Transcription Lambda
        trigger_polly_lambda = lambda_.Function(self, "file-upload-trigger", **python_lambda_kwargs,
                                                        code=lambda_.Code.from_asset(
                                                            './pattern/assets'),
                                                        function_name="start-polly",
                                                        initial_policy=[
                                                            iam.PolicyStatement(
                                                                actions=['polly:SynthesizeSpeech'],
                                                                resources=['*']),
                                                            iam.PolicyStatement(actions=['s3:PutObject'],
                                                                                resources=[
                                                                                    self.destination_bucket.bucket_arn+'/*']
                                                                                ),
                                                            iam.PolicyStatement(actions=['s3:GetObject',
                                                                                        's3:ListBucket'],
                                                                                resources=[
                                                                                    self.input_bucket.bucket_arn,
                                                                                    self.input_bucket.bucket_arn + '/*'
                                                                                ]
                                                                        )
                                                        ]
                                                        )

        # Add Trigger and Environment Variable
        trigger_polly_lambda.add_event_source(eventsources.S3EventSource(self.input_bucket, events=[s3.EventType.OBJECT_CREATED]))
        trigger_polly_lambda.add_environment('DESTINATION_BUCKET', self.destination_bucket.bucket_name)

Download

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

Pattern repository

View on GitHub

Last updated on 26 Dec 2024

Edit this page