Skip to main content

Layer Management

If your cloud function has numerous dependencies or common code files, you can use layers within the cloud function for management.

By using layer management, you can place dependencies in layers instead of the deployment package, ensuring the deployment package remains compact in size. For Node.js, Python, and PHP functions, as long as the deployment package is kept below 10MB, you can edit the function code online in the console.

How It Works

Creating and Binding

The ZIP file for the created layer will be stored according to the layer version.

When binding a layer to a function, it is bound according to the specific layer version and function version.

A function currently supports binding up to 5 specific layer versions, and the binding order must be followed.

Runtime Loading and Access

When a function with bound layers is triggered to run and a concurrent instance is started, the function's runtime code will be decompressed and loaded into the /var/user/ directory, while the layer contents will be decompressed and loaded into the /opt directory.

If the file file you need to use or access is placed in the root directory of the ZIP file when creating the layer, it can be directly accessed via the path /opt/file after decompression and loading. If the file is compressed within a folder dir/file during layer creation, it should be accessed through /opt/dir/file during function execution.

When multiple layers are bound to a function, the decompression and loading of files from the layers will follow the binding order. The layers are sorted in ascending order of their sequence numbers, with higher-numbered layers loaded later. However, all loading is completed before the concurrent instance of the function starts. The files in the layers are already available for use during function code initialization.

Layers are typically used to store infrequently changed static files or dependency libraries. When storing dependency libraries, you can directly package and upload the available dependencies to the layer.

For example, in a Python environment, you can directly package the folder of the dependency library's code package and create it as a layer, then reference it via import in the function code.

In the Nodejs environment, you can package the project's node_modules dependency library folder and create it as a layer, then reference it directly via require in the function code.

By using layers, you can separate function code from dependency libraries or static files, keeping the function code compact. Updates can be quickly uploaded when editing functions via command-line tools, IDE plugins, or the console.

Notes

  • Files in the layer will be added to the /opt directory, which is accessible during function execution.
  • If your function is bound to multiple layers, these layers will be merged into the /opt directory sequentially. If the same file appears in multiple layers, the cloud function will retain the file from the layer with the highest order number.

Step 1: Creating a Layer

  1. Go to the Cloud Functions menu in the CloudBase platform, then click Layer Management to access the "layer" list page.
  2. On the Create Layer page, configure the layer information according to your actual needs, as shown in the figure below:
    • Layer name: Enter a custom layer name.
    • Description: The description of the layer. Fill in according to the actual situation.
    • Layer code: Supports local upload of zip packages, with a maximum size of 50 MB. After confirming the submission method, click Upload. In the pop-up dependency package selection interface, select the dependency package to be uploaded and click OK.
    • Add Runtime Environment: The compatible runtime environments for this layer, which can be configured for up to 5.
  3. Click 'OK' to complete the creation.

Step 2: Binding Layers to Cloud Functions

  1. Select the function that requires layer management and enter the function management page.
  2. Select the Layer Management tab and click Bind, as shown in the figure below:
  3. In the pop-up "Bind Layer" window, select the corresponding Layer name and Layer version, as shown in the figure below:
  4. Click "OK" to complete the binding.