OpenFaaS: How to Add Python Requirements and Dependencies

This guide will show you how to add requirements and dependencies for a Python project using OpenFaaS.

Python dependencies are software components that your project needs for it to work. You can manually use PyPI (the Python Package Index) to provide packages that you need, but OpenFaaS can automate this for you.

Let's Get Started!

First, let's use the faas-cli new command to create a new python3 function from the templates.

See below:

faas-cli new --lang python3 my-function

Result:

2021/10/31 00:10:12 No templates found in current directory.
2021/10/31 00:10:12 Attempting to expand templates from https://github.com/openfaas/templates.git
2021/10/31 00:10:13 Fetched 14 template(s) : [csharp dockerfile go java11 java11-vert-x node node12 node12-debian node14 php7 python python3 python3-debian ruby] from https://github.com/openfaas/templates.git
Folder: my-function created.
  ___                   _____           ____
 / _ \ _ __   ___ _ __ |  ___|_ _  __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
| |_| | |_) |  __/ | | |  _| (_| | (_| |___) |
 \___/| .__/ \___|_| |_|_|  \__,_|\__,_|____/
      |_|


Function created in folder: my-function
Stack file written: my-function.yml

Notes:
You have created a Python3 function using the Classic Watchdog.

To include third-party dependencies create a requirements.txt file.

For high-throughput applications, we recommend using the python3-flask
or python3-http templates.

This command will then create a few files in the folder in which you ran the command.

See below:

.
├── my-function
│   ├── handler.py
│   ├── __init__.py
│   └── requirements.txt
├── my-function.yml
└── template

This guide will focus on the requirements.txt and handler.py files. You use the requirements.txt file to set up the python-pip modules that you need. And the handler.py file has the code you are writing.

Add Your OpenFaaS Python Requirements

This guide will show you how to add the "scanless" module for Python. You can, of course, add any module that you need. This module is a Python 3 command-line utility for using websites to perform port scans on your behalf.

To add the logging module, add the following line to your requirements.txt file:

scanless

Next, edit your handler.py file and replace the contents with the following:

import scanless
import logging

def handle(req):
    logging.warning('-- Start of Code --')

    sl = scanless.Scanless()
    output = sl.scan('scanme.nmap.org', scanner='hackertarget')

    return output['raw']

The first import is "scanless", and the faas-cli will install to your image once you start the build.

The second import is "logging", and you did not add it to the requirements.txt. And you don't need to since it's part of the Python standard library. Therefore, it is available as soon as you install Python.

Now you must build, push and deploy your function using the faas-cli up command:

faas-cli up -f my-function.yml

Finally, you can run your function using the following command:

curl http://127.0.0.1:8081/function/my-function

You should see the following result with the warning message as well as the port scan results:

WARNING:root:-- Start of Code --
Starting Nmap 7.40 ( https://nmap.org ) at 2021-11-05 07:37 UTC
Nmap scan report for scanme.nmap.org (45.33.32.156)
Host is up (0.069s latency).
Other addresses for scanme.nmap.org (not scanned): 2600:3c01::f03c:91ff:fe18:bb2f
PORT     STATE  SERVICE
21/tcp   closed ftp
22/tcp   open   ssh
23/tcp   closed telnet
80/tcp   open   http
110/tcp  closed pop3
143/tcp  closed imap
443/tcp  closed https
3389/tcp closed ms-wbt-server

Nmap done: 1 IP address (1 host up) scanned in 0.30 seconds

Wrapping Up

As you can see, it is easy to add your Python dependencies to an OpenFaaS Project. In addition, Python provides some of the dependencies out-of-the-box by Python.

You May also be Interested in

Source:



About the Authors

Anto's editorial team loves the cloud as much as you! Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee



Leave a Reply

Your email address will not be published. Required fields are marked *