Layers allow you to publish and deploy custom resources such as common dependencies, runtime environments, and function extensions. You can extract the public libraries on which functions depend into layers or use the common layers in Function Compute to reduce the sizes of code packages when you deploy and update the functions. This topic describes layers in different runtimes, how layers work, how to build a ZIP package for a layer, and how to create and delete a custom layer.

How layers work

  • When you build a layer, you need to package the content into a ZIP file. The Function Compute runtimes decompress and deploy the content of the layer in the /opt directory.
  • When multiple layers are configured for a function, the content of these layers is merged and stored in the /opt directory in reverse order.
  • If two layers have a file with the same name, the file in the layer configured first overwrites the file in the layer configured later.
  • If the code in the layer depends on binary libraries or executable files, you must use a Linux system to compile and build the layer. Debian 9 is recommended.
  • If the dependency libraries in the layer depend on instruction sets, you must use a machine that runs on the AMD64 architecture, or perform cross-compilation to ensure the compatibility between the dependency libraries and the runtimes of Function Compute.

Usage notes for layers in different runtimes

For a runtime that supports layers, Function Compute adds a specific directory to the search path of the dependency package of the runtime language. If the same folder structure is defined in the layer ZIP package, the function code can access the layer without specifying the path.

Directories that can be added in each runtime

RuntimeDirectory
Python/opt/python
Node.js/opt/nodejs/node_modules
Java/opt/java/lib
PHP/opt/php
Runtimes other than custom runtimes and custom containers/opt/bin (PATH)
/opt/lib (LD_LIBRARY_PATH)

File structures of the ZIP packages in each runtime

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

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

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

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

Path after the ZIP package is decompressed and deployed
/
└── opt
    └── php
        ├──composer.json
        ├──composer.lock
        └──vendor

Build a ZIP package for a layer

When you create a layer, you need to package the content into a ZIP file. Function Compute runtimes decompress and deploy the content of the layer in the /opt directory.

The method to build the ZIP package of a layer is similar to the method to build a code package. The code directory structure of the libraries must comply with the standard directory specifications for different languages. This ensures that the libraries can be loaded when Function Compute runs. For more information, see Usage notes for layers in different runtimes. For the function dependency libraries that are deployed in a layer, Function Compute runtimes automatically add the search paths of the dependency libraries of each language if these libraries are packaged based on the specifications. You do not need to specify the full paths. If you want to customize the directory structure of the layer, you must explicitly add the search paths of the dependency libraries to the code. For more information, see How do I reference dependencies in a layer in a custom runtime?.

This section describes how to build the ZIP package of a layer in each runtime.

Note The working directory my-layer-code created in this section is only an example. Replace it with the actual directory.

Python Runtime

  1. Run the following command to create a working directory:
    mkdir my-layer-code
  2. Go to the created working directory.
    cd my-layer-code
  3. Run the following command to install the dependency library in my-layer-code/python:
    pip install --target ./python ${PackageName}

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

    Sample code:
    pip install --target ./python numpy
    After the dependency is installed, check whether your directory structure complies with the following structure:
    my-layer-code
    └── python
        ├── bin
        ├── numpy
        ├── numpy-1.22.4.dist-info
        └── numpy.libs
  4. Run the following command in the my-layer-code directory to package the dependencies:
    zip -r my-layer-code.zip python

