From 6a4c75db9b2ebd177b35b93557fbd642f73ee728 Mon Sep 17 00:00:00 2001 From: Hammy Date: Fri, 22 Jan 2021 22:39:48 +0000 Subject: [PATCH] Add lambda handler --- canary.py | 38 ++++++++++++++++++++++++++++++++++++++ main.py | 16 ---------------- 2 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 canary.py delete mode 100644 main.py diff --git a/canary.py b/canary.py new file mode 100644 index 0000000..98cbd36 --- /dev/null +++ b/canary.py @@ -0,0 +1,38 @@ +import os +from datetime import datetime +from urllib.request import Request, urlopen + +SITE = os.environ['site'] # URL of the site to check, stored in the site environment variable +EXPECTED = os.environ['expected'] # String expected to be on the page, stored in the expected environment variable + + +def validate(res): + """ + Return False to trigger the canary + + Currently this simply checks whether the EXPECTED string is present. + However, you could modify this to perform any number of arbitrary + checks on the contents of SITE. + """ + + return EXPECTED in res + + +def lambda_handler(event, context): + print(f"Checking {SITE} at {event['time']}...") + + try: + req = Request(SITE, headers={'User-Agent': 'AWS Lambda'}) + if not validate(str(urlopen(req).read())): + raise Exception('Validation failed') + + except Exception: + print('Check failed!') + raise + + else: + print('Check passed!') + return event['time'] + + finally: + print(f"Checking complete at {str(datetime.now())}") diff --git a/main.py b/main.py deleted file mode 100644 index 5596b44..0000000 --- a/main.py +++ /dev/null @@ -1,16 +0,0 @@ -# This is a sample Python script. - -# Press Shift+F10 to execute it or replace it with your code. -# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. - - -def print_hi(name): - # Use a breakpoint in the code line below to debug your script. - print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint. - - -# Press the green button in the gutter to run the script. -if __name__ == '__main__': - print_hi('PyCharm') - -# See PyCharm help at https://www.jetbrains.com/help/pycharm/