This workshop will show you how to use Pandas lib in your Lambda function, which implement with Lambda layer.
-
Configure settings Leave all setting as default and click Next Step
-
Review Click Create environment to create you Cloud9 environment
Create a new Lambda Layer which include Pandas and required python lib.
Create an requirement.txt file for pip to install selected lib from file, with File > New File File in navigation bar.
pandas==0.23.4
pytz==2018.7
Next, create an script called prepare_layer_packages.sh with File > New File File in navigation bar.
#!/bin/bash
export DIR="python"
rm -rf ${DIR} && mkdir -p ${DIR}
docker run --rm -v $(pwd):/foo -w /foo lambci/lambda:build-python3.6 \
pip install -r requirement.txt --no-deps -t ${DIR}
https://aws.amazon.com/tw/premiumsupport/knowledge-center/lambda-layer-simulated-docker/
Run command as following in your terminal. And then download the my-pandas23-layer.zip in your environment panel and right click it and then Download it.
chmod +x prepare_layer_packages.sh
./prepare_layer_packages.sh
zip -r my-pandas23-layer.zip .
- Input my-pandas23-layer as your Layer name, and then upload your my-pandas23-layer.zip file. In the end, select python 3.6 as runtime.
Use required Lambda layers into your Lambda functions
Creating Lambda function and using build-in Lambda layer and customized layer you created.
- Testing Lambda function working like expected.
import json
import numpy as np
import pandas as pd
def lambda_handler(event, context):
# TODO implement
dates = pd.date_range('20181201', periods=6)
df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))
print(df)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}