All Products
Search
Document Center

Function Compute:Create a custom layer

Last Updated:Feb 02, 2024

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 layers in different runtimes, how layers work, how to build a .zip file for a layer, and how to create and delete a custom layer.

How it works

  • When you build a layer, you need to package the content into a .zip file. Function Compute runtimes decompress and deploy the content of layers to 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 first layer overwrites the file in the subsequent layer.

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

Usage notes for layers in different runtimes

For runtimes that support layers, Function Compute adds specific directories to the paths in which dependencies for different runtimes reside. The following table describes the directories that you can add. If the same folder structure is defined in the .zip file of a layer, function code can access the layer without the need to specify 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 structure of the .zip file in each runtime

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

Path after the .zip file is decompressed and deployed
/
└── opt
    └── python
        └ ── requests 
Use the file structure that is packaged by using the uuid dependency
my-layer-code.zip
└── nodejs
    ├── node_modules
    │   └── uuid
    ├── package-lock.json
    └── package.json

Path after the .zip file is decompressed and deployed
/
└── opt
    └── nodejs
        ├── node_modules
        │   └── uuid
        ├── package-lock.json
        └ ── package.json 
Use the file structure that is packaged by using the jackson-core dependency
my-layer-code.zip
└── java
    └── lib
        └── commons-lang3-3.12.0.jar

Path after the .zip file is decompressed and deployed
/
└── opt
    └── java
        └── lib
            └── commons-lang3-3.12.0.jar
 Use the file structure that is packaged by using the composer dependency
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 file for a layer

When you create a layer, you need to package the content into a .zip file. Function Compute runtimes decopmpress and deploy the content of the layer 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 specify a directory of a custom layer, append the search paths of the dependency libraries to code in an explicit manner. For more information, see How to reference the layer dependencies in a custom runtime?

The following sections describe the procedure for how to build a .zip layer in different runtimes.

Note
  • When you build a layer on an on-premises machine, the programming language that you use must be the same as that of the desired runtime in Function Compute.

  • In the following sections, the working directory my-layer-code is used as an example. In your actual business scenarios, replace the directory with the actual directory.

Python runtime

Note

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

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

    mkdir my-layer-code
  2. Go to the 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}

    The ${PackageName} parameter 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 dependency library is installed, check whether your directory 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 dependency:

    zip -r my-layer-code.zip python

Node.js runtime

Note

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

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

    mkdir my-layer-code
  2. Go to the 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}

    Replace ${PackageName} with 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 dependency library is installed, check whether your directory 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 runtime

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

    mkdir my-layer-code/java
  2. Go to the working directory.

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

    1. Create a pom.xml file in the my-layer-code/java directory.

      Sample code:

      <?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>
          <!-- Maven dependency that you want 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 from the suffix of the .jar file of the dependency. Default value: false. -->
                          <stripVersion>false</stripVersion>
                          <!-- File output path-->
                          <outputDirectory>./lib</outputDirectory>
                      </configuration>
                  </plugin>
              </plugins>
          </build>
      </project>

      Description:

      • The dependency fie that you want to install is org.apache.commons.lang3.

      • Use maven-dependency-plugin to copy the dependency file to the /java/lib directory.

    2. Run the following command in the my-layer-code/java directory to install the dependency:

      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 dependency:

    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 working directory.

    cd my-layer-code/php
  3. Use Composer to install the dependency.

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

      Sample code:

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

      composer install

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

    zip -r my-layer-code.zip php

Create a custom layer in the Function Compute console

Prerequisites

  • A function is created. For more information, see the "Create a function" section of the Manage functions topic.

  • A custom layer is created. For more information, see Create a custom layer.

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, specify parameters and click Create. The following table describes the parameters.

    Parameter

    Description

    Name

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

    Description

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

    Compatible Runtime

    Select compatible runtimes.

    Layer Upload Method

    Select a method to upload layer dependencies. Valid values:

    • 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 the 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 file is 500 MB.

    After the layer is created, the system generates a layer version that starts from 1 in ascending order.

  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.

    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.

Delete a layer or its version

You can delete a layer or its version that is no longer required. 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. 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 its 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.