Develop Function Compute by Installing Third-Party Dependencies

Background Information

First, there’s Function Compute, which is an event-driven service that allows users to write and upload code without having to manage server health or consider some other factors. Function Compute prepares and auto scales to the necessary amount of computing resources to run user code. The user only pays for the resources required to run their code.

Procedure

Use the Fun Install Init

Use the fun install init command to initialize a fun.yml file in the current directory. This step is not necessary if you plan to write fun.yml and run tasks in batches using the "fun install" command, init is a good start.

$ fun install init
? Select runtime (Use arrow keys)
python2.7
python3
nodejs6
nodejs8
java8
php7.2
runtime: python2.7
tasks: []

Install pip package dependency

The following command installs the Python TensorFlow package

$ fun install --runtime python2.7 --package-type pip tensorflow
skip pulling image aliyunfc/runtime-python2.7:build-1.2.0...
Task => [UNNAMED]
=> PYTHONUSERBASE=/code/.fun/python pip install --user tensorflow
  • --package-type specifies the type of dependency to install. pip and apt are the current two optional values.
  • tensorflow is a pip package name.
.fun
└── python
├── bin
│ ├── freeze_graph
│ ├── markdown_py
│ ├── pbr
│ ├── saved_model_cli
│ ├── tensorboard
│ ├── tflite_convert
│ ├── toco
│ └── toco_from_protos
└── lib
└── python2.7
└── site-packages
├── tensorboard
├── tensorboard-1.12.2.dist-info
├── tensorflow
├── tensorflow-1.12.0.dist-info
├── termcolor-1.1.0.dist-info
...
$ fun install env
LD_LIBRARY_PATH=/code/.fun/root/usr/lib/x86_64-linux-gnu:/code:/code/lib:/usr/local/lib
PATH=/code/.fun/root/usr/local/bin:/code/.fun/root/usr/local/sbin:/code/.fun/root/usr/bin:/code/.fun/root/usr/sbin:/code/.fun/root/sbin:/code/.fun/root/bin:/code/.fun/python/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin:/bin
PYTHONUSERBASE=/code/.fun/python

Use — save for Persistence

After the --save parameter is added to the install command, the command is persisted as a task, and saved to the fun.yml file.

$ fun install --runtime python2.7 --package-type pip --save tensorflow
skip pulling image aliyunfc/runtime-python2.7:build-1.2.0...
Task => [UNNAMED]
=> PYTHONUSERBASE=/code/.fun/python pip install --user tensorflow
runtime: python2.7
tasks:
- pip: tensorflow
local: true
$ fun install
skip pulling image aliyunfc/runtime-python2.7:build-1.2.0...
Task => [UNNAMED]
=> PYTHONUSERBASE=/code/.fun/python pip install --user tensorflow

Use -v to Display Detailed Logs

$ fun install -v
skip pulling image aliyunfc/runtime-python3.6:build-1.2.0...
Task => [UNNAMED]
=> apt-get update (if need)
Ign http://mirrors.aliyun.com stretch InRelease
Get:1 http://mirrors.aliyun.com stretch-updates InRelease [91.0 kB]
Get:2 http://mirrors.aliyun.com stretch-backports InRelease [91.8 kB]
Get:3 http://mirrors.aliyun.com stretch/updates InRelease [94.3 kB]
Hit http://mirrors.aliyun.com stretch Release.gpg
Hit http://mirrors.aliyun.com stretch Release
Get:4 http://mirrors.aliyun.com stretch-updates/main Sources [3911 B]
....

Install apt package dependency

For Function Compute, using apt-get to install dependencies is another common installation problem. The fun install command can also be used for installation purposes.

$ fun install --runtime python3 --package-type apt libzbar0
skip pulling image aliyunfc/runtime-python3.6:build-1.2.0...
Task => [UNNAMED]
=> apt-get update (if need)
=> apt-get install -y -d -o=dir::cache=/code/.fun/tmp libzbar0
=> bash -c 'for f in $(ls /code/.fun/tmp/archives/*.deb); do dpkg -x $f /code/.fun/root; done;'
=> bash -c 'rm -rf /code/.fun/tmp/archives'

Use fun.yml

fun.yml is composed of a group of tasks. When you run the fun install command, tasks are executed in sequence to achieve batch installation.

runtime: python3
tasks:
- name: install libzbar0
apt: libzbar0
local: true
- name install Pillow by pip
pip: Pillow
local: true
- name: just test shell task
shell: echo '111' > 1.txt
Task => [UNNAMED]

apt/pip tasks

The tasks of apt and pip type are subtypes of install tasks. The description format is similar to the following:

name: install libzbar0
apt: libzbar0
local: true
fun install --package-type apt libzbar0

shell tasks

shell tasks are designed for source code-based installation scenarios.

name: install from source
shell: ./autogen.sh --disable-report-builder --disable-lpsolve --disable-coinmp

Examples

The following is an example of python3 implementing the deployment of a simple QR code identification program to Function Compute. In this example, the pip pyzbar library is used to identify the QR code. The pyzbar library relies on the libzbar0 library installed by apt-get. The pip Pillow library is required to load images. The fun.yml file is described as follows:

runtime: python3
tasks:
- apt: libzbar0
local: true
- pip: Pillow
local: true
- pip: pyzbar
local: true
$ fun install
skip pulling image aliyunfc/runtime-python3.6:build-1.2.0...
Task => [UNNAMED]
=> apt-get update (if need)
=> apt-get install -y -d -o=dir::cache=/code/.fun/tmp libzbar0
=> bash -c 'for f in $(ls /code/.fun/tmp/archives/*.deb); do dpkg -x $f /code/.fun/root; done;'
=> bash -c 'rm -rf /code/.fun/tmp/archives'
Task => [UNNAMED]
=> PYTHONUSERBASE=/code/.fun/python pip install --user Pillow
Task => [UNNAMED]
=> PYTHONUSERBASE=/code/.fun/python pip install --user pyzbar
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
pyzbar-srv:
Type: 'Aliyun::Serverless::Service'
pyzbar-fun:
Type: 'Aliyun::Serverless::Function'
Properties:
Handler: index.handler
Runtime: python3
Timeout: 60
MemorySize: 128
CodeUri: .
from pyzbar.pyzbar import decode
from pyzbar.pyzbar import ZBarSymbol
from PIL import Image
def handler(event, context):
img = Image.open('./qrcode.png')
return decode(img, symbols=[ZBarSymbol.QRCODE])[0].data
fun local invoke pyzbar-fun
skip pulling image aliyunfc/runtime-python3.6:1.2.0...
Thalassiodracon
RequestId: 964980d1-1f1b-4f91-bfd8-eadd26a307b3 Billed Duration: 630 ms Memory Size: 1998 MB Max Memory Used: 32 MB

Summary

This article introduces a new feature of the Fun tool, fun install, which allows you to easily install apt and pip software packages. For engineering requirements of multiple installations, you can consider persisting the installation steps as the fun.yml file. The fun.yml file provides more features than the command line, and shell tasks can be written to support source code installation. The dependency can be installed to the system directory by setting local: false to deal with the situation of compiling dependency instead of running dependency.

Original Source

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store