SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for AWS Amazon Web Services, Redshift, AWS Lambda Functions, S3 Buckets, VPC, EC2, IAM

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.

Amazon Polly AWS Diagram for Text-to-Speech


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.

AWS Simple Storage Service S3 bucket policy for public access

Here is the AWS Simple Storage Service S3 bucket policy in JSON format for public access to AWS Polly output files.

{
 "Version": "2012-10-17",
 "Statement": [
  {
   "Sid": "AddPerm",
   "Effect": "Allow",
   "Principal": "*",
   "Action": "s3:GetObject",
   "Resource": "arn:aws:s3:::kodyaz-polly/*"
  }
 ]
}
Amazon S3 bucket policy

Here is what I see on my AWS account when I go the Amazon S3 service dashboard.

public S3 bucket in Amazon S3 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.

{
 "Version": "2012-10-17",
 "Statement": [
  {
   "Effect": "Allow",
   "Action": [
    "polly:SynthesizeSpeech",
    "s3:ListBucket",
    "s3:PutObject"
   ],
   "Resource": "*"
  }
 ]
}
IAM role policy

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.

create policy required for AWS IAM role

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.

attach permissions and policies to AWS 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.

create Amazon Identity and Access Management IAM role

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.

create AWS Lambda function using blueprint and Python

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

Amazon Lambda function basic information

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.

AWS Lambda and other related Amazon Services Polly and S3 on Designer screen

Now Lambda developer or AWS developer can copy following Python code and paste it in.

import json

def lambda_handler(event, context):

import codecs
from boto3 import Session
from boto3 import resource

session = Session(region_name="us-east-1")
polly = session.client("polly")

s3 = resource('s3')
bucket_name = "kodyaz-polly"
bucket = s3.Bucket(bucket_name)

filename = "mynameis.mp3"
myText = """
Hello,
My name is Eralper.
Welcome to my website kodyaz.com
"""

response = polly.synthesize_speech(
Text=myText,
OutputFormat="mp3",
VoiceId="Matthew")
stream = response["AudioStream"]

bucket.put_object(Key=filename, Body=stream.read())
Python code for AWS Lambda function

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.

Amazon Lambda function Python codes using AWS Polly and AWS S3 services

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.

Amazon S3 bucket containing speech audio file converted by AWS Polly function

And the audio file created from provided text by Polly function synthesize_speech() is as follows: AWS Polly text-to-speech file



AWS


Copyright © 2004 - 2024 Eralper YILMAZ. All rights reserved.