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.

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
Notice The package manager files must be stored in the code directory specified by the codeUri parameter in the s.yaml file.

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

  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 dependencies to the .s directory based on the content of the requirements.txt 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 deliverable content in the .s directory and configures the environment variable of the dependent library, as marked by ③ in the following figure. 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, 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 dependencies to the .s directory based on the content of the package.json 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 deliverable content in the .s directory and configures the environment variable of the dependent library, as marked by ③ in the following figure. 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 dependencies to the .s directory based on the content of the pom.xml 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 deliverable content in the .s directory and configures the environment variable of the dependent library. This allows the function to directly introduce the corresponding code dependent library. The command output is 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 dependencies to the .s directory based on the content of the composer.json 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 deliverable content in the .s directory 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 dependencies to the .s directory based on the content of the Dockerfile file.

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

Additional information

When 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 needs to be installed and stored in the apt-get.list file.
Notice The apt-get.list file must be manually created to store extended libraries. In addition, the apt-get.list file must be stored in the code directory specified by the codeUri parameter in the s.yaml file.
  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 dependencies to the .s directory based on the content of the package.json and apt-get.list files.
    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
    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 ./.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 ./.s/build/artifacts/puppeteer-nodejs12/html2png/node_modules  /mnt/auto/node_modules
  4. Run the following command to execute an on-premises 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 a function based on the deliverable content in the .s directory 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