4.23 Use Polly to Pass Your Exam - Lab of a Serverless Application
Last updated
Was this helpful?
Last updated
Was this helpful?
Select a region which support Polly service (e.g. North Virginia).
Set up DynamoDB.
Create a table. Set the name and primary key (String id) of the table.
Create S3 bucket.
First bucket is used to store the website pages. The region should be same with the Polly service to decrease the latency. When you create this bucket, go to its properties, and use this bucket to host a static website. Create index document and error document (index.html and error.html).
Second bucket is used to store the output audio files. Same region with the first bucket.
Create SNS (Simple Notification Service) topic.
Create topic. Set the topic name and display name.
Create IAM Role
Go to roles and create policy. Choose Lambda service. For attaching permission policy, click create policy, and click create your own policy. At the review policy step, configure the policy name, description, and policy document. In the policy document, copy and paste the content of "lambdapolicy.json", which can be downloaded from Udemy. In the policy, in the Action array, each element represents a policy for a particular service. After you copy and paste it into policy document, click validate policy and click create policy.
Go to roles again and create a role about lambda. Look up the policy you created at last step, and click review role, and this will let you to create a role which has the policy you create at last step. When you configured the role name and role description, go head and click create role.
Create First Lambda function
Create the first Lambda function. Make sure you are in the region where Alexa is available. Click create a function. Click Author from scratch (don't use any blueprint). Configure the name, role, and existing role. Click create function.
Enter the function. In the Configuration tab, select runtime as python 2.7 and copy the code in "newposts.py" which can be found on Udemy and paste it to index.py of function code. Change your handler to be [index.function-name].
Configure the environment variables which will be passed into the code every time the code is run. For key "DB-TABLE-NAME", the value is "posts", for "SNS-TOPIC", the value is the ARN of the SNS topic you created in this lab.
Click save.
Configure test events. Select create new test event and select a event template and set a event name and copy the content in "sample.json" which can be found on Udemy and paste it to the text area. Click create.
Click Test to test your lambda function code. You can see the details of test result. Go to DynamoDB, go to Tables, go to the table you created in this lab, go to Items, you can find there is a record has been stored there.
Create Second Lambda function
Click Lambda and click create function. Click Author from scratch. Set the name, role, existing role of the function. Click create function.
Copy the code in "converttoaudio.py" which can be found on Udemy to the function code. Change the handler name.
Define the environment variables. DB-TABLE-NAME: posts, BUCKET-NAME: acloudguru-pollyaudiofiles.
This lambda function may spend longer time to convert text to audio, so in the Basic Settings, you can set the timeout to be 5 minutes (maximum).
Click save.
Click Triggers and add trigger. Select SNS. Select the SNS topic to be "new_posts" which is the topic you created just now in this lab. Click submit.