Layers allow you to publish and deploy custom resources such as public libraries, runtimes, and function extensions. The public libraries on which a function depends can be abstracted to a layer. This reduces the size of code packages 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 limits of layers.

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. In this case, you must explicitly specify the full paths to the libraries in functions. The following examples demonstrate how to build layers. In these examples, the Python and Node.js environment languages are used.
Note In these examples, layers are built in the /tmp directory. Packages that are built in the /tmp directory are deployed in the /opt directory later. Replace the example path with the actual path when you perform the operation.
  • Python
    1. Run the following command in the /tmp directory to build the destination directory of a layer:
      mkdir -p target/python
    2. Use one of the following methods to install dependencies:
      • Run the following command in the /tmp/target/python directory to specify the dependency library that you want to download:
        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.
      • Copy the library to the /tmp/target/python directory. To specify a version of the library, you can copy the library to the /tmp/target/python/lib/python{major-version}.{minior-version}/site-packages/ directory.

        When the system deploys the layer, the /opt/python or /opt/python/lib/python{}.{}/site-packages directory is added to the sys.path variable in Python.

    3. Run the following command in the /tmp/target directory to package the directory to build the code package of the layer:
      zip -r ${PACKAGE_NAME} ./*
  • Node.js
    1. Run the following command in the /tmp directory to build the destination directory of the layer:
      mkdir -p target/nodejs
    2. Use one of the following methods to install dependencies:
      • Run the following commands in the destination directory to install dependencies:
        1. Run the following command in the /tmp/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.
      • Copy the library to the /tmp/target/nodejs/node_modules directory. To specify a version of the library, you can copy the library to the /tmp/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.

    3. Run the following command in the /tmp/target directory to package the directory to build the code package of the layer:
      zip -r ${PACKAGE_NAME} ./*

Use layers in functions

Configure function layers

When you create or update a function, you can specify the Alibaba Cloud Resource Names (ARNs) of a group of layers and call API operations to configure the layers. For more information, see PublishLayerVersion, DeleteLayerVersion, GetLayerVersion, ListLayerVersions, and ListLayers.
Notice The layers that you specify must be compatible with the runtime 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}));
};

Manage layers

You can use one of the following methods to manage layers:

Limits

  • You can create up to five layers for a function.
  • The size of a layer must be smaller than 50 MB after the layer is decompressed.