All Products
Search
Document Center

Function Compute:Install a third-party dependency for a function

Last Updated:Aug 14, 2024

Common dependencies are built in runtimes of Function Compute for you to reference in functions. If these built-in dependencies cannot meet your business requirements, you can install your own third-party dependencies. This topic describes how to install third-party dependencies for a function.

Background

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

Install a dependency by using a layer

The official public layers of Function Compute are pre-installed with common dependency libraries and can be directly used. You can also build a custom layer to install required dependencies. If you want to get more public layers, see awesome-layers.

Use public layers to install dependencies

  • Official public layers

    Log on to the Function Compute console, create a function or find the desired function, choose Configurarions > Layers, and click Modify. In the Layers panel, choose + Add Layer > Add Public Layer. For more information, see Configure a public layer.

  • Non-official public layers

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

image

Install dependencies by building a custom layer

  • Build a layer in 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 does not support dependencies that contain system dynamic-link libraries with the .so extension. Example: Puppeteer dependency library for Node.js. If the dependency contains a dynamic link library (.so), we recommend that you use a Dockerfile to build the 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 file for a layer. To use this method, you need to ensure that the on-premises operating system and processor architecture are completely consistent with the Function Compute runtime. Specifically, use an x86-64 Linux operating system, or make sure that the installed dependency library does not depend on the underlying environment or processor architecture. Otherwise, we recommend that you build layers in the Function Compute console or use a Dockerfile to build layers.

    For example, the numpy library for Python depends on its underlying environment. If your on-premises machine runs a macOS with M1 chips, you cannot install dependencies on the on-premises machine.

  • Use a Dockerfile to build a layer

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

Install dependencies in the Function Compute console

Package the dependencies and upload them to the Function Compute console.

  1. Compress a third-party dependency and its code files into a .zip file.

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

    • The packaging method varies based on the OS of your on-premises machine. You can use a packaging method based on your business requirements.

  2. Log on to the Function Compute console and upload a code package by using one of the following methods to deploy the function: Upload ZIP, Upload Folder or OSS.

Install a dependency by using a web IDE terminal

  1. Log on to the Function Compute console and find the function that you want to manage.

  2. On the function details page, click the Code tab. On the WebIDE page, click Terminal > New Terminal. In the terminal window, run the pip install -t . <PackageName> command to install dependencies.

    image

Use Serverless Devs to install dependencies

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