Creating an AWS Lambda Function for Scheduling

Lab Steps

lock
Logging In to the Amazon Web Services Console
lock
Creating an AWS Lambda Function for Scheduling
lock
Scheduling an AWS Lambda Function Using Amazon EventBridge Scheduler
lock
Verifying Your AWS Lambda Function Is Running on a Schedule
lock
Creating a Dashboard to Monitor your AWS Lambda function
Need help? Contact our support team

Here you can find the instructions for this specific Lab Step.

If you are ready for a real environment experience please start the Lab. Keep in mind that you'll need to start from the first step.

Introduction

AWS Lambda is a fully-managed, highly-scalable serverless function service.

In this Lab Step, you will create a Lambda function that fetches a web page and inspects the contents.

 

Instructions

1. In the AWS Management Console search bar, enter Lambda, and click the Lambda result under Services:

alt

 

2. Click Create function:

alt

 

3. In the Create function form, ensure Author from scratch is selected:

alt

 

4. In the Basic information section of the form, in the Function name field, enter lab-function:

alt

 

5. In the Runtime drop-down, select Python 3.8:

alt

Runtimes are programming languages that are used to execute function code. Lambda supports a number of different runtimes, the most popular are Python and NodeJS. In this Lab Step, you will implement your function in Python.

 

6. Click Change default execution role:

alt

 

7. Select Use an existing role:

alt

 

8. In the Existing role drop-down, select lambda_basic_execution:

alt

This role has been pre-populated for this Lab. It allows the function to create logs in Amazon CloudWatch. In a non-lab environement, when creating a Lambda function, you will create your own role beforehand or use an appropriate pre-existing role.

 

9. To create your function, scroll to the bottom and click Create function:

alt

 

 10. In the Code source section, double click the lambda_function.py file, and replace the code in the editor textbox with the following:

Copy code
1
2
3
4
5
6
7
8
9
10
11
12
13
from urllib.request import urlopen
URL = 'https://cloudacademy.com'
TEXT = 'AWS'
def lambda_handler(event, context):
    print('Event: %s' % event)
    t = event.get('time') or "?"
    with urlopen(URL) as u:
        output = u.read().decode('utf-8')
        if output:
            if TEXT in output:
                return "SUCCESS at %s" % t
    print('%s not found in %s at %s' % (TEXT, URL, t))
    return "FAILURE"

This code fetches the contents of the front page of the CloudAcademy website. If the page contains the text "AWS" it returns "SUCCESS" along with a timestamp.

In this lab the return value is not important, because the goal of the Lab is to run the function on a schedule. When you implement Lambda functions you can use the return value to pass data or a result back to the system that called the function.

 

11. To deploy your function, at the top of the Code source section, click Deploy:

alt

You will see a notification that your function has been deployed:

alt

 

Summary

In this Lab Step, you created a Lambda function and you added a simple example implementation of website monitoring to your function.

Validation checks
1Checks
Created the desired number of Lambda functions

Check if the desired number of AWS Lambda functions have been created

AWS Lambda