Function Compute provides built-in common dependencies that you can reference in runtime environments. Function Compute supports third-party dependencies. This topic describes how to install third-party dependencies for a function.
Background informationFor more information about built-in common dependencies of Function Compute, see the following topics:
Install dependencies by using layers
Function Compute released several dependency libraries 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 the function is created, click the Configurations tab. In the Layers section, click Modify. In the layer modification panel, choose . 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 .
Install dependencies by using 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 . 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 the Puppeteer dependency library 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 dependency libraries to be installed do not rely on the underlying environment or processor architecture. Otherwise, we recommend that you build layer in the Function Compute console or use a Dockerfile to build layers.
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.
- Use a Dockerfile to build a layer
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
- 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.
- Log on to the Function Compute console, upload the code package by clicking Upload ZIP, Upload Folder, or OSS, and deploy the function.
Install dependencies by using web IDE terminals in the console
- Log on to the Function Compute console and find the desired function.
- On the function details page, click the Code tab. On the web IDE page, choose . 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.
- The following figure shows the directory structure of web IDE after the installation.
Use Serverless Devs to install dependencies
Create and deploy functions by using Serverless Devs provided by Function Compute. For more information, see Serverless Devs commands.