Node.js Runtime

  1. Run the following command to create a working directory:
    mkdir my-layer-code
  2. Go to the created working directory.
    cd my-layer-code
  3. Run the following command to install the dependency library in my-layer-code/nodejs:
    npm install --prefix ./nodejs --save ${PackageName}

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

    Sample code:
    npm install --prefix ./nodejs --save uuid
    After the dependency is installed, check whether your directory structure complies with the following structure:
    my-layer-code
    └── nodejs
        ├── node_modules
        │   └── uuid
        ├── package-lock.json
        └── package.json
  4. Run the following command in my-layer-code 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 created working directory.
    cd my-layer-code/java
  3. Install dependencies by using Maven.
    1. Create the pom.xml file in the my-layer-code/java directory.
      Sample URL:
      <?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 dependencies 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>
                  <!-- Maven-related download plug-ins -->
                  <plugin>
                      <artifactId>maven-dependency-plugin</artifactId>
                      <configuration>
                          <!-- Specify whether to exclude indirect dependencies. Default value: false. -->
                          <excludeTransitive>false</excludeTransitive>
                          <!-- Specify whether to remove the version information in the suffix of the dependency JAR package. Default value: false. -->
                          <stripVersion>false</stripVersion>
                          <!-- The path of the output file-->
                          <outputDirectory>./lib</outputDirectory>
                      </configuration>
                  </plugin>
              </plugins>
          </build>
      </project>
      Description:
      • The dependency package to install is org.apache.commons.lang3.
      • Use maven-dependency-plugin to copy the dependency package to the /java/lib directory.
    2. Run the following command in the my-layer-code/java directory to install dependencies:
      mvn dependency:copy-dependencies
    After the dependency is installed, check whether your directory structure complies with the following structure:
    my-layer-code
    └── java
        └── lib
            └── commons-lang3-3.12.0.jar
  4. Run the following command in the my-layer-code directory 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 created working directory.
    cd my-layer-code/php
  3. Install dependencies by using Composer.
    1. Create the composer.json file in the my-layer-code/php directory.
      The following code provides an 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 dependency is installed, check whether your directory structure complies with the following structure:
    layer-code
     └─php
       ├──composer.json
       ├──composer.lock
       └──vendor
  4. Run the following command in the my-layer-code directory to package the dependencies:
    zip -r my-layer-code.zip php

Create a custom layer in the Function Compute console

Before you start

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 click Create.
    ParameterDescription
    NameEnter a custom name for the layer that you want to create.
    DescriptionEnter the description of the layer. Layer descriptions can be used to distinguish layers.
    Compatible RuntimeSelect compatible runtime environments.
    Layer Upload MethodSelect a method to upload the layer dependencies. You can use one of the following methods to upload the layer dependencies:
    • Upload Layer in ZIP Package: Select the ZIP file of the layer.
    • Upload Layer in Folder: Select the folder in which the ZIP file of the layer is located.
    • Upload Layer Using OSS: Select the ZIP file of the layer by specifying the Bucket Name and Object Name parameters.
    • Build Dependency Layer Online: Select an environment from the Build Environment drop-down list and upload the dependency file of the layer package.json or requirements.txt.
      Note Only Python and Node.js runtimes support building dependency layers online. The time limit for uploading dependency package is 15 minutes and the size limit is 500 MB.
    After the layer is created, the system generates the layer version. The version number starts from 1.
  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, find the desired layer and click the name of the layer or click Versions in the Actions column.
    2. In the Versions section of the page that appears, click Create Version.
    3. On the page for creating a layer version, select runtime environments, upload the new layer code, and then click Create.

Create a custom layer by using 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
    Description:
    • --code: specifies the path of the code package.
    • --compatible-runtime: specifies the compatible runtime environments of the layer.
    • --layer-name: specifies the name of the layer.
    After the layer is created, a message is displayed and the ARN of the layer is returned. The ARN is divided into three parts by #. The first part is the identifier of the account ID, the second part is the name of the layer, and the third part is the version of the layer. The following figure shows an example of an ARN. You can log on to the Function Compute console to view the information about the created layer. dg-createlayer-success
  2. Repeat the following command to create a new version for the created layer:
    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 layers and layer versions

You can delete a layer or its version that is no longer used based on your business requirements. The deleted layers can no longer be viewed or referenced by function configurations. However, the execution of functions that are configured to reference the layer is not affected.

  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, choose Advanced Features > Layers.
  3. In the top navigation bar, select the region where the service resides.
  4. On the Layers page, delete a layer or a layer version based on your business requirements.
    • Delete a layer

      Find the layer that you want to delete and click Delete in the Actions column. In the dialog box that appears, select I want to delete all of the N versions at the layer, and click Delete.

    • Delete a layer version

      Click the name of the layer whose version you want to delete. In the Versions section of the layer details page, find the layer version that you want to delete and click Delete in the Actions column. In the Confirm message box that appears, click Delete.

Additional information

You can also use API operations or SDKs to manage and configure layers. You can configure layers by calling the following API operations:

You can use OpenAPI Explorer to call API operations and use SDKs.