By Du Wan (Yixian)
First, let's take a quick look at several key concepts mentioned in this article:
template.yml
file. For more information about Fun, see here.Note: The operations in this article are applicable to Fun 3.0.0-beta.1 and other later versions.
This project development is based on MacOS. However, the tools involved are platform-independent and also applicable to the Linux and Windows operating systems. Before proceeding with the example, make sure that the following tools are correctly installed, updated to the latest version, and properly configured.
Fun is based on Docker to simulate the local environment.
MacOS users may use homebrew to install these tools:
brew cask install docker
brew tap vangie/formula
brew install fun
Windows and Linux users must read this article to successfully install these tools.
After the installation, first run the fun config
command to initialize the configuration.
Note: Make sure to use a Fun version 3.0.0-beta.1 or any later version.
$ fun --version
3.0.0-beta.1
Function Compute uses Zip files as agreed deliverables, which usually contain code and dependencies. These dependencies are generally system dependencies that are installed and managed through apt-get and language runtime dependencies that are installed using language-related package managers such as NPM and pip.
The system dependencies are usually related to the environment. For example, running the brew install brotli
command to install brotli on a development machine that uses MacOS and package it to Function Compute. Then, upon calling a function to run the brotli command to decompress a file, the function invocation fails. Sometimes, the function invocation may fail even on a development machine that uses Linux. The reason is that the executable programs and dynamic link libraries installed with the package managers provided by the system are strongly related to the system type and version. The program and files installed on different systems such as Mac, Windows, and Linux are non-portable for each other.
In general, language dependencies are platform-independent. For example, running the npm install jszip
command to install a Node.js dependency. Consequently, the dependency may run on different operating systems and under different Node.js versions. The dependencies on the language platform are usually portable. However, there may be exceptions. For example, npm install node-pty
is an example with native binding. The installation of the node-pty module depends on some C/C ++ code, which are compiled in the installation process. Though C/C ++ code is portable across different platforms, the code becomes non-portable after compilation.
Fun 2.0 supports using the DSL file (fun.yml
) to install dependencies in batches. A command line mode is available for daily development, such as fun install --package-type pip tensorflow
. Fun 3.0 provides a new DSL file, that is, Funfile. Consider Funfile as a syntax subset of Dockerfile. Therefore, developers who are familiar with Docker will get started quickly.
However, there is a pain point for developers no matter whether they use fun.yml
, Funfile, or the command line mode. The current environment is unknown, which may include the following aspects:
1) What software has been installed?
2) What files are in a specific directory?
3) What are the content and attributes of each file?
Developers need a sandbox environment that involves interaction. However, this function is not available in Fun 2.0. Instead, users often run the fcli sbox
command, or directly use the image provided by fc-docker to start a container by using the docker run --rm -it -v $(pwd):/code aliyunfc/runtime-python2.7:build bash
command. However, users must know Docker and Function Compute well before using these complex commands and parameters.
To solve these problems and improve the development experience of users, Fun 3.0 provides the fun install sbox
subcommand.
Refer to the various command line parameters shown in the following snippet.
$ fun install sbox --help
Usage: fun install sbox [-f|--function <[service/]function>] [-r|--runtime <runtime>] [-i|--interactive] [-e|--env key=val ...] [-e|--cmd <cmd>]
Start a local sandbox for installation dependencies or configuration
Options:
-f, --function <[service/]function> Specify which function to execute installation task.
-r, --runtime <runtime> function runtime, avaliable choice is: nodejs6, nodejs8, nodejs10, python2.7, python3, java8, php7.2, custom
-i, --interactive run as interactive mode. Keep STDIN open and allocate a pseudo-TTY when in a interactive shell. (default: false)
-e, --env <env> environment variable, ex. -e PATH=/code/bin (default: [])
-c, --cmd <cmd> command with arguments to execute inside the installation sandbox.
-h, --help output usage information
Take the Pyzbar_example project as an example. The pyzbar_example
project contains the following files:
$ tree .
.
©À©¤©¤ fun.yml
©À©¤©¤ index.py
©À©¤©¤ qrcode.png
©¸©¤©¤ template.yml
0 directories, 4 files
The following snippet displays the content of the template.yml
file.
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: .
$ fun install sbox -f pyzbar-fun -i
using template: template.yml
root@fc-python3:/code# ls
fun.yml index.py qrcode.png template.yml
root@fc-python3:/code# exit
exit
$
In the directory where the template.yml
resides, run the fun install sbox command.
ls
command in the sandbox environment.If the template.yml
file does not exist or the function in the template.yml
file is not configured, specify the -- runtime parameter to start the interactive mode. In this case, the current directory is mounted to the /code directory in the sandbox environment.
$ fun install sbox -r nodejs10 -i
root@fc-nodejs10:/code# ls
fun.yml index.py qrcode.png template.yml
root@fc-nodejs10:/code# exit
exit
$
The above method is applicable to temporarily starting a sandbox for experiments.
Execute the following commands to install apt and pip dependencies.
$ fun install sbox -f pyzbar-fun -i
using template: template.yml
root@fc-python3:/code# fun-install apt-get install libblas3
Task => [UNNAMED]
=> apt-get update (if need)
=> apt-get install -y -d -o=dir::cache=/code/.fun/tmp libblas3
=> bash -c
for f in $(ls /code/.fun/tmp/archives/*.deb); do
dpkg -x $f /code/.fun/root;
mkdir -p /code/.fun/tmp/deb-control/${f%.*};
dpkg -e $f /code/.fun/tmp/deb-control/${f%.*};
if [ -f "/code/.fun/tmp/deb-control/${f%.*}/postinst" ]; then
FUN_INSTALL_LOCAL=true /code/.fun/tmp/deb-control/${f%.*}/postinst configure;
fi;
done;
=> bash -c 'rm -rf /code/.fun/tmp/archives'
root@fc-python3:/code# fun-install --help
Usage: fun local [options] [command]
build function codes or install related depedencies for Function Compute
Options:
-h, --help output usage information
Commands:
apt-get install apt depencies
pip install pip depencies
build build function codes for Function Compute
help [cmd] display help for [cmd]
Run the following command to print the pre-installed deb package in the Sandbox of the pyzbar-fun function.
$ fun install sbox -f pyzbar-fun -c 'dpkg -l'
using template: template.yml
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-==================================-================================-============-===============================================================================
ii acl 2.2.52-2 amd64 Access control list utilities
ii adduser 3.113+nmu3 all add and remove users and groups
ii apt 1.0.9.8.4 amd64 commandline package manager
ii apt-utils 1.0.9.8.5 amd64 package management related utility programs
ii autoconf 2.69-8 all automatic configure script builder
ii automake 1:1.14.1-4+deb8u1 all Tool for generating GNU Standards-compliant Makefiles
...(´Ë´¦Ê¡ÂÔÁËÐí¶àÐÐ)
Alternatively, run the following command to send the outputs of an external command to the internal system through the pipeline.
$ echo hello | fun install sbox -r nodejs10 -i -c 'cat -'
hello
Note: Specify the -i
parameter, which indicates that standard input is accepted.
The fun install sbox command is the alternative for the fcli sbox command. In addition to the support for the interactive mode, the fun install sbox command also starts a Sandbox through a specified function in the specified runtime. Secondly, the fun install sbox command may also be used in the non-interactive mode, such as inline commands and pipelines, which facilitates script writing.
How to Develop Function Compute - Using LibreOffice to Convert Word Files to PDF Files
Step-by-step Instructions on Porting Ghostscript to Function Compute
99 posts | 7 followers
FollowAlibaba Cloud Serverless - August 21, 2019
vangie - January 23, 2019
Alibaba Clouder - December 17, 2018
Alibaba Cloud Serverless - August 21, 2019
Alex - September 18, 2018
Apache Flink Community China - December 25, 2019
99 posts | 7 followers
FollowAlibaba Cloud Function Compute is a fully-managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure such as servers.
Learn MoreVisualization, O&M-free orchestration, and Coordination of Stateful Application Scenarios
Learn MoreServerless Application Engine (SAE) is the world's first application-oriented serverless PaaS, providing a cost-effective and highly efficient one-stop application hosting solution.
Learn MoreAlibaba Cloud (in partnership with Whale Cloud) helps telcos build an all-in-one telecommunication and digital lifestyle platform based on DingTalk.
Learn MoreMore Posts by Alibaba Cloud Serverless