All Products
Search
Document Center

Function Compute:Create a custom layer

Last Updated:Mar 24, 2026

Layers provide publishing and deployment capabilities for resources such as public dependency libraries, runtime environments, and function extensions. You can extract the common dependencies of your functions into a layer or use an official public layer from Function Compute to reduce the size of your code package when you deploy or update a function. This topic describes the principles of layers, explains how to use layers with different runtimes, and shows you how to build a ZIP package for a layer and how to create and delete custom layers.

How it works

  • When you build a layer, you must package all content into a ZIP file. The Function Compute runtime extracts the content of the layer to the /opt directory.

  • When you add multiple layers to a function, their contents are merged into the /opt directory. If the same file exists in multiple layers, the version from the layer that was added first takes precedence.

    For example, if you add Layer 1 and then Layer 2 to a function, any duplicate files from Layer 2 are overwritten by those from Layer 1.

  • If your layer's code depends on binary libraries or executables, you must build the layer on a Linux system. We recommend using Debian 9.

  • The Function Compute runtime is based on the x86_64 architecture. If dependencies in a layer are dependent on the instruction set, you must use a machine with the x86_64 architecture or use cross-compilation to ensure that the dependencies are compatible with the Function Compute runtime.

Layer usage for different runtimes

If a runtime supports layers, Function Compute adds specific directories to the dependency search path of the runtime language, as shown in the following table. We recommend that you use a folder structure in your layer ZIP package that matches the specific directories listed in the table below. This allows your function code to access the layer without specifying a path. For more information about how to build a layer ZIP package, see Build a layer's ZIP package. If you want to use a custom directory structure for the layer, you must explicitly add the dependency library search path in your code. For more information, see How to reference dependencies from a layer in a custom runtime.

Supported directories for each runtime

Runtime

Directory path

Python

/opt/python

Node.js

/opt/nodejs/node_modules

Java

/opt/java/lib

PHP

/opt/php

Runtimes other than custom runtimes and custom container runtimes

/opt/bin (PATH)

/opt/lib (LD_LIBRARY_PATH)

custom runtime and custom container runtime

None

Layer .zip file structure

The following examples show the required .zip file structure and the corresponding deployment paths for different runtimes.

File structure after packaging with the requests dependency
my-layer-code.zip
└── python
    └── requests

Path after the .zip file is extracted and deployed
/
└── opt
    └── python
        └── requests
File structure after packaging with the uuid dependency
my-layer-code.zip
└── nodejs
    ├── node_modules
    │   └── uuid
    ├── package-lock.json
    └── package.json

Path after the .zip file is extracted and deployed
/
└── opt
    └── nodejs
        ├── node_modules
        │   └── uuid
        ├── package-lock.json
        └── package.json
File structure after packaging with the jackson-core dependency
my-layer-code.zip
└── java
    └── lib
        └── commons-lang3-3.12.0.jar

Path after the .zip file is extracted and deployed
/
└── opt
    └── java
        └── lib
            └── commons-lang3-3.12.0.jar
File structure after packaging with the composer dependency
my-layer-code.zip
└── php
    ├──composer.json
    ├──composer.lock
    └──vendor

Path after the .zip file is extracted and deployed
/
└── opt
    └── php
        ├──composer.json
        ├──composer.lock
        └──vendor

Build a .zip file for a layer

When you create a layer, you must package all its content into a ZIP file. The Function Compute runtime decompresses the content of the layer and deploys it to the /opt directory.

Building a ZIP package for a layer is similar to building a code package. For the function runtime to correctly load libraries from a layer, the directory structure of the library code must follow the standard directory specifications for each language. For more information, see Layer usage for each runtime. If you package function dependency libraries in a layer according to the standard specifications, Function Compute automatically adds the dependency library search paths for each language. In this case, you do not need to specify the full path. If you want to use a custom directory structure for the layer, you must explicitly add the dependency library search path in your code. For more information, see How to reference dependencies in a layer in a custom runtime.

The following steps show how to build a layer .zip file for each runtime:

Note
  • When you build a layer locally, ensure the programming language version you use matches the runtime version you select in Function Compute.

  • The working directory my-layer-code used in the following examples is for demonstration purposes only. You can replace the directory name as needed.

Python runtime

Note

