AWS> Лямбда> Канареечная функция - PullRequest
0 голосов
/ 02 августа 2020

Я хочу использовать функцию Lambda для мониторинга нескольких веб-сайтов , как бы вы изменили приведенные ниже коды (из BluePrint)?

Можно настроить несколько переменных env (например, site2 , ожидалось2), мне просто нужна помощь, чтобы заставить их работать внутри функции.

Спасибо.

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('Checking {} at {}...'.format(SITE, event['time']))
    try:
        req = Request(SITE, headers={'User-Agent': 'AWS Lambda'})
        if not validate(str(urlopen(req).read())):
            raise Exception('Validation failed')
    except:
        print('Check failed!')
        raise
    else:
        print('Check passed!')
        return event['time']
    finally:
        print('Check complete at {}'.format(str(datetime.now())))


...