Function Compute does not provide all dependent libraries in each runtime environment. As a result, you may fail to invoke a function after you deploy a project by using the FC component. For example, a Python function that you define needs to access the MySQL dependent library. If the MySQL dependent library is not provided in the Python runtime environment, the function fails to be executed when you invoke the function. To prevent such a failure, you must install the MySQL dependent library before you deploy a project. This topic describes how to use the FC component to install dependencies in various runtime environments.

Prerequisites

Before you begin, make sure that the following operations are complete:

Procedure

Except for .NET Core, Java, Node.js, Python, PHP, and container images all support rapid building of resources. The FC component supports the following manifest files of package managers for different programming languages:
  • requirements.txt files of pip for Python
  • package.json files of npm for Node.js
  • pom.xml files of Apache Maven for Java
  • composer.json files of Composer for PHP
  • Dockerfiles of container images

The following example shows how to install dependencies in Python 3:

  1. Develop and edit source code.
  2. Run the following command in the project directory to install dependencies:
    s build --use-docker

    After you run the preceding command, Serverless Devs downloads the corresponding dependencies to your local machine based on the content of the requirements.txt file, and packages the downloaded dependencies and source code into a deliverable ZIP file.

  3. Run the following command to deploy the project:
    s deploy

    After you run the preceding command, Serverless Devs creates a function based on the content in the deliverable ZIP file and configures the environment variable of the dependent library. This allows the function to directly introduce the corresponding code dependent library. In the command output, you can view the added environment variable, as shown in the following figure.

The following example shows how to install dependencies in Node.js 10:

  1. Develop and edit source code.
  2. Run the following command in the project directory to install dependencies:
    s build --use-docker

    After you run the preceding command, Serverless Devs downloads the corresponding dependencies to your local machine based on the content of the package.json file, and packages the downloaded dependencies and source code into a deliverable ZIP file.

  3. Run the following command to deploy the project:
    s deploy

    After you run the preceding command, Serverless Devs creates a function based on the content in the deliverable ZIP file and configures the environment variable of the dependent library. This allows the function to directly introduce the corresponding code dependent library. In the command output, you can view the added environment variable, as shown in the following figure.

The following example shows how to install dependencies in Java 8:

  1. Develop and edit source code, as marked by ① in the following figure.
  2. Run the following command in the project directory to compile the Java project and install dependencies:
    s build --use-docker

    After you run the preceding command, Serverless Devs downloads the corresponding dependencies to your local machine based on the content of the pom.xml file, and packages the downloaded dependencies and source code into a deliverable ZIP file, as marked by ② in the following figure.

  3. Run the following command to deploy the project:
    s deploy

    After you run the preceding command, Serverless Devs creates a function based on the content in the deliverable ZIP file and configures the environment variable of the dependent library. This allows the function to directly introduce the corresponding code dependent library. In the command output, you can view the added environment variable, as shown in the following figure.

The following example shows how to install dependencies in PHP 7.2:

  1. Develop and edit source code, as marked by ① in the following figure.
  2. Run the following command in the project directory to install dependencies:
    s build --use-docker

    After you run the preceding command, Serverless Devs downloads the corresponding dependencies to your local machine based on the content of the composer.json file, and packages the downloaded dependencies and source code into a deliverable ZIP file, as marked by ② in the following figure.

  3. Run the following command to deploy the project:
    s deploy

    After you run the preceding command, Serverless Devs creates a function based on the content in the deliverable ZIP file and configures the environment variable of the dependent library. This allows the function to directly introduce the corresponding code dependent library. In the command output, you can view the added environment variable, as shown in the following figure.

The following example shows how to install dependencies in a custom container:

  1. Develop and edit source code, and replace the image in the s.yml file with your image in Container Registry.
  2. Run the following command in the project directory to install dependencies:
    s build --use-docker --dockerfile ./Dockerfile

    After you run the preceding command, Serverless Devs downloads the corresponding dependencies to your local machine based on the content of the Dockerfile, and packages the downloaded dependencies and source code into a deliverable ZIP file.

  3. Run the following command to deploy the project:
    s deploy -y

Additional information

In scenarios in which code packages are used, you may need to perform more operations in addition to installing the libraries for various programming languages. For example, the runtime environment of a Puppeteer application deployed in Function Compute is Node.js. In addition to the Puppeteer library, an underlying .so library, such as apt-get.list, needs to be installed.

  1. Create a Puppeteer project and develop and edit source code, as marked by ① in the following figure.
    Run the following command in the specified directory to initialize the project:
    s init devsapp/start-puppeteer -d start-puppeteer
    Note -d is used to specify the name of the project. In this example, the project name is start-puppeteer.
  2. Go to the project directory.
    The template of the initialized project provides two deployment modes: deployment by using Node.js and an Apsara File Storage NAS file system and deployment by using containers. This section describes how to deploy the project by using Node.js and a NAS file system.
    1. Run the following command in the project directory to go to the project directory:
      cd start-puppeteer
    2. Run the following command in the start-puppeteer project to go to the nodejs12 project:
      cd nodejs12
  3. Install dependencies. After you run the command that is used to install dependencies, Serverless Devs downloads the corresponding dependencies to your local machine based on the content of the package.json and apt-get.list files, and packages the downloaded dependencies and source code into a deliverable ZIP file, as marked by ② in the following figure.
    Note The deliverable ZIP file into which the downloaded dependencies and source code are packaged in this step does not contain .fcignore files. The deliverable ZIP file is small in size because the downloaded dependencies are directly uploaded to the NAS file system.
    1. Run the following command to install dependencies:
      s build --use-docker
    2. Optional:Run the following command to initialize the NAS file system.
      If the nasConfig parameter in your s.yml file is set to Auto, you can run this command to create a NAS file system. If you have specified a value other than Auto for the nasConfig parameter, you do not need to run this command.
      s nas init
    3. Run the following command to upload the downloaded apt-get dependent library to the NAS file system. The apt-get library is automatically added to the LD_LIBRARY_PATH environment variable, as marked by ③ in the following figure.
      s nas upload -r -n ./.s/build/artifacts/puppeteer-nodejs12/html2png/.s/root /mnt/auto/root
    4. Run the following command to upload the downloaded npm dependent library to the NAS file system, as marked by ④ in the following figure.
      s nas upload -r -n ./.s/build/artifacts/puppeteer-nodejs12/html2png/node_modules  /mnt/auto/node_modules
  4. Run the following command to execute a local function:
    s local start
    Command output:
    [2021-08-11T11:03:46.053] [INFO ] [FC-LOCAL-INVOKE] - HttpTrigger httpTrigger of puppeteer-nodejs12/html2png was registered
        url: http://localhost:7928/2016-08-15/proxy/puppeteer-nodejs12/html2png/
        methods: GET,POST,PUT
        authType: anonymous
    
    Tips for next step
    ======================
    * Deploy Resources: s deploy
    fc-puppeteer-nodejs12-html2png:
      status: succeed
    
    function compute app listening on port 7928!
  5. Run the following command to deploy the function:
    s deploy
    After you run the preceding command, Serverless Devs creates the function based on the content in the deliverable ZIP file and configures the environment variable of the dependent library. This allows the function to directly introduce the corresponding code dependent library. In the command output, you can view the added environment variable, as shown in the following figure.serverlessdevs5gaojishezhi