When you build a layer locally, the Python version you use must match the version of the Python runtime you select in Function Compute.

  1. Run the following command to create a working directory.

    mkdir my-layer-code
  2. Go to the directory you just created.

    cd my-layer-code
  3. Run the following command to install dependencies into the my-layer-code/python directory.

    pip install --target ./python ${PackageName}

    ${PackageName} is the name of the dependency package you want to install. For more information about the pip install command, see pip install.

    Example:

    pip install --target ./python numpy

    After the installation is complete, the directory structure is as follows:

    my-layer-code
    └── python
        ├── bin
        ├── numpy
        ├── numpy-1.22.4.dist-info
        └── numpy.libs
  4. In the my-layer-code directory, run the following command to package the dependencies.

    zip -r my-layer-code.zip python

Node.js runtime

Note

When you build a layer locally, the Node.js version you use must match the version of the Node.js runtime you select in Function Compute.

  1. Run the following command to create a working directory.

    mkdir my-layer-code
  2. Go to the directory you just created.

    cd my-layer-code
  3. Run the following command to install dependencies into the my-layer-code/nodejs directory.

    npm install --prefix ./nodejs --save ${PackageName}

    ${PackageName} is the name of the dependency package you want to install. For more information about the npm install command, see npm-install.

    Example:

    npm install --prefix ./nodejs --save uuid

    After the installation is complete, the directory structure is as follows:

    my-layer-code
    └── nodejs
        ├── node_modules
        │ └── uuid
        ├── package-lock.json
        └── package.json
  4. In the my-layer-code directory, run the following command to package the dependencies.

    zip -r my-layer-code.zip nodejs

Java runtime

  1. Run the following command to create a working directory.

    mkdir my-layer-code/java
  2. Go to the directory you just created.

    cd my-layer-code/java
  3. Install dependencies using Maven.

    1. In the my-layer-code/java directory, create a pom.xml file.

      Example:

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <groupId>maven.util</groupId>
          <artifactId>install-layer</artifactId>
          <version>1.0</version>
          <!-- The Maven dependency to download. -->
          <dependencies>
              <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
              <dependency>
                  <groupId>org.apache.commons</groupId>
                  <artifactId>commons-lang3</artifactId>
                  <version>3.12.0</version>
              </dependency>
          </dependencies>
          <build>
              <plugins>
                  <!-- The Maven download plug-in. -->
                  <plugin>
                      <artifactId>maven-dependency-plugin</artifactId>
                      <configuration>
                          <!-- Specifies whether to exclude transitive dependencies. Default is false (do not exclude). -->
                          <excludeTransitive>false</excludeTransitive>
                          <!-- Specifies whether to remove version information from the dependency JAR file suffix. Default is false (do not remove). -->
                          <stripVersion>false</stripVersion>
                          <!-- The output file path. -->
                          <outputDirectory>./lib</outputDirectory>
                      </configuration>
                  </plugin>
              </plugins>
          </build>
      </project>

      The sample code shows the following:

      • The dependency package to be installed is org.apache.commons.lang3.

      • The maven-dependency-plugin is used to copy the required dependency package to the /java/lib directory.

    2. In the my-layer-code/java directory, run the following command to install the dependencies.

      mvn dependency:copy-dependencies

    After the installation is complete, the directory structure is as follows:

    my-layer-code
    └── java
        └── lib
            └── commons-lang3-3.12.0.jar
  4. In the my-layer-code directory, run the following command to package the dependencies.

    zip -r my-layer-code.zip java

PHP runtime

  1. Run the following command to create a working directory.

    mkdir -p my-layer-code/php
  2. Go to the directory you just created.

    cd my-layer-code/php
  3. Install dependencies using Composer.

    1. In the my-layer-code/php directory, create a composer.json file.

      Example:

      {
        "require": {
          "aliyunfc/fc-php-sdk": "~1.2",
          "alibabacloud/fnf": "^1.7"
        }
      }
    2. Run the following command to install the dependencies.

      composer install

    After the installation is complete, the directory structure is as follows:

    my-layer-code
     └─php
       ├──composer.json
       ├──composer.lock
       └──vendor
  4. In the my-layer-code directory, run the following command to package the dependencies.

    zip -r my-layer-code.zip php

Create a custom layer

Console

Prerequisite

You have created a function. For more information, see Create a function.

