Lambda – Building a serverless webpage (49)

  Amazon Web Services (AWS)

Links

Download the example files.

 

Setup

Setup S3

  • Must have Static Web Hosting enabled.
  • Files must have Public read access enabled.

When accessing S3 using a custom domain, the name of the bucket must EXACTLY match the domain name.

  • Example: new S3 bucket = “s3.thomasandsofia.com”
  • Set the CNAME for s3.thomasandsofia.com to equal your URL for the bucket.

 

Setup Lambda

  • Compute > Lambda > [Get started now] or [Create function]
  • Select Blueprint
    • [Author from scratch] a.k.a. Blank function
  • Configure triggers
    • click the “empty box” image, then select “API Gateway” from the dropdown.
    • API name: ‘LambdaMicroservice’
    • Deployment state: ‘prod’
    • Security: ‘Open’
    • [Next]
  • Configure function
    • Name: HelloCloudGurus
    • Description: HelloCloudGurus
    • Runtime: Python 3.6
    • Paste the code from the hellocloundgurus.py file linked above.
      • Feel free to change ‘Thomas Roberts’ at the bottom of the script to your own name!
    • Lambda function handler and role
      • Role: Create new role from template(s)
      • Role Name: HelloCloudGurusLambda
      • Policy templates: Simple Microservie permissions
      • [Next]
    • [Create function] – This might take several seconds, so relax a moment.
  • Locate the Lambda link
    • Compute > Lambda > HelloCloudGurus > “Triggers” tab
    • This is tricky.  I could NOT find a quick like like shown in the video!  Instead, I created my own.
    • Locate your ARN displayed under the words “API Gateway: LambdaMicroservice
      • arn:aws:execute-api:us-east-1:727545344356:y2g5acdrxg/prod/ANY/HelloCloudGurus
      • URL = https://y2g5acdrxg.execute-api.us-east-1.amazonaws.com/prod/HelloCloudGurus
        • Using this link should display the name entered at the bottom of the .py script (default ‘Thomas Roberts’)
        • If you see a JSON error, but the raw data tab displays the correct data, the script will still work, so move on!

 

Setup API Gateway

  • Application Services > API Gateway
  • You’ll see the LambdaMicroservices has already been setup for you
    • This was part of the delay earlier!
  • That’s it!  Thanks for looking

 

Update index.html to call the function

  • edit ‘index.html’ and replace the “YOUR-API-GATEWAY-LINK-HERE” with the link you created in the ‘Setup Lambda” section and save the change.
  • Upload both the index.html and error.html files to the root folder of your new bucket.
  • Enable Public read access on both files.

 

View your serverless webpage!

  • http://s3.bucketname.s3-website.region.amazonaws.com
  • When viewing the page, click the [Click here] button
    • “Cloud Gurus” should be replaced with the name you entered in the python script!

LEAVE A COMMENT