Paul Ahuevo
Paul Ahuevo

Reputation: 131

IBM Cloud Functions - How to use and upload own libraries?

Is it possible to use / upload own libraries to IBM Cloud Functions? Or is it limited to the preinstalled packages? I plan to use Python as programming language.

Upvotes: 1

Views: 2107

Answers (3)

data_henrik
data_henrik

Reputation: 17118

It is possible to use more libraries than the preinstalled ones. There are some tips & tricks in the IBM Cloud Functions docs and the linked blog articles, e.g., here for Python.

For Python you can either use a virtual environment and package that up or use a zip file with the required Python files. The virtual environment might be easier to start with, but you could end up with a lot of unnecessary files. What I prefer is to download the required files and put them into a zip file on my own. Of course, this is only manageable to a certain degree.

I used that method in this IBM Cloud solution tutorial on serverless GitHub traffic statistics. You can find the source code, including the zip file I created for the Python action, in this GitHub repository (see the functions folder).

Upvotes: 1

msciab
msciab

Reputation: 11

You can use any docker image to execute your actions, as long as the images are available on Docker Hub. So you can create your own image with your libraries.

So, for example, if you want your own image that adds the python library yattag, a library generating HTML from python code.

you can write a Dockerfile like this:

FROM openwhisk/python3action
RUN pip install yattag

and then build and push

$ docker build -t docker.io/msciab/python3action-yattag:latest .
$ docker push docker.io/msciab/python3action-yattag

Now you have a public image you can use in OpenWhisk/IBM Cloud.

Here is a simple python hello world using yattag:

from yattag import Doc

def main(dict):
  doc, tag, text = Doc().tagtext()
  with tag('h1'):
     text('Hello world!')
  dict['body'] = doc.getvalue()
  return dict

create and run the action:

$ wsk action create  hello-yattag hello.py --web true --docker msciab/python3action-yattag
$ curl $(wsk action get hello-yattag --url|tail -1)
<h1>Hello world!</h1>

Upvotes: 0

user6062970
user6062970

Reputation: 831

You can bundle your own dependencies. See the docs here https://github.com/apache/incubator-openwhisk/blob/master/docs/actions-python.md#packaging-python-actions-with-a-virtual-environment-in-zip-files for creating a virtual environment with your libraries. The docs provide an example installing dependencies via requirements.txt.

Upvotes: 3

Related Questions