Procedure

  1. Log on to the Function Compute console. In the left-side navigation pane, choose Advanced Features > Layers.

  2. In the top navigation bar, select a region. On the Layers page, click Create Layer.

  3. On the Create Layer page, configure the parameters, and then click Create.

    Parameter

    Description

    Name

    Enter a name for the layer.

    Description

    Enter a description for the layer.

    Compatible Runtime

    Select the runtimes that are compatible with this layer.

    Layer Upload Method

    Select a method to upload the layer's dependencies:

    • Upload Layer in ZIP Package

      Upload the layer's .zip file. The uploaded .zip file cannot exceed 500 MB.

    • Upload Layer in Folder

      Select a folder that contains the layer's contents. Function Compute automatically compresses the folder into a .zip file, which cannot exceed 500 MB.

    • Upload Layer Using OSS

      Specify the Bucket Name and Object Name of the layer's .zip file in Object Storage Service (OSS). The selected OSS object cannot exceed 500 MB.

    • Build Dependency Layer Online

      Select this option to build a layer directly from a dependency file. This method is supported for Python (requirements.txt) and Node.js (package.json) applications, and for installing lightweight system libraries. Paste the contents of your dependency file into the editor, and the system automatically installs the dependencies to create the layer.

    Build Environment

    If you select Build Dependency Layer Online, you must select the runtime to use for building the dependency layer.

    Note

    Currently, only the Python and Node.js runtimes support building dependency layers online.

    apt command

    If you select Build Dependency Layer Online and need to install software packages into the layer, you can enter the names of the dependencies in the input box after apt install.

    After the layer is created, a version is automatically generated. The version number starts from 1 and increments sequentially.

  4. Create a new version.

    Note

    You cannot modify a created layer or the versions of a created layer. If you want to update the configurations of a layer, you can create a new layer or a new version. If a referenced layer version is deleted, the reference must be deleted before you update the layer configurations.

    1. On the Layers page, click the name of the target layer.

    2. In the Version Management section, click Create Version.

    3. On the Create Version page, select a runtime, upload the new layer code, and then click Create.

Serverless Devs

Prerequisites

Procedure

  1. Run the following command to create a layer.

    s cli fc layer publish --code ./my-layer-code --compatible-runtime java8,Java11,custom  --region cn-hangzhou --layer-name my-layer

    Parameters:

    • --code: The path to the code package.

    • --compatible-runtime: The compatible runtimes for the layer.

    • --layer-name: The name of the layer.

    On success, the command returns the layer's Alibaba Cloud Resource Name (ARN). The ARN contains the account ID, layer name, and version number, separated by hash symbols (#), as shown in the figure. You can also log on to the Function Compute console to view information about the created layer.dg-createlayer-success

  2. To create a new version for an existing layer, run the command again with the same layer name.

    s cli fc layer publish --code ./my-layer-code --compatible-runtime java8,java11,custom  --region cn-hangzhou --layer-name my-layer
    Note

    You cannot modify a created layer or the versions of a created layer. If you want to update the configurations of a layer, you can create a new layer or a new version. If a referenced layer version is deleted, the reference must be deleted before you update the layer configurations.

Delete a layer or a layer version

You can delete layers or layer versions that you no longer need. After a layer version is deleted, you can no longer view it or reference it in new function configurations. However, functions that already reference the deleted layer version are not affected.

  1. Log on to the Function Compute console. In the left-side navigation pane, choose Advanced Features > Layers.

  2. In the top navigation bar, select a region.

  3. On the Layers page, delete a layer or a layer version as needed.

    • Delete a layer

      Find the target layer, click Delete in the Actions column, select the confirmation checkbox in the dialog box, and then click Delete.

    • Delete a layer version

      On the layer details page, go to the Version Management section. Find the target version, click Delete in the Actions column, and then click Delete in the Confirm dialog box.

References

  • You can also manage and configure layers by setting the layers parameter when you create or update a function using an API or SDK. For more information, see Create a function and Update a function.

  • If the dependencies for the layer you want to install contain dynamic-link libraries, or if your local environment is incompatible with the Function Compute runtime environment, you cannot build the layer from the console or locally. You can only build the layer based on a Dockerfile. For more information, see How to build a layer based on a Dockerfile.

  • After you create a custom layer, you can bind it to a function by using the Function Compute console or Serverless Devs so that the function can access the resources in the layer. For more information, see Configure a custom layer.