Skip to content

awshktsa/AWSTPEWorkshop-20200320-Lambda-with-Pandas

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

AWSTPEWorkshop-20200320-Lambda-with-Pandas

This workshop will show you how to use Pandas lib in your Lambda function, which implement with Lambda layer.


1. Prepare environment

a. Create a cloud9 environment

  1. Start to create AWS Cloud9 Image

  2. Name environment Image

  3. Configure settings Leave all setting as default and click Next Step

  4. Review Click Create environment to create you Cloud9 environment


2. Create a Lambda Layer

Create a new Lambda Layer which include Pandas and required python lib.

a. Create an required.txt file

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

b. Create a script to run and prepare required resources

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/

c. Package files and download it

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 .

d. Create Lambda layer in AWS

  1. Create a new AWS Lambda Layer Image

Image

  1. 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. Image

Image


3. Use Lambda layers into functions

Use required Lambda layers into your Lambda functions

a. Creating Lambda function with Lambda Layer

Creating Lambda function and using build-in Lambda layer and customized layer you created.

  1. Create a new Lambda function called pandas-sample. Image

  2. Add new Layer to this function Image

  3. Add AWS provided AWSLambda-Python36-SciPy1x Image

Image

  1. Add your layer my-pandas23-layer Image

b. Testing Lambda function

  1. 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!')
    }
  1. Config a test event Image

  2. Test your Lambda function with Lambda Layer Image

About

Lambda with Pandas and implement with Lambda layer

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published