Layers allow you to publish and deploy custom resources, such as public libraries, runtimes, and function extensions. You can abstract the public libraries on which a function depends to a layer. This reduces the size of the code package when you deploy or update the function. You can also deploy a custom runtime as a layer to share the runtime among multiple functions. This topic describes the principles, usage, and restrictions of the layer, as well as the configuration and application of the function layer.

How layers work

By default, a layer is deployed in the /opt directory of the execution environment of a function and serves as an additional code directory for the function. When multiple layers are configured for a function, the content of these layers is combined to the /opt directory. If multiple layers that have the same version are configured, the layers are loaded based on the sequence in which they are configured. The function uses the content of the layer that is first configured. The libraries on which a function depends are deployed as a layer. If you package these libraries based on the specifications, Function Compute appends the paths to the libraries when it runs. You do not need to specify full paths.

Build layers

The method used to build layers is similar to that used to build code packages. The directory structure of the libraries that are published as layers must comply with the standard directory specifications for different languages. This ensures that the libraries can be loaded when Function Compute runs. You can also customize the directory structure, but you must explicitly specify the load address of the library in the function code, that is, specify the full path of the library. Function Compute supports Python and Node.js. This topic describes how to build layers in the above two runtime environments.
Note The sample target directory is used to contain the contents of the layer. You can replace the directory name as needed during the actual operation. When deployed through Function Compute, the target/xxx is mapped to /opt/xxx.
  • Python
    1. Run the following command to build the destination directory of the layer:
      mkdir -p target/python
    2. You can select one of the following methods to install dependencies:
      • Method 1: Run the following command in the target/python to download the dependent library:
        pip install --target=target/python ${PACKAGE_NAME}
        Note ${PACKAGE_NAME} indicates the name of the Python library that you want to install. You must specify the name.
      • Method 2: Copy your library to the target/python directory. To specify a version of the library, you can copy the library to the target/python/lib/python{major-version}.{minior-version}/site-packages/ directory.

      When the system deploys the layer, it maps the target/python directory, maps it to the /opt/python directory, and then adds the /opt/python or /opt/python/lib/python{major-version}.{minior-version}/site-packages to the Python sys.path.

      Note The python{major-version}.{minior-version} is a Python version, such as python3.6. see Python runtime environment.
    3. Execute the following command in the target directory to package the output of the directory, which is the code package of the layer.
      zip -r ${PACKAGE_NAME} ./*
  • Node.js
    1. Run the following command to build the destination directory of the layer:
      mkdir -p target/nodejs
    2. You can select one of the following methods to install dependencies:
      • Method 1: Run the following command in the target directory to install dependencies:
        1. Run the following command in the target/nodejs directory to initialize the application.
          npm init
        2. Run the following command to install dependencies:
          npm install --save ${PACKAGE_NAME}
          Note ${PACKAGE_NAME} indicates the name of the Node.js library that you want to install. You must specify the name.
      • Method 2: Copy your library to the target/nodejs/node_modules directory. To specify a version of the library, you can copy the library to the target/nodejs/node{version}/node_modules directory.

        When the system deploys the layer, the /opt/nodejs/node{version}/node_modules:/opt/nodejs/node_modules directory is added to the NODE_PATH variable.

        Note The node{version} is a Node.js version, for example, node12. see Node.js runtime environments.
    3. Execute the following command in the target directory to package the output of the directory, which is the code package of the layer.
      zip -r ${PACKAGE_NAME} ./*

Limits

  • A function can create up to five layers.
  • The size of a layer must be smaller than 50 MB after the layer is decompressed.
  • Currently, the running environment only supports Python and Node.js.
  • The runtime environment of the layer must be consistent with the runtime environment of the function.

Apply function layers

If you deploy libraries as a layer in a standard manner, you can directly reference the libraries in the function code. After you specify a layer that contains the moment library when you create or update a function in Node.js, you can directly reference the moment library in the function code.

const moment = require('moment');

module.exports.handler = function(event, context, callback) {
    console.log('Receive event:', event.toString());
    let momentNow = moment.now();
    callback(null, JSON.stringify({momentNow}));
};

Create and use layers

  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, click Layer Management.
  3. In the top navigation bar, select the region where the service resides.
  4. In the upper-left corner of the Layer Management page, click Create Layer.
  5. On the Create Layer page, set the parameters and click Create.
    pg_create_layer

    The following table describes the parameters.

    Parameter Description
    Parameter The custom name of the layer.
    Description The description of the layer. Layer descriptions can be used to distinguish between layers.
    Running Environment The runtime environment of the code. Only Python and Node.js are supported.
    Code upload method The method to upload the code.
    Note If you use ZIP files or folders to upload code, the compressed files can be up to 50 MB. If you use OSS objects to upload code, the compressed files can be up to 100 MB.
    After a layer is created, the layer version is automatically generated. The version number starts from 1.
  6. Create a new version.
    Note You cannot modify the version of a created layer or related layer. If you need to modify the configuration of a layer, you can create a layer or create a new version. However, when updating the layer configuration, you must first delete the reference to the deleted version.
    1. On the Layer Management page, find the target layer and click Versions in the Actions column.
    2. In the Versions section, click Create Version.
    3. On the Create Version page, select the runtime environment, upload the new layer code, and then click Create.
  7. Configure the layer and layer version for the function.
    When configuring a layer for a function, if the layer has multiple versions, it will be bound to the function according to the specific layer version.
    1. In the left-side navigation pane, click Services and Functions.
    2. On the Services page, click the target service.
    3. On the Functions page, find the function that you want to manage and click Configure in the Actions column.
    4. In the Layer section, click + Add Configuration, select the required configuration from the Layer Name and Layer Version drop-down lists, and then click Save.
      Note A function can create up to five layers.

Deleting a Layer and Layer Version

Notice Before you delete a layer, make sure that the layer is not referenced by a function. Otherwise, function exceptions may occur.
  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, click Layer Management.
  3. In the top navigation bar, select the region where the service resides.
  4. On the Layer Management page, select Delete Layer or Layer Version as needed.
    • Delete a layer

      Find the target layer and click Delete in the Actions column. In the dialog box that appears, select Delete all N versions in the layer. check box, and then click Delete.

    • Deleting a Layer Version

      Click the name of the target layer. In the Versions section of the layer details page, click Delete in the Actions column. In the Confirm dialog box that appears, click Delete.

References