Created
November 29, 2017 03:15
-
-
Save kaidokert/e8ab534af3650018ee20a04461cb4458 to your computer and use it in GitHub Desktop.
Simple AWS Lambda script that self-installs dependencies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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