Setting up Text To Speech Application using Amazon Polly

Vivek Maskara
Mar 17, 2018 · 7 min read

Long back we used AWS to set up a PHP and MYSQL application. This weekend will work Amazon Polly to deploy our own TTS application.

Amazon Polly is a Text-to-Speech service that uses advanced deep learning technologies to synthesize speech that sounds like a human voice.

This article is heavily based on the guide on AWS. Our application architecture looks as below:

Source Link

Lets start building the application where we will set up a few lambda functions, SNS and S3 bucket to finally result in RESTful APIs that can convert text to speech for us.

Create a DynamoDB Table

Create a DynamoDB table to store text and corresponding audio files.

Creating an AloudStory table

Create an S3 Bucket

Create an S3 bucket that will hold all the audio files for you. Go through the Create bucket wizard to complete the process.

Creating a S3 bucket

Create a SNS Topic

The work of converting a text file to an audio output would be done by 2 Lambda functions. Let’s create a new SNS topic from the SNS console.

Creating a new SNS topic for new posts

Create a New Role

Create a new Role in the IAM Console.

Choose the Lambda service for this role

After choosing the service that will use this role, go ahead and give this Role a name and click on Create Role.

Give the role a name and description

After the Role is created, click on Add inline policy under the Permissions tab.

Click on Add inline policy

Copy paste the following policy, which provides Lambda with access to the services included in the architecture diagram

After adding the JSON, review the policy and name it.

Create policy after reviewing it

Creating a New Post Lambda Function

Create a Lambda function fore new posts

Copy paste the following code for it:

Use the following environment variables for the DynamoDB table and the SNS topic.

  • SNS_TOPIC — the Amazon Resource Name (ARN) of the SNS topic we created
  • DB_TABLE_NAME — the name of the DynamoDB table (in our case, it’s posts)

Assign the IAM role that we created for the Lambda functions.

Select the role for execution

Add a New Test in the wizard to test if the function is working.

Now you test your function by clicking on Test.

Create a Convert to Audio Lambda function

Again use the same wizard to create a new Lambda function, PostReader_ConvertToAudio.

Create a lambda function to convert to audio

Configure an SNS trigger so that this function is executed whenever a new post comes in.

Configuring trigger for the lambda function

Copy paste the following code to this lambda function editor. Take care of your indentations, its python. :D

Use the following environment variables and values:

  • DB_TABLE_NAME — The name of the DynamoDB table (in our case, it’s posts )
  • BUCKET_NAME — The name of the S3 bucket that we created to store MP3 files

Update the execution time settings for this lambda function.

Go ahead and retest the PostReader_NewPost function. It will add an entry to your database table and a mp3 file to the S3 bucket.

Create a Lambda function to get Audio

Now, we just need to create another lambda function, PostReader_GetPost that retrieves information from the database.

Get post lambda function

This function needs just one environment variable ie. DB_TABLE_NAME.

To test function add a New Test as below:

Exposing the Lambda function as an API

Next, we need to expose our application logic as a RESTful web service. Choose to Create API option in the API Gateway console to get started.

Fill in the details to create an API

After the API is created, choose Create Method from the menu.

The POST method invokes the PostReader_NewPost Lambda function. For the GET method, our API invokes the PostReader_GetPost Lambda function.

Next, enable CORS to enable calling these methods from a different domain.

Enable CORS

Now, modify the GET method to add a postId query param to it.

The lambda function expects a JSON input so we need to add a mapping in the Integration Request section.

Similarly, you can configure your POST method to accept a JSON input. Go to the Models section and create a new model.

Deploying the API

Now that you have configured the methods, go ahead and deploy it.

Choose a deployment stage

Choose a deployment stage

That’s it your APIs are up and running. The AWS console will show you the invocation URL for it.


Make sure you give this post 50 claps and follow me if you enjoyed this post and want to see more!

Vivek Maskara

Written by

Working on awesome stuff at Zeta.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade