9

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())))
Jedi
  • 3,088
  • 2
  • 28
  • 47
Amy Neville
  • 10,067
  • 13
  • 58
  • 94

3 Answers3

6

I found out a way to do requests in AWS Lambda.

Courtesy of https://stackoverflow.com/a/58994120/129202 - this answer has a couple of other solutions too, that could be worth checking out. The below one worked for me.

import urllib.request
import json

res = urllib.request.urlopen(urllib.request.Request(
        url='http://asdfast.beobit.net/api/',
        headers={'Accept': 'application/json'},
        method='GET'),
    timeout=5)

print(res.status)
print(res.reason)
print(json.loads(res.read()))
Jonny
  • 15,955
  • 18
  • 111
  • 232
5

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)
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Thanks for the help - I feel so stupid to ask such a simple thing but I've never learned python and all the languages I know aren't on their list :) – Amy Neville Jun 14 '16 at 18:38
  • Will this run in the frequency set in Lambda? Where is that set in this function? – Amy Neville Jun 14 '16 at 18:41
  • My understanding is that you use the AWS Lambda console or CLI to specify the frequency. When asked for a function name, specify ".lambda_handler", and the above function will be invoked at the frequency you've specified. – Robᵩ Jun 14 '16 at 18:48
  • ""errorMessage": "Unable to import module 'lambda_function': No module named 'urllib2'"," – Jonny Feb 15 '22 at 13:14
  • 1
    @Jonny - This answer is specific to the now-obsolete Python 2. You may want to consult https://docs.python.org/3/library/urllib.request.html – Robᵩ Feb 16 '22 at 00:05
  • Yes I got urrlib.request working https://stackoverflow.com/a/71127429/129202 – Jonny Feb 16 '22 at 07:19
5

You can use Requests (http://docs.python-requests.org/en/master/) to work with the response in a easier way.

import requests

URL = 'https://www.amazon.com/'
def lambda_handler(event, context):
    requests.get(URL)
  • 2
    It seemed to me that `requests` wasn't available in the default lambda environment. Do you know if it is? – Robᵩ Jun 14 '16 at 18:52
  • I know I'm stupid for asking this, but how do I time this to run every 1 day in lambda? When creating custom functions it doesn't seem to have the cron options that the canary function had - or do I just modify the canary function? – Amy Neville Jun 14 '16 at 18:55
  • @AmyNeville, try reading this tutorial: http://docs.aws.amazon.com/lambda/latest/dg/with-scheduledevents-example.html or this reference: http://docs.aws.amazon.com/lambda/latest/dg/with-scheduled-events.html – Robᵩ Jun 14 '16 at 19:03
  • 1
    @Robᵩ I think not, the library must be installed. – Victor Oliveira Arêas Jun 14 '16 at 19:04
  • 14
    requests module must be imported like this: `from botocore.vendored import requests` – lost in binary Mar 13 '18 at 09:16
  • 1
    requests is going to be removed from botocore.vendored as stated here: https://aws.amazon.com/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/ although in this page it mentions Oct 19', it still works for me, but the log messages state 3/31/2020 as the final date: This dependency was removed from Botocore and will be removed from Lambda after 2020/03/31 – kalsky Mar 28 '20 at 18:27
  • Is there anyway to actually do requests from lambda? I'm specifically using lambda@edge. Neither requests nor urllib2 modules exist. – Jonny Feb 15 '22 at 13:27