×
Community Blog How to Develop Function Compute - Installing Dependencies in Interactive Mode

How to Develop Function Compute - Installing Dependencies in Interactive Mode

This tutorial is a part of the 'How to Develop Function Compute' series. It demonstrates how to install dependencies in an interactive mode.

By Du Wan (Yixian)

Preface

First, let's take a quick look at several key concepts mentioned in this article:

  • Function Compute: It's an event-driven service that allows users to focus on writing and uploading code without the need to manage infrastructure such as servers. Function Compute provides compute resources, allowing users to run code more elastically by just paying for the resources consumed while running the code. For more information about Function Compute, see here.
  • Fun: Also known as Funcraft, is a developer tool for serverless applications that helps in managing resources such as Function Compute, API Gateway, and Log Service. Use Fun to develop, build, and deploy resources by defining specified resources in the 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.

Dependent Tools

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

Background

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.

System Dependencies are Generally Non-portable

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.

Language Dependencies are Non-portable

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.

Limits of DSL Scripts

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.

1

Command Line Parameters

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

Quick start

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: .

Enabling a Sandbox in Interactive Mode

$ 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.

  • -f/-- function: Specifies the function to start a sandbox. In this example, the runtime configured in the function is Python 3. Therefore, the sandbox in the Python 3 environment is enabled. The CodeUri directory corresponding to the pyzbar-fun function is mounted to the /code directory within the sandbox environment, which are confirmed in the file list returned after you run the ls command in the sandbox environment.
  • -i/-- interactive: Enables the interactive mode. The usage of the non-interactive mode will be introduced later.

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.

Use fun-install to Install apt and pip dependencies

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]

Use a Sandbox in Non-interactive Mode

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.

Summary

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.

0 0 0
Share on

Alibaba Cloud Serverless

30 posts | 4 followers

You may also like

Comments

Alibaba Cloud Serverless

30 posts | 4 followers

Related Products

  • Function Compute

    Alibaba 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 More
  • Serverless Workflow

    Visualization, O&M-free orchestration, and Coordination of Stateful Application Scenarios

    Learn More
  • DevOps Solution

    Accelerate software development and delivery by integrating DevOps with the cloud

    Learn More
  • Web App Service

    Web App Service allows you to deploy, scale, adjust, and monitor applications in an easy, efficient, secure, and flexible manner.

    Learn More