Serverless Lambda Function using AWS Polly and Amazon S3 Services
In this Amazon Web Services aka AWS guide I will show cloud service developers to create a serverless Lambda function created with Python and uses AWS Polly service that converts given text into audio and stores the media file in an S3 bucket using the Amazon Simple Storage Service S3.
To summarize, I want to show initial steps for how to use Amazon Web Services aka AWS Services to create a text-to-speech solution.
Create Simple Storage Service AWS S3 Bucket
First of all, let's start with Amazon S3 bucket.
Since I want the output audio files to be accessable by everyone, this bucket will be Public.
Objects included in a public S3 bucket can be accessible using URL pointing to the object on a web browser. So the speech audio file converted from the given text can be downloadable from the S3 bucket URL.
Otherwise, the user should have to download the converted speech audio using AWS Console.
Launch AWS Console and login to your account.
Note, use an IAM account as much as possible instead of using your AWS root account to protect your account against unauthorized use.
Additionally use MFA, please refer to our guide: Enable MFA Multi-Factor Authentication for AWS Users
Then on AWS Services main page, open S3 service which is listed under Storage services.
Before you create the AWS S3 bucket as your converted speech audio files from text, decide for a meaningful name for your S3 bucket.
After you create the S3 bucket, apply following policy using the Permissions tab of the S3 bucket properties page.
This policy will enable public access to the contents of the S3 bucket.
Here is the AWS Simple Storage Service S3 bucket policy in JSON format for public access to AWS Polly output files.
Here is what I see on my AWS account when I go the Amazon S3 service dashboard.
Create IAM Role
For our solution to convert given text to speech audio file using a Python Lambda function and store the output audio in Amazon S3 bucket, we need a role which provides required access to all mentioned AWS services and related service actions.
To manage all these AWS service relations we require an AWS Identity and Access Management IAM role.
Let's create the AWS IAM role.
Go to IAM Service page and switch to Policies tab.
As you have realized, we will start with policy creation and then attach or assign this policy to a new IAM role as the following step.
Start creating a new policy for our process by pressing on Create policy button.
On the following screen, switch to JSON tab to edit the policy permissions using text editor instead of Visual editor
Copy and paste following policy JSON string into the policy editor screen.
This policy will allow all resources on S3 bucket to list objects and create a new object in the S3 bucket.
Additionally, users who have the role with this policy can execute SynthesizeSpeech method of AWS Polly service.
Click on Review policy to continue.
Then press Create policy to complete this task, that is to create policy for role required by the lambda function to convert text to audio using ASW Polly service and to store output audio files in S3 bucket.
Now we can continue with Role creation.
Go to IAM main page again. There switch to Roles tab this time instead of Policies as we did last time.
Press Create role button.
Then select Amazon Lambda service from AWS services list
We selected Lambda, because our main development will take place in serverless structure using an AWS Lambda function. And this function requires access to other AWS services.
After Lambda service is selected, click Next: Permissions button to continue.
Since we have already created the policy in previous step, start typing the policy name in Search box.
Mark the checkbox right before the policy name to attach this policy (or permissions) to this new IAM role.
Press Next: Review
Give a descriptive name to your new AWS IAM role and provide some description for future to understand at first look what does this role is used for.
Press Create role to finish AWS role creation.
Create Lambda Function using Python
On AWS Console, launch Lambda service.
Display Functions list using the shortcut on the left side.
To create a new Lambda function, press Create function button.
There are 3 options to start creating a lambda function:
Author from scratch,
Blueprints,
Serverless Application Repository
Let's start with using a blueprint. This will make Lambda creation easier. Click on Blueprints option.
In Blueprints filter box, type "hello" and press Enter to search.
From the list, select hello-world-python with Python 2.7
Then press Configure
In Basic Information section, provide a name for your AWS Lambda function that will convert text to speech and store it in your Amazon S3 bucket.
For example: Python_Lambda_Function_for_Polly
Additionally, to assign the Role we have created in previous steps in this tutorial follow steps:
In the Role dropdown list, choose Choose an existing role
In Existing Role list, choose recently created AWS role
Don't try to change the Lambda function code at this step.
Just press Create function button
After you have created the AWS Lambda function, the initial view from Configuration screen will be similar to following screenshot.
You see the Lambda function in the midlle.
The other AWS services; Amazon Polly and Amazon S3 are displayed because the IAM role attached to this Lambda service has access to these two AWS services too.
These services and relations are automatically brought to the designer.
Now Lambda developer or AWS developer can copy following Python code and paste it in.
You see, we have important modules from boto3 to access to AWS region and Amazon services like Polly and S3 Simple Storage Service.
An important note for developers who are new to AWS with Python, Boto is the Amazon Web Services AWS SDK for Python. So if you are a Python developer, you can access to more Amazon AWS services using Boto in your Python developments.
For more about Boto please refer to online documentation on Boto 3
Using Polly instance, it is possible to execute synthesize_speech function which converts text to speech audio file.
In the last code line, the output of the synthesize_speech function is written into the target S3 bucket.
One final configuration developers should consider is in Basic settings. There you will see timeout options, change it to 3 minutes for example. Developers should increase the default timeout value to be in safe side since the process is taking some time.
To finish the Lambda function editing, go to the top of the page and press Save button.
Then we are ready to Test our AWS Lambda function. To execute Lambda script, press Test button.
For the first time Configure Event screen will be displayed in front of the developer, just type anything in Event name and press Create button.
If test execution is successfull, you will see a message in green background.
If you got green as your test result, now switch to Amazon S3 service on AWS console and open your S3 bucket to display bucket object list.
There you will see your mp3 audio file which is converted from the given text to the Amazon Polly synthesize_speech() function and ready for all users to listen and download from public S3 bucket.
And the audio file created from provided text by Polly function synthesize_speech() is as follows: AWS Polly text-to-speech file