Skip to content

Instantly share code, notes, and snippets.

@kaidokert
Created November 29, 2017 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaidokert/e8ab534af3650018ee20a04461cb4458 to your computer and use it in GitHub Desktop.
Save kaidokert/e8ab534af3650018ee20a04461cb4458 to your computer and use it in GitHub Desktop.
Simple AWS Lambda script that self-installs dependencies
import json
import logging
import pip
import site
# installed by lambda env
# import six
# import boto3
# from dateutil.easter import *
# preserve lambda-installed handler
PRE_HANDLERS = logging.root.handlers
def resetLoggers():
global PRE_HANDLERS
logging.root.handlers = []
logging.basicConfig(level=logging.DEBUG)
for h in PRE_HANDLERS:
logging.getLogger().addHandler(h)
def script_main(log):
import requests
import html5lib
data = requests.get('https://httpbin.org/anything')
log.info('all the data %r %r', data, data.json())
def lambda_handler(event, context):
resetLoggers()
log = logging.getLogger(__name__)
log.debug('Received event: ' + json.dumps(event, indent=2))
pip.main(['install', 'requests','html5lib', '-t', '/tmp/stage'])
resetLoggers()
site.addsitedir('/tmp/stage')
log = logging.getLogger(__name__)
try:
script_main(log)
except ImportError as e:
log.exception('Import failed')
return 'OK'
if __name__=='__main__':
logging.basicConfig(level=logging.DEBUG)
script_main(logging.getLogger())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment