Skip to main content

AWS Lambda in the Console – Creating a Website Canary

Hands-On Lab

 

Photo of

Training Architect

Length

00:30:00

Difficulty

Intermediate

In this live AWS environment, you will create your own Lambda canary function inside of the AWS Lambda console using Python3. A 'canary' in development usually refers to a program that monitors a service, site or other application for uptime and downtime. This will provide you with hands-on experience using the Lambda console to create Lambda functions, as well as solidifying your understanding of Lambda concepts. At the end of the activity, you will have created your own Lambda function to periodically check a website status and notify you of any downtime that occurs when checking the status of the site.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

AWS Lambda in the Console: Creating a Website Canary

Introduction

In development, a canary is a program that monitors a service, site, or other application for uptime and downtime. In this hands-on lab, we will create our own Lambda canary function in the AWS Lambda Console using Python3. We will configure this Lambda function to monitor a website's status and notify us of any downtime that occurs.

Log in to the AWS Management Console using the credentials provided on the lab instructions page. Make sure you're using the us-east-1 (N. Virginia) region.

The GitHub code we'll be using can be found here. Open this link in a new tab of your browser — we'll use it later.

Create a Lambda Function in the Console

  1. Navigate to the Lambda service in the AWS Console.
  2. Click Create a function (or Manage functions).
  3. On the Functions page, click Create function.
  4. Select the Author from scratch option.
  5. Under Author from scratch, enter the following settings:
    • Name: MyCanary
    • Runtime: Python 3.6
    • Role: Choose an existing role
    • Existing role: LambdaRoleLA
  6. Click Create function.

Configure a Lambda Function with a Scheduled Event

  1. Under Add triggers, click CloudWatch Events.
  2. Scroll down to Configure triggers, and enter the following settings:
    • Rule: Create a new rule
    • Rule name: MyCanaryRule
    • Rule description: Runs every minute
    • Rule type: Schedule expression
    • Schedule expression: rate(1 minute)
  3. Make sure the Enable trigger option is selected.
  4. Click Add at the bottom of the screen.
  5. Click Save at the top of the screen.
  6. Click the MyCanary icon to select it, then scroll down to the Function code section.
  7. Switch to your GitHub browser tab, and click the canary.py file name to open it.
  8. Click the Raw button, then select and copy all of the text on the page.
  9. Switch back to your Lambda Management Console browser tab.
  10. Paste the text we just copied into the code editor field, replacing what was there before.
  11. Scroll down to the Environment variables header, and enter the following:
  12. Scroll up to the code editor. Next to PhoneNumber, replace +15556668888 with your cell phone number. (Be sure to include the + and the correct country code for your phone number.)
  13. Click the Save button at the top of the page.

Test the Lambda Function

  1. Click the Test button at the top of the page.
  2. On the Configure test event menu, select the Create a new test event option. Then enter the following:
    • Event template: Amazon CloudWatch
    • Event name: testevent
  3. Click Create.
  4. Click the Test button at the top of the page.
  5. You should receive a text message on your phone that says the check has passed.
  6. Go back to the Lambda Management Console. Locate the expected environment variable, and change its value to "superdyduper".
  7. Click Save.
  8. Click Test.
  9. You should receive a text message on your phone that says the check has failed.

Conclusions

Congratulations, you've successfully completed this hands-on lab!