All Products
Search
Document Center

Function Compute (2.0):Create a custom layer

Last Updated:Dec 29, 2023

Layers allow you to publish and deploy custom resources such as common dependencies, runtimes, and function extensions. You can abstract the public libraries that are depended on by functions into layers or use common layers of Function Compute to reduce the sizes of the code packages when you deploy or update functions. This topic describes usage notes of 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.

Usage notes

  • When you build a layer, you need to package the content into a ZIP file. Runtimes of Function Compute decompress and deploy content of layers to the /opt directory.

  • When multiple layers are configured for a function, content of these layers is merged and stored in the /opt directory in reverse order. If multiple layers contain files with the same name, files in upper layers overwrite the files with the same name in lower layers.

  • 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 library in a layer depends on instruction sets, you must use a machine of the AMD 64 architecture or perform cross compilation to ensure that the dependency library is compatible with runtimes in Function Compute.

Usage notes for layers in different runtimes

For runtimes that support layers, Function Compute adds specific directories to the search directories of dependencies for runtimes. The following table describes the directories. 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

Other runtimes (excluding custom runtimes and Custom Container runtimes)

/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 file 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. Runtimes of Function Compute decompress and deploy content of layers to the /opt directory.

The method to build the .zip file of a layer is similar to the method to build a code package. To ensure that function runtimes can load layer libraries as expected, the code directories of the layer libraries must comply with standard packaging rules for each language. For more information, see Usage notes for layers in different runtimes. For dependency libraries of a function on layers, if you package the libraries based on standard packaging rules, Function Compute runtimes automatically append the search paths of function libraries for different languages, and you do not need to specify full paths. If you want to customize the directory structure of the layer, you must explicitly add 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
  • When you build a layer on an on-premises machine, the programming language that you use must be the same with the programming language of the runtime in Function Compute.

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

Python runtimes

Note

When you build a layer on an on-premises machine, the version of Python you use must be the same with the version of the Python runtime in Function Compute.

  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 library that you want to install. For more information about the pip install command, see pip install.

    Sample code:

    pip install --target ./python numpy

    After the dependencies are 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 dependencies:

    zip -r my-layer-code.zip python

Node.js runtimes

Note

When you build a layer on an on-premises machine, the version of Node.js you use must be the same with the version of the Node.js runtime in Function Compute.

  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 library that you want to install. For more information about the npm install command, see npm-install.

    Sample code:

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

    After the dependencies are 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 dependency:

    zip -r my-layer-code.zip nodejs

Java runtimes

  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:

      <?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 dependencies are 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 runtimes

  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. (Recommended) Use Composer to install dependencies.

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

      Example:

      {
        "require": {
          "aliyunfc/fc-php-sdk": "~1.2",
          "alibabacloud/fnf": "^1.7"
        }
      }
    2. Install dependencies.

      composer install

    After the dependencies are installed, check whether your directory structure complies with the following structure:

    my-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

Create a custom layer in the Function Compute console

Prerequisites

A function is created. For more information, see Create a function.

Procedure

  1. For more information about how to build a ZIP package of a layer, see Build a ZIP package for a layer.

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

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

  4. On the Create Layer page, specify related parameters and click Create.

    Parameter

    Description

    Name

    Enter a name for the layer that you want to create.

    Description

    Enter the description of the layer. Layer descriptions can be used to distinguish layers.

    Compatible Runtime

    Select compatible runtimes.

    Layer Upload Method

    Select a method to upload the layer dependencies. You can use one of the following methods to upload 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: Specify Build Environment and upload the dependency file package.json or requirements.txt of the layer.

      Note

      Only Python and Node.js runtimes support building dependency layers online. The time limit for uploading a dependency package is 15 minutes and the size limit of a dependency package is 500 MB.

    After the layer is created, the system generates a layer version. The version number starts from 1.

  5. 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 layer that you want to manage 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 a runtime, upload the new layer code, and then click Create.

Create a custom layer by using Serverless Devs

Prerequisites

Procedure

  1. For more information about how to build a ZIP package of a layer, see Build a ZIP package for a layer.

  2. 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 compatible runtimes of the layer.

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

    After the layer is created, a success message is displayed and the ARN of the layer is returned. The ARN contains three parts, which are separated 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. You can log on to the Function Compute console to view the information about the created layer.dg-createlayer-success

  3. 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 a region.

  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 that appears, click Delete.

References