Quotes

Friday, June 28, 2019

Python Numpy AWS Lambda


On my AWS account I created a Python 3.7 Lambda function using a deployment package with NumPy library following the below steps [5]:

1) I launched an EC2 instance with Amazon Linux AMI which is used by AWS Lambda Python 3.7 runtime [3]

2) I updated my instance with this command "sudo yum update -y" and then installed Python 3.7 following the steps in this link [6]:

               $ python3 --version
               Python 3.7.0

               $ pip3 --version
               pip 10.0.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)

3) Created a new directory

               $ mkdir my_package_dir
               $ cd my_package_dir

4) Installed NumPy

               $ pip3 install numpy -t .

               # Successfully installed numpy-1.16.4

5) My Lambda function handler is

               $ nano lambda_function.py

               import numpy as np

               def lambda_handler(event, context):
                              a = np.arange(15).reshape(3, 5)

6) Finally I created a zip file including the NumPy library and my function's code in the handler file

               $ zip -r9 ../function.zip .

Then I used this "function.zip" file as my deployment package in my Python 3.7 Lambda function and I confirm that it works as expected. The above steps are also working with Python 3.6 which is included in the yum repository and can be installed using this command "sudo yum install -y python36", instead of pip3 you will use pip-3.6, all other steps are the same.