YD's blog

Posted 二 23 8月 2016

Lambda 分析機環境建置

Lambda Tutorial

安裝套件 virtualenv

pip install virtualenv

建立 virtual environment 並執行環境內的cmd模式

mkdir env
source env/bin/activate
deactivate //離開虛擬環境之cmd模式

安裝完所需 python 套件後,將套件與腳本放入根目錄,腳本含兩種:

  1. init.py (內容為空白)
  2. main.py (主要執行檔),以下範例
# !/usr/bin/env python
# -*- coding: utf-8 -*-
def lambda_handler(event, context):
  your code here

最後打包成.zip檔上傳至lambda即可測試 (上傳最大限制為5mb)


Case by Case

AWS linux environment compile (以EC2 為例,編譯含C語言之python套件)

進入ec2

sudo yum -y update
sudo yum -y groupinstall "Development Tools"
sudo yum install python27-devel python27-pip gcc
sudo yum install blas-devel lapack-devel //for scipy
sudo yum install python-devel //for python-igraph
sudo yum install -y libxml2 libxml2-devel libxslt libxslt-devel //for puthon-igraph
virtualenv ~/env --no-site-packages
source ~/env/bin/activate
//這裏開始進入切割出的虛擬環境中安裝所需套件,不過不知為何不能下sudo,不然會安裝到系統的python(可能是AWS sudo的特殊權限)
pip install --upgrade pip
pip install Cython
pip install numpy
pip install scipy
pip install scikit-learn
pip install gensim
pip install python-igraph
pip install pandas
//回到系統安裝
sudo yum install unzip
sudo yum install boost-devel boost-python-devel
//再次進入虛擬環境中安裝,不過這裡的 pymongo 和 elasticsearch 會安裝在 env/lib/python2.7/dist-packages/ 而不是 env/lib64/python2.7/site-packages/
pip install elasticsearch //but put in the diet-package
easy_install pymongo //but put in the diet-package


接著將需要的binary file上傳到ec2中 scp -r -i [pemPath] [.binlocalPath] ec2-user@[ip]:/home/ec2-user/env

修改 binary file 權限 chmod +x env/SIO2-Algorithm_GPL.bin

執行 binary file (此檔案會出現於env/lib64/python2.7/dist-packages/,將其移動到 env/lib/python2.7/dist-packages 與相依套件一起包下來) env/SIO2-Algorithm_GPL.bin

拉下編譯過的套件 scp -r -i [pemPath] ec2-user@[ip]:/home/ec2-user/env/lib/python2.7/dist-packages [localDirPath]

scp -r -i [pemPath] ec2-user@[ip]:/home/ec2-user/env/lib/python2.7/site-packages [localDirPath]

除此之外,還有一些零散的資料夾也一併拉下:

/usr/lib64/libboost_python.so.1.53.0 /usr/lib64/libquadmath.so.0 /usr/lib64/libgfortran.so.3 /usr/lib64/libblas.so.3 /usr/lib64/liblapack.so.3

將編譯過的套件連同執行腳本打包成zip檔,超過5mb則丟到S3

aws configure
keyID...
pw ...
region ...
aws s3 cp [local.zipPath] s3://[bucketName]/[dir]

Other solutions

Category: WorkNote
Tags: AWS Lambda