AWS Lambda and Python

From the above list, it’s clear that every Python runtime uses Amazon Linux, which is a distribution that evolved from CentOS and Red Hat Enterprise Linux (RHEL).

Why this information is useful? It is useful because if we are using an external package in our Python app, it has to be compatible with that specific Linux distro (see what’s behind pip here: https://pydist.com/blog/pip-install).

The main “problem” is we don’t have control of what can be installed on a Lambda environment due to its serverless nature (which is a huge benefit from a cloud management perspective).

The solution is simple: bundle and ship our script/s and the package/s together.

Considering the above information, the next natural step is to build and bundle the entire application using the AWS Docker Amazon Linux 2018.03 image (https://hub.docker.com/_/amazonlinux)

Create the developer environment

In this section, we will see how to set up from scratch our Python 3.7 environment using the AWS Docker image.

Let’s start by creating a Dockerfile based on the Amazon Linux image, then add some lines to install Python 3.7 and pip.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

FROM amazonlinux:2018.03

RUN yum update-y

RUN yum install-y\

gcc\

openssl-devel\

zlib-devel\

libffi-devel\

wget&amp;&amp;\

yum-yclean all

WORKDIR usr/src

# Install Python 3.7

RUN wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz

RUN tar xzf Python-3.7.4.tgz

RUN cd Python-3.7.4;./configure--enable-optimizations;make altinstall

RUN rm Python-3.7.4.tgz

RUN rm-rf Python-3.7.4

RUN python3.7-V

# Install pip

RUN wget https://bootstrap.pypa.io/get-pip.py

RUN python3.7get-pip.py

RUN rm get-pip.py

RUN pip-V

Finally, we can build our image with the following command where amazonlinux-py3.7 is an arbitrary image tag.

1

docker build-tamazonlinux-py3.7.

A complete example

Let’s try to create a simple function which calls an API and returns a response.

From the terminal, we can create a local folder for this project, then move inside it.

1

2

mkdir lambda_python

cd lambda_python

Inside our project directory we can create a file called lambda_function.pywith the following code:

If the .zip bundle is bigger than the supported size there are at least two other alternatives:

Bundle the rest(or part) of the files/packages in an S3 bucket and deploy the main .py handler in Lambda. The main handler for each call must download and use the S3 files (example here). This is possible since the current Lamda function and layer storage is 75 GB but could cause some performance issue.