This topic describes the basic principles and usage notes of layers in Function Compute. You can use layers 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 way, you can reduce the size of the code package when you deploy or update the function. You can also deploy a custom runtime as a layer so that the runtime can be shared among multiple functions.

Usage notes

Principles

Layers include the additional code that functions can load and are deployed in the /opt directory in the function execution environment. If multiple layers are configured for a function, the content of these layers is merged to the /opt directory. The layers are loaded in the order specified in the function configuration. If the content of multiple layers overlaps, the content of the last applied layer prevails. Assume that the libraries on which a function depends are deployed as a layer. If you package the libraries based on the specifications, Function Compute automatically appends the paths to the libraries when it runs. That is, you do not need to specify the full paths.

Build layers

The method of building layers is similar to that of building 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 properly loaded when Function Compute runs. You can also design the directory structure as required. However, you must explicitly specify the full paths to the libraries in functions. In the following examples, Python and Node.js are used to build layers:

  • Python example:
       # Create the directory for the layer to be built.
       mkdir -p target/python
       # Use pip to download a library to the specified directory, or copy your library to the following directory:
       #target/python
       # Or target/python/lib/python{major-version}.{minior-version}/site-packages/
       pip install --target=target/python ${PACKAGE_NAME}
       # Go to the directory and compress the entire directory to a package named ${PACKAGE_NAME}. This package is used as the code package of the layer.
       cd target/ && zip -r ${PACKAGE_NAME} . /*
  • Node.js example:
     # Create the directory for the layer to be built.
     mkdir -p target/nodejs
     # Go to the specified directory and use npm to install a Node.js library. You can also copy your library to the following directory:
     #target/nodejs/node_modules
     # Or target/nodejs/node{version}/node_modules
     cd target/nodejs
     npm init
     npm install --save ${PACKAGE_NAME}
     # Go to the directory and compress the entire directory to a package named ${PACKAGE_NAME}. This package is used as the code package of the layer.
     cd ../
     zip -r ${PACKAGE_NAME} . /*

Manage layers

You can manage layers in the Function Compute console or by using SDKs or HTTP requests. For more information about how to manage layers in the Function Compute console, see Layer-related operations.

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. For more information about the configuration methods, see API Reference. 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. Assume that you specify a layer that contains the moment library when you create or update a function in Node.js. You can use the require() function to 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}));
};

Limits

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