[PYTHON] I want to use an external library with IBM Cloud Functions

Various libraries are provided by default for the languages that are supported by IBM Cloud Functions as standard. However, command line development is required to develop using libraries that are not in the default. Also, Java does not have a GUI development environment, so command line development is essential. So, this time I will summarize what I got stuck when developing Cloud Functions from the command line using Python as an example. In addition, this time we will proceed on the assumption that docker is installed in advance. (If you develop in another language, I think that you can develop it if you read it properly.)

First, try to make it according to the document

This time I would like to install python's LINE bot SDK. Here Refer to the document and create the contents of `` `requirements.txt``` as follows. I will.

# Requirements.txt contains a list of dependencies for the Python Application #

# Setup modules
gevent == 1.4.0
flask == 1.0.2

# default available packages for python3action
beautifulsoup4 == 4.8.0
httplib2 == 0.13.0
kafka_python == 1.4.6
lxml == 4.3.4
python-dateutil == 2.8.0
requests == 2.22.0
scrapy == 1.6.0
simplejson == 3.16.0
virtualenv == 16.7.1
twisted == 19.7.0
PyJWT == 1.7.1

# packages for numerics
numpy == 1.16.4
scikit-learn == 0.20.3
scipy == 1.2.1
pandas == 0.24.2

# packages for image processing
Pillow == 6.2.1

# IBM specific python modules
ibm_db == 3.0.1
cloudant == 2.12.0
watson-developer-cloud == 2.8.1
ibm-cos-sdk == 2.5.1
ibmcloudsql == 0.2.23

# Compose Libs
psycopg2 == 2.8.2
pymongo == 3.8.0
redis == 3.2.1
pika == 1.0.1
elasticsearch == 6.3.1
cassandra-driver == 3.18.0
etcd3 == 0.10.0

#Additional modules
line-bot-sdk

After creating the text file, use the docker command to get the operating environment of Functions and create a package of the virtual environment of Python.

$ docker pull ibmfunctions/action-python-v3.7
$ docker run --rm -v "$PWD:/tmp" ibmfunctions/action-python-v3.7 bash -c "cd /tmp && virtualenv virtualenv && source virtualenv/bin/activate && pip install -r requirements.txt"

Below is the code (`` `__ main__. Py```) that runs when deployed.

__main__.py


def main(args):
    return {"result":"OK!"}

Compress the package and code you just created into a zip and create the action from the command line. In addition, continue while logged in with the IBM Cloud command.

$ zip -r hellobot.zip virtualenv __main__.py
$ ibmcloud fn action create hellobot hellobot.zip --kind python:3.7

Then, the execution result was output to the following.

error: Unable to create action 'my-action-name': The connection failed, or timed out. (HTTP status code 413)

Oh? Why do I get this error even though I haven't typed it?

I tried to find out the cause

After investigating the cause of this error, I found this article on Stackoverflow. IBM Cloud functions - Unable to create an action According to the answer of the person responsible for IBM Cloud Functions, this seems to be a bug on the command line side, and it seems that actions cannot be created this way. (Write it in the document)

Create actions using Docker Hub (read here if you're busy)

Not only did the Stackoverflow mentioned earlier say that it can't be used because it's a bug, but it also offered an alternative solution, so I'd like to try it there next time. Refer to here, build your own virtual environment, upload it to Docker Hub, and then take action. Create. This method is easy for those who are accustomed to using Docker, but it may be difficult for those who have never touched it.

Create an execution environment

First, prepare a Dockerfile for the action execution environment.

FROM openwhisk/actionloop-python-v3.7:36721d6

COPY requirements.txt requirements.txt

RUN pip install --upgrade pip setuptools six && pip install --no-cache-dir -r requirements.txt

As you can see from the Dockerfile, the `` `requirements.txt``` that appears in this is the same as the one created earlier, so the contents are omitted. Now that you have the necessary files, execute the following command to create an image. It will take some time to complete.

$ docker build -t linebot_function .

Upload the created image to Docker Hub. If you have not registered with Docker Hub, please register from here. After registering, sign in to Docker Hub from the Docker GUI screen. After signing in, use the following command to tag and upload the created image.

$ docker tag linebot_function YOUR_USER_NAME/linebot_function
$ docker push YOUR_USER_NAME/linebot_function

Create an action

Now that we have the execution environment ready, let's prepare the code to run it. This time, I installed the LINE bot Python SDK as an external library, so check if it is included properly. Therefore, prepare the following code.

first-linebot.py


import linebot

def main(args):
    return {"LINEbot":linebot.__version__}

Once you have the code, it's time to create the action. First, install the IBM Cloud Functions plug-in.

$ ibmcloud plugin install cloud-functions

Target the resource group with the following command. For the group name, log in from here to check.

$ ibmcloud target -o <org> -s <space> 

Now that you have the code, create an action with the following command.

$ ibmcloud fn action create first-linebot --docker YOUR_USER_NAME/linebot_function first-linebot.py

If the result is returned without any error, it is successful.

Operation check

Check the operation of the created action. However, since the operation of the action created in the Docker execution environment cannot be confirmed from the GUI, this is also performed by operating the command line. Execute the following command.

$ ibmcloud fn action invoke first-linebot --result

If the LINE bot SDK version is output as a result after execution, the action is working fine in your own environment.

{
    "LINEbot": "1.15.0"
}

I think this will expand the range of actions you can create. I will try to create a serverless LINE bot using the environment built this time.

You can see the article that made the LINE bot from here.

Recommended Posts

I want to use an external library with IBM Cloud Functions
I want to use R functions easily with ipython notebook
I want to use MATLAB feval with python
I want to be an OREMO with setParam!
I want to use Temporary Directory with Python2
I don't want to use -inf with np.log
I want to use ip vrf with SONiC
I want to convert an image to WebP with lollipop
[Python] I want to use the -h option with argparse
I want to use a virtual environment with jupyter notebook!
I want to do ○○ with Pandas
I want to debug with Python
I want to use a wildcard that I want to shell with Python remove
How to use an external editor for Python development with Grasshopper
I really want to use GitHub Flavored Markdown (GFM) with Pelican!
I want to detect objects with OpenCV
I want to blog with Jupyter Notebook
I want to use jar from python
I want to use Linux on mac
I want to pip install with PythonAnywhere
I want to analyze logs with Python
I want to play with aws with python
I want to use IPython Qt Console
I want to make an automation program!
I want to operate DB using Django's ORM from an external application
Tips for manipulating numpy.ndarray from c ++ -I want to use an iterator-
[AWS] [GCP] I tried to make cloud services easy to use with Python
I want to create an Ubuntu chrome User Profile with Colab only
I tried to use lightGBM, xgboost with Boruta
I want to analyze songs with Spotify API 2
I want to mock datetime.datetime.now () even with pytest!
I want to display multiple images with matplotlib.
I want to knock 100 data sciences with Colaboratory
I want to make a game with Python
I tried to detect an object with M2Det!
I want to use ceres solver from python
#Unresolved I want to compile gobject-introspection with Python3
I want to solve APG4b with Python (Chapter 2)
Serverless LINE bot made with IBM Cloud Functions
I want to start over with Django's Migrate
I want to write to a file with Python
I want to use the activation function Mish
I want to send Gmail with Python, but I can't because of an error
I want to write an element to a file with numpy and check it.
I want to get / execute variables / functions / classes of external files from Python
I want to use mkl with numpy and scipy under pyenv + poetry environment
[Python] I want to add a static directory with Flask [I want to use something other than static]
[Python] I want to use only index when looping a list with a for statement
I want to detect unauthorized login to facebook with Jubatus (1)
I want to transition with a button in flask
I want to use self in Backpropagation (tf.custom_gradient) (tensorflow)
I want to handle optimization with python and cplex
I want to develop an Android application on Android (debugging)
I wanted to use the Python library from MATLAB
I tried to implement an artificial perceptron with python
I want to inherit to the back with python dataclass
I tried to build ML Pipeline with Cloud Composer
I want to work with a robot in python.
I want to split a character string with hiragana
I want to AWS Lambda with Python on Mac!
I want to manually create a legend with matplotlib