AWS Lambda Python Development Package on Ubuntu 18.04

Kade Killary · 2019.06.29 · 3 minutes until it's over

aws_lambda_header

AWS Lambda is a convenient way to run and deploy simple functions. However, for Python, importing external packages can be a pain. There are a handful of ways to solve this problem, perhaps most practically by using Docker. Nevertheless, in this tutorial I’ll demonstrate deploying a simple function using numpy from a remote Ubuntu server.

Remote Server

The first step is to acquire a VPS. For this example I chose to use DigitalOcean. You can use the following link to get a $50 dollar credit. Other services to consider are Linode, Vultr, AWS EC2 and Google Compute Engine.

Basics

After spinning up and logging into your remote server, create the following script:

$ touch install.sh

Copy the following commands into the script.

#!/usr/bin/env bash

sudo apt update
sudo apt full-upgrade --yes
sudo apt autoremove --yes

sudo apt install zip --yes
sudo apt install python3-pip --yes

Make the script executable.

$ chmod +x install.sh

Run the script.

$ ./install.sh

AWS CLI

Install the Amazon Command Line Interface for easily interacting with AWS services.

$ pip3 install awscli

In order to configure the awscli you’ll need to have, or create, an IAM User. You can follow this guide to generate an access key.

Your credentials should look similar to below:

Access Key ID: AKIAIOSFODNN7EXAMPLE
Secret Access Key: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

Run aws configure and enter your credentials.

$ aws configure
# AWS Access Key ID [None]:
# AWS Secret Access Key [None]:
# Default region name [None]:
# Default output format [None]:

Test Function

Create directory numpy_tester and cd into it.

$ mkdir numpy_tester

$ cd numpy_tester

Create the lambda function numpy_tester.py and add the following:

import numpy as np

def handler(event, context):

    a = np.arange(15).reshape(3, 5)

    print("Your numpy array:")
    print(a)

if __name__ == "__main__":
    handler('', '')

Numpy

In order to access numpy in our function we will need to install the package directly.

$ pip3 install numpy -t .

You can test the function locally by running the following command:

$ python3 numpy_tester.py
# Your numpy array:
# [[ 0  1  2  3  4]
#  [ 5  6  7  8  9]
#  [10 11 12 13 14]]

Deployment

To create the deployment package we need to zip all files included in our function. The -r9 flag will include hidden files.

$ zip -r9 numpy_tester.zip .

In order to deploy we must specify a role-arn. The simplest way to get one is via the AWS console.

Next up, create an S3 bucket and upload the zip file.

$ aws s3 mb s3://numpy-tester

$ aws s3 cp numpy_tester.zip s3://numpy-tester/

Lastly, check the version of python 3. The value listed is what we will use for the runtime parameter below.

$ python3 --version
# Python 3.6.8

Create the Lambda using the awscli command create-function. You will need to specify a value for both the region and role-arn parameters - everything else you can leave as is.

$ aws lambda create-function \
    --region region \
    --function-name numpy_tester \
    --code S3Bucket=numpy-tester,S3Key=numpy_tester.zip \
    --role role-arn \
    --handler numpy_tester.handler \
    --runtime python3.6 \
    --profile default
    --timeout 10 \
    --memory-size 1024

Navigate to the AWS console. You should see the following:

lambdacreation

The final check is to test the function. Select the Test button in the upper right hand corner of the UI. If the run is successful you should see the following output.

test

The only thing left to do is destroy the original server. That’s it. If you want to update the code at some point you can reference the awscli command update-function-code.