Skip to main content

Adding 3rd Party Dependencies

If you have a third party dependency, you can include it in the enclave by compiling it using Docker. Using this method ensures the compiled dependencies will be compatible with the runtime of the enclave.

For this example, we will deploy a function that returns the current time based on a specific timezone using the Pendulum library. You can find the code for this example, here, in the functions repository. The project structure looks like this:

$ ls pendulum/
app.py requirements.txt

Where pendulum/requirements.txt looks like:

pendulum==2.1.2

and pendulum/app.py looks like:

import pendulum

def cape_handler(timezone_bytes):
timezone = timezone_bytes.decode()
now = pendulum.now(timezone)
return now.to_time_string()

To deploy our function, let's first create a deployment folder where we'll add the function and the dependency. We create a deployment folder to keep things organized. But if you prefer, you could compile the dependencies directly in the pendulum/ folder.

mkdir pendulum-deployment
cp pendulum/app.py pendulum-deployment/.

We can add the pendulum dependency with:

docker run -v `pwd`:/build -w /build --rm -it python:3.9-slim-bullseye pip install -r pendulum/requirements.txt --target pendulum-deployment/

Note: If you are using an ARM processor (like newer Macs using an M1 or M2 chip), you will first need to pull the Docker image for the amd64 platform using:

docker pull --platform linux/amd64 python:3.9-slim-bullseye

If running on Windows with command prompt, the command becomes:

docker run -v %cd%:/build -w /build --rm -it python:3.9-slim-bullseye pip install -r pendulum/requirements.txt --target pendulum-deployment/

Once done, you should see the following files in your deployment folder:

$ ls pendulum-deployment/
app.py pendulum python_dateutil-2.8.2.dist-info pytzdata-2020.1.dist-info six.py
dateutil pendulum-2.1.2.dist-info pytzdata six-1.16.0.dist-info

We are now ready to deploy our function which uses pendulum:

$ cape deploy pendulum-deployment/

Once you have received the function id from cape deploy, you can call the function as follow. You should see the current time based on the timezone provided

cape run 2zbK8Fong64smU2GVuFMmS Europe/Paris

VoilĂ , you have deployed a python function requiring a dependency!