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 to reduce the sizes of code packages when you deploy and update the code. This topic describes the principles of layers, the descriptions of layers in different runtimes, and how to build the ZIP package of a layer.

How it works

  • 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 layers contain files with the same name, the files in the first configured layer overwrites the files with the same names in the later configured layer.
  • 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 libraries in the layer depend on instruction sets, use a machine that runs on the AMD64 architecture, or perform cross-compilation to ensure the compatibility between the libraries and the Function Compute runtimes.

Layer file directory and file structure of 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

Runtime Directory
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

			

The file structure after the requests dependencies are used my-layer-code.zip └── python └── requests

Path after the ZIP package is decompressed and deployed / └── opt └── python └── requests

			

The file structure after the uuid dependencies are used 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

			

The file structure after the jackson-core dependencies are used 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

			

The file structure after the composer dependencies are used 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 the ZIP package of the layer

When you create 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.

The method to build the ZIP package of a layer is similar to the method to build code packages. 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 Layer file directory and file structure of 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 you 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 need to 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 to 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
    The following sample code describes the file directory structure after the installation:
    my-layer-code
    └── python
        ├── bin
        ├── numpy
        ├── numpy-1.22.4.dist-info
        └── numpy.libs
  4. Run the following command in my-layer-code 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 to 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
    The following sample code describes the file directory structure after the installation:
    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.
      Example for pom.xml:
      <?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
    The following sample code describes the directory structure after the installation:
    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/java directory.
      Sample code:
      {
        "require": {
          "aliyunfc/fc-php-sdk": "~1.2",
          "alibabacloud/fnf": "^1.7"
        }
      }
    2. Run the following command to install the dependencies:
      composer install
    The following sample code describes the directory structure after the installation:
    layer-code
     └─php
       ├──composer.json
       ├──composer.lock
       └──vendor
  4. Run the following command in my-layer-code to package the dependencies:
    zip -r my-layer-code.zip php