Using Polly to Pass your Exam (50 – 51)

  Amazon Web Services (AWS)

Polly is Amazon’s text to speech service.

Requires API Gateway and Cross Origin Resource Sharing* Part of the exam!

Artificial Intelligence > Amazon Polly (was just “Polly”)

Create a DynamoDB table

  • New Table: posts
  • Partition Key: id

Setup S3

  • Create 2 buckets.
    • One for the website (enable static web hosting)
    • One for the files.  (Why shouldn’t this be the same?)

Setup Messenging

  • Messaging > Simple Notification Service
  • Create a topic
    • Name: new_posts
    • Display Name: New Posts
    • [Create topic]

Create a Role for the Lambda functions

  • Security, Identity & Complience > IAM > Roles > [Create role]
  • Select type of role
    • AWS Service > Lambda
    • [Next: Permissions]
  • Attach Policy
    • Will create a custom role, so we’ll skip this for now.
    • [Next: Review]
  • Review
    • Role Name: LambdaPostsReaderRole
    • Role Description: Allows Lambda Function to call AWS services on your behalf
    • [Create role]
  • Assign the policies
    • IAM > Roles > LambdaPostsReaderRole
    • “Permissions” tab > Inline Policies > + Add inline policy
  • Set Permissions
    • (*) Custom Policy
    • [Select]
  • Review Policy
    • Policy Name: LambdaPolicy
    • Policy Document: Paste the code found lambdapolicy.json.
    • [Validate Policy] to see if there are any errors (There is on the video)
    • [Apply Policy]

Create Lambda Functions

Write text to the DynamoDB Table

  • Compute > Lambda > [Create a function]
  • Select Blueprint
    • [Author from scratch]
  • Configure Triggers
    • No triggers
    • [Next]
  • Configure Function
    • Name: PostReader_NewPosts
    • Description: Lambda function for new posts
    • Runtime: Python 2.7
    • Lambda function code
      • Insert the code located here: newposts.py
      • Environmental Variables (You can see these in the code.)
        • DB_TABLE_NAME = posts
        • SNS_TOPIC = Get the ARN value for the new_posts topic
          • arn:aws:sns:us-east-2:727545344356:new_posts
    • Lambda function handler and role
      • Handler: lambda_function.lambda_handler
        • This is not explained in the video.
        • Mine defaulted to index.handler, so I changed it to match his.
      • Role: Choose and existing role
      • Existing role: LambdaPostsReaderRole
      • [Next]
    • Review
      • [Create function]
    • Test
      • “voice”: “Joanna”
      • “text”: “This is working!”
      • – Success!

Create Polly Service

  • Lambda > Functions > [Create function] > [Author from scratch]
  • Configure triggers
    • Select SNS
    • Topic = new_posts
    • [x] Enable trigger
    • [Next]
  • Configure function
    • Name: PostReader_ConvertToAudio
    • Description: Convert our posts to audio
    • Runtime: Python 2.7
    • Advanced settings
      • timeout: 5 minutes 0 seconds (might take a while to convert a lot of text!)
    • Code: Insert code found here: convertoaudio.py
    • Environmental Variables
      • DB_TABLE_NAME = posts
      • BUCKET_NAME = your_bucket_name (s3.thomasandsofia.com)
    • Handler: lambda_function.lambda_handler
    • Role: Choose and existing role
    • Existing role: LambdaPostsReaderRole
    • [Next]
  • Review
    • [Create function]

Get Posts from DynamoDB

(might explain why I didn’t understand some of the code….)

  • Lambda > Functions > [Create function] > [Author from scratch]
  • Configure triggers
    • Will be triggered by API Gateway, so leave blank
    • [Next]
  • Configure function
    • Name: PostReader_GetPosts
    • Desc: PostReader_GetPosts
    • Runtime: Python 2.7
    • Code: Insert code found here: getposts.py
    • Environmental Variables
      • DB_TABLE_NAME = posts
      • BUCKET_NAME = your_bucket_name (s3.thomasandsofia.com)
    • Handler: lambda_function.lambda_handler
    • Role: Choose and existing role
    • Existing role: LambdaPostsReaderRole
    • [Next]
  • Review
    • [Create function]

Configure API Gateway

  • Application Services > API Gateway
  • Create new API
    • (*) New API
    • Name and description
      • API name: PostReaderAPI
      • Description: API to read my posts
    • [Create API]
  • Resources – Actions > Create Method
    • Blank dropdown: POST
    • Click the Checkmark Icon
    • – POST – Setup
      • Integration type: (*) Lambda Function
      • Use Lambda Proxy integration [ ] (do not check)
      • Lambda Region: Select your region (us-east-2)
      • Lambda Function: click in box, type a character and select ‘PostReader_NewPost’
      • [Save]
      • “Add Permission to Lambda Function”
        • [OK]
  • Resources – Actions > Create Method
    • Blank dropdown: GET
    • Click the Checkmark Icon
    • – POST – Setup
      • Integration type: (*) Lambda Function
      • Use Lambda Proxy integration [ ] (do not check)
      • Lambda Region: Select your region (us-east-2)
      • Lambda Function: click in box, type a character and select ‘PostReader_GetPost’
      • [Save]
      • “Add Permission to Lambda Function”
        • [OK]
  • “Cross Origin Resource Sharing” aka CORS
    • Resources – Actions > Enable CORS
    • Enable CORS
      • This enables you to invoke your API from a website/domain name that has a different hostname to that API.  In this instance, it will be our S3 bucket.
      • Methods: [x]POST [X]GET [x]Options
      • [Enable CORS and replace existing CORS headers]
      • Confirm method changes
        • [Yes, replace existing values]
  • Resources > click ‘GET’ > Method Request
  • – GET – Method Request > URL Query String Parameters
  • + Add query string
    • Name: postId (Case sensitive!)
    • Click the Checkmark Icon
  • Resources > click ‘GET’ > Integration Request
  • – GET – Method Request > Body Mapping Templates
  • Request body passthrough
    • (*) When there are no templates defined (recommended)
    • + Add mapping template
      • Under ‘Content-Type’ (above, I know, strange): application/json
      • Click Checkmark
      • application/json code: Paste contents found here: mappings.json
      • [Save]
  • Resource – Actions > Deploy API
    • Deployment stage: [New Stage]
    • Stage name: Dev
    • Stage description: Dev
    • Deployment description: Dev
    • [Deploy]
  • dev Stage Editor – Get the URL!!
    • Invoke URL:  https://oep9d88gib.execute-api.us-east-2.amazonaws.com/dev

Deploy the website

  • Storage > S3 > Select Bucket > Permissions > Bucket Policy
  • Insert the JSON code found here: bucketpolicypermissions.json
    • Over-write “arn:aws:s3:::BUCKET_NAME” with the actual ARN of the bucket.  This is posted above the code window. arn:aws:s3:::s3.thomasandsofia.com
    • [Save]
  • Edit scripts.js
    • Replace YOUR-API-GATEWAY-HERE with the URL you saved from the API gateway.
    • Upload to the bucket
  • Upload index.html
  • Upload styles.css

 

 

LEAVE A COMMENT