4.24 Use Polly to Pass Your Exam - Lab of a Serverless Application
Lab steps:
Create the third Lambda function.
Go to Lambda. Click create function. Click Author from scratch. Configure the name, role, existing role, and click create function.
Select Python 2.7, and copy the code in "getposts.py" which can be found on Udemy and paste it to the text area. Change the Handler name.
Configure the Environment variables. DB-TABLE-NAME: posts.
Write some description for this function in the Basic Settings.
Click save.
Configure a test event. Event name called RetrievePosts, and write {"postId": "*"} in the text area. Click Create.
Click Test. This will retrieve any post that we have in the DynamoDB.
Configure API Gateway.
Click API Gateway. Click get started.
In Create new API part, select New API. Give a API name and Description. Click Create API.
Go in the API and create some methods. Click Actions and click create methods. Select a POST method. Integration type should be Lambda Function, don't use Lambda Proxy Integration, select a Lambda Region, select a Lambda function you created before (the one which takes charge of receiving POST requests). Click save.
Click Actions and click create methods. Select a GET method. Integration type should be Lambda Function, don't use Lambda Proxy Integration, select a Lambda Region, select a Lambda function you created before (the one which takes charge of receiving GET requests). Click save.
We also need a thing called Cross Origins Resource Sharing. (This will be in the exam). Click Actions and click Enable CORS. What this is doing is enabling you to invoke your API from a website that has a different host name to that API. In our case, it gonna be the S3 bucket. When you enable CORS, you need to select POST and GET Methods, and then click "Enable CORS and replace exisiting CORS headers".
In the GET method, go to Method Request, and go to URL Query String Parameters and Add query string: postId, and save it.
In the GET method, go to Integration Request, and go to Body Mapping Templates. In the Request body passthrough, select the option: When there are no templates defined (recommended). Click Add mapping template, in the Content-Type, type in: application/json, and save it. Below it, in the text area, type in: {"postId": "$input.params('postId')"}. Click save.
For now the API is ready. Go to Actions and click Deploy API. The Deployment stage should be [new stage], and give a stage name to it, for example, "dev". Click deploy.
Go to Stages of APIs, You will see a stage called dev and you can see the invoke URL of it. This is your API's URL, and you need to add this to your S3 wesite.
Go to S3, and deploy your website.
Click your polly website bucket, and go to Permissions tab, and click Bucket Policy. Copy the content of "bucketpolicypermissions.json" which can be found on Udemy and paste it to the text area of bucket policy.
Copy the ARN and paste it to the "Resource" element. Click save. This will make it so that any objects that we put into our S3 bucket will be automatically public so we don't need to keep making them public which is what we did in the last lecture.
"index.html", "styles.css", "scripts.js" can be found on Udemy. Open "script.js" file, and replace the value of API-ENDPOINT to be the invoke URL of your API Gateway (you can find it in API Gateway -> one of your API -> stages -> dev).
Go to the polly website bucket, click Upload, and Add files, select "index.html", "styles.css" and "script.js" and upload them.
When you finish to upload, click on "index.html", and you can see the link. Click the link, you can visit the website. You can use * to retrieve all of your posts and its audio.
Go to DynamoDB, go to Tables, go to posts (this is the table of this lab), go to Items, and you can see all of the stored posts. You can select them and delete them or do other things.
Go to S3, go to the bucket your store the audio files, check the mp3 objects, and then you can delete them through "More".
Last updated
Was this helpful?