A Funfile contains the commands that specify the steps to produce Function Compute deliverables such as the content that must be published after you develop, package, and compile a project. Funcraft runs the commands in the Funfile to produce the deliverables. This topic describes how to use a Funfile to install third-party dependencies.

Syntax supported by Funfiles

Funfiles support specific commands in Dockerfiles, such as the COPY, RUN, ENV, USER, and WORKDIR commands. To use a Funfile to install third-party dependencies, add fun-install before the apt-get and pip commands in the Funfile.

You can use one of the following methods to run the fun install command to verify the syntax supported by a Funfile:
  • Copy the following content to the Funfile. Then, Function Compute copies the package.json file in the function directory to perform subsequent operations.
    RUNTIME nodejs8
    COPY ./package.json .
    RUN npm install
  • Use the following complex commands to install the rlang package in Function Compute:
    RUNTIME python3RUN 
    fun-install apt-get install libblas3
    RUN fun-install pip install pandas
    RUN apt-get build-dep -y r-base; \  
      curl -L https://fc-demo-public.oss-cn-hangzhou.aliyuncs.com/fun/examples/R-3.5.2.tar.gz | tar -zxf -; \  
      cd R-3.5.2 ;  \  
      ./configure --prefix=/code/.fun/R/ --enable-R-shlib --with-blas --with-lapack ;  \  
      make ;  \  
      make install;
    RUN PATH=/code/.fun/R/bin:$PATH LD_LIBRARY_PATH=/code/.fun/root/usr/lib/libblas:$LD_LIBRARY_PATH fun-install pip install rpy2
    RUN rm -rf R-3.5.2;  \  
      rm -rf .fun/root/usr/share;  \  
      rm -rf .fun/R/share;  \  
      rm -rf .fun/R/lib/R/doc; \  
      rm -rf .fun/python/lib/python3.6/site-packages/pandas/tests; \  
      rm -rf .fun/R/lib/R/library/*/{help,demo,html,doc}; \  
      find .fun -type f -name "*.so" -exec strip --strip-all {} \;

Advantages of Funfiles

  • Standardized: The commands that are specified in a Funfile to install dependencies help standardize the development process.
  • Convenient: You can submit a Funfile instead of specific dependencies to the version control system. This helps maintain function code.
  • Native: Funfile commands are similar to native Dockerfile commands. You can test installation commands in an interactive environment by using fun install sbox and then write the final installation commands in a Funfile as in a Dockerfile.
  • Efficient: Funfiles support the cache feature of Dockerfiles. If the changes in a project do not affect Funfiles, Function Compute can skip related commands in the Funfiles based on the cached data. This accelerates the execution of Funfiles and improves development efficiency.

Procedure

  1. Create a Funfile.
    1. In the project directory, run the following command to initialize the installation:
      fun install init                      
    2. Select a runtime environment for the function.
      ? Select a runtime (Use arrow keys)
      > nodejs6
        nodejs8
        nodejs10
        nodejs12
        python2.7
        python3
        java8
      (Move up and down to reveal more choices)
      After you select a runtime environment, a file named Funfile is generated in the current directory. If you select python3 as the runtime environment, the Funfile file contains the following content:
      RUNTIME python3
      Note A Funfile is a text file, and you do not need to use Funcraft to initialize a Funfile. You can directly create a Funfile and specify the runtime environment in the Funfile based on your requirements.
  2. Use the Funfile to install dependencies.
    1. Configure the Funfile based on your requirements. For example, if you want to install a Flask library by using pip, configure the following content in the Funfile:
      RUNTIME python3
      RUN fun-install pip install flask                    
      Note If you want to use apt-get to install a Flask library, replace pip in the preceding content with apt-get in the Funfile.
    2. Run the following command to install dependencies:
      fun install
      Expected output:
      using template: template.yml
      start installing function dependencies without docker
      
      building pyzbar-srv/pyzbar-fun
      Funfile exist, Fun will use container to build forcely
      Step 1/2 : FROM aliyunfc/runtime-python3.6:build-1.6.1
       ---> 2110e9fa9393
      Step 2/2 : RUN fun-install pip install flask
       ---> Using cache
       ---> 992b0d65136d
      sha256:992b0d65136dfa63b9566b8280881bb4cf22c4f1b68155713545e4b61ada8dd9
      Successfully built 992b0d65136d
      Successfully tagged fun-cache-e95a9bfa-a6fa-4d70-b5b4-0084613e2b8d:latest
      copying function artifact to /Users/tan/code/fun/examples/install/pyzbar_example
      
      could not find any manifest file for pyzbar-srv/pyzbar-fun, [install] stage for manifest will be skipped
      
      Tips for next step
      ======================
      * Invoke Event Function: fun local invoke
      * Invoke Http Function: fun local start
      * Build Http Function: fun build
      * Deploy Resources: fun deploy