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.

Step Functions to Polly to S3 with CDK (Typescript)

Created with SnapStep FunctionsPollyS3

Step Functions - Polly SDK integration for text to speech output as mp3 into s3 .

This project demonstrates how to implement a State Function State machine to synthesize given words using Amazon Polly and save each mp3 generated in a S3 bucket.
This is implemented with CDK (Typescript)

import {
  aws_stepfunctions as sfn,
  aws_stepfunctions_tasks as tasks,
  aws_s3 as s3,
  CfnOutput,
  Stack,
  StackProps
} from 'aws-cdk-lib';
import { Construct } from 'constructs';

export class StepfunctionPollyS3CdkStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    // For more about voice ids in Amazon Polly
    // Visit: https://docs.aws.amazon.com/polly/latest/dg/voicelist.html#availablevoice-list
    const VOICE_ID = 'Joey';

    const wordsBucket = new s3.Bucket(this, 'WordsBucket');

    const wordsIterator = new sfn.Map(this, 'WordsIterator', {
      maxConcurrency: 5,
    });

    const synthersize = new tasks.CallAwsService(this, 'StartSpeechSynthesisTask', {
      service: 'polly',
      action: 'startSpeechSynthesisTask',
      resultPath: '$.result',
      parameters: {
        "OutputFormat": "mp3",
        "OutputS3BucketName": wordsBucket.bucketName,
        "Text.$": "$.word",
        "VoiceId": VOICE_ID
      },
      iamResources: ['*'],
    });

    const stateMachineDefinition = wordsIterator.iterator(synthersize);

    const wordSynthesiserStateMachine = new sfn.StateMachine(this, 'WordSynthesiser', {
      definition: stateMachineDefinition
    });

    wordsBucket.grantPut(wordSynthesiserStateMachine);

    // Outputs
    new CfnOutput(this, 'StateMachine', {
      value: wordSynthesiserStateMachine.stateMachineName,
      description: 'State machine',
      exportName: 'StateMachine',
    });

    new CfnOutput(this, 'BucketName', {
      value: wordsBucket.bucketName,
      description: 'Bucket name',
      exportName: 'BucketName',
    });
  }
}

< 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/stepfunction-polly-s3-cdk

Deploy

cdk deploy --all


Testing

See the GitHub repo for detailed testing instructions.

Cleanup

Delete the stack: cdk destory --all.

Pubudu Jayawardana

Presented by Pubudu Jayawardana

AWS Community Builder in Serverless category. Serverless advocate and enthusiast.

Follow on LinkedIn