All Products
Search
Document Center

Function Compute (2.0):Install third-party dependencies for a function

Last Updated:Nov 27, 2023

Function Compute provides built-in common dependencies for you to reference in the runtime environment. Function Compute also supports third-party dependencies. This topic describes how to install third-party dependencies for a function.

Background information

For more information about built-in common dependencies in Function Compute, see the following topics:

Install dependencies by using layers

Function Compute has released several library dependencies that you can directly use. For more information, see Configure common layers for a function. If you want to obtain more common layers, see awesome-layers.

Install dependencies by using an official common layer

  • Official common layer

    After a function is created, click the Configurations tab. In the Layers section, click Modify. In the layer modification panel, choose + Add Layer > Add Official Common Layer. For more information, see Configure common layers for a function.

  • Non-official common layer

    Find the desired layer from awesome-layers and obtain the Alibaba Cloud Resource Name (ARN) of the layer. Click the Configurations tab. In the Layers section, click Modify. In the layer modification panel, choose + Add Layer > Add Layer by ARN.

Install dependencies by using a custom layer

  • Build a layer by using the Function Compute console

    Log on to the Function Compute console. In the left-side navigation pane, choose Advanced Features > Layers. For more information, see Create a custom layer.

    Note
    • This method applies only to Node.js and Python runtime environments.

    • This method does not support dependencies that contain system dynamic-link libraries (.so), such as Puppeteer for Node.js. If the dependencies include dynamic-link libraries (.so), we recommend that you use a Dockerfile to build a layer.

  • Build a layer on an on-premises machine

    You can build a custom layer on an on-premises machine. For more information, see Build a ZIP package for a layer. To use this method, make sure that the operating system and processor architecture of the on-premises machine are the same as those of the Function Compute runtime environment. The on-premises machine must run Linux x86-64, or the library dependencies to be installed do not rely on the underlying environment or processor architecture. Otherwise, we recommend that you build a layer by using the Function Compute console or a Dockerfile.

    For example, the numpy library of Python is dependent on the underlying environment. If your on-premises machine runs a Mac system with M1 chips, you cannot install dependencies on the on-premises machine.

  • Build a layer by using a Dockerfile

    If the dependencies contain the underlying dynamic-link libraries or fail to be installed on an on-premises machine, you can use a Dockerfile to install the dependencies. For more information, see Use a Dockerfile to build a layer.

Use the Function Compute console to install dependencies

Package the dependencies and upload the dependencies to the console

  1. Compress the third-party dependencies and code files into a package.

    Important
    • You must compress all files in the code directory into a package. The file of the handler function must be in the root directory of the package.

    • The packaging method varies based on the operating system. Select a packaging method based on your business requirements.

  2. Log on to the Function Compute console, upload the code package by clicking Upload ZIP, Upload Folder, or OSS, and then deploy the function.

Install dependencies by using Web IDE in the console

  1. Log on to the Function Compute console and find the desired function.

  2. On the function details page, click the Code tab. On the Web IDE interface, choose Terminal > New Terminal. In the terminal that appears, run the command to install dependencies.

    The following sample code shows how to install dependencies in Python. For more information, see Deploy a code package.

    pip install -t . <PackageName>      # PackageName indicates the name of the dependency package. 
  3. View the directory structure of your Web IDE, as shown in the following figure. install-rely

Use Serverless Devs to install dependencies

Create and deploy a function by using Serverless Devs provided by Function Compute. For more information, see Serverless Devs commands.