Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda function to make simple HTTP request

I want to use AWS lambda to make a cronjob-style HTTP request. Unfortunately I don't know Python.

I found they had a "Canary" function which seems to be similar to what I want. How do I simplify this to simply make the HTTP request? I just need to trigger a PHP file.

from __future__ import print_function

from datetime import datetime
from urllib2 import urlopen

SITE = 'https://www.amazon.com/'  # URL of the site to check
EXPECTED = 'Online Shopping'  # String expected to be on the page


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:
        if not validate(urlopen(SITE).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())))
like image 450
Amy Neville Avatar asked Jun 14 '16 18:06

Amy Neville


1 Answers

This program will "simply make the HTTP request."

from urllib2 import urlopen

SITE = 'https://www.amazon.com/'  # URL of the site to check

def lambda_handler(event, context):
    urlopen(SITE)
like image 94
Robᵩ Avatar answered Nov 02 '22 08:11

Robᵩ