import os import sys # https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html workdir = os.getenv('LAMBDA_TASK_ROOT') version = f'{sys.version_info[0]}.{sys.version_info[1]}' additionals = [f'{workdir}/venv/lib64/python{version}/site-packages', f'{workdir}/venv/lib64/python{version}/lib-dynload', f'{workdir}/venv/lib64/python{version}/dist-packages', f'{workdir}/venv/lib/python{version}/dist-packages', f'{workdir}/venv/lib/python{version}/site-packages'] # We need to setup the Python search path in order to make use of the # virtual environment. These `additionals` can be found by inspecting # `sys.path` from any Python in an active virtual environment. sys.path = additionals + sys.path import pandas_datareader as pdr import boto3 def handle_one(event, context): symbol = event['symbol'] # To obtain a free API key, go to https://www.quandl.com reader = pdr.quandl.QuandlReader(symbols=[f'WIKI/{symbol}'], start=event['start'], end=event['end'], api_key=event['apikey']) df = reader.read() df.to_csv(f'/tmp/{symbol}.csv') s3 = boto3.resource('s3') bucket = s3.Bucket(event['bucket']) # This line requries premission to create a bucket. # If the bucket is already there, this line merely gets the bucket. bucket.create() bucket.wait_until_exists() with open(f'/tmp/{symbol}.csv', 'rb') as f: ret = bucket.put_object(Key=f'WIKI/{symbol}.csv', Body=f.read()) return { 'statusCode': 200, 'body': { 'bucket': event['bucket'], 'path': f'WIKI/{symbol}.csv' } }