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.)
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?
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)
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.
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
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.
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