Creating an AWS Lambda Function for Scheduling
Lab Steps
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:
Â
2. Click Create function:
Â
3. In the Create function form, ensure Author from scratch is selected:
Â
4. In the Basic information section of the form, in the Function name field, enter lab-function:
Â
5. In the Runtime drop-down, select Python 3.8:
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:
Â
7. Select Use an existing role:
Â
8. In the Existing role drop-down, select lambda_basic_execution:
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:
Â
 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:
You will see a notification that your function has been deployed:
Â
Summary
In this Lab Step, you created a Lambda function and you added a simple example implementation of website monitoring to your function.
Check if the desired number of AWS Lambda functions have been created