Skip to main content

file layer

If your cloud function has numerous dependencies or common code files, you can use layers within the cloud function for management. By employing layer management, you can place dependencies in layers instead of the deployment package, ensuring the deployment package remains compact in size.

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 platform will retain the file from the layer with the highest order number.
  • If the layer version you are using is deleted, the function bound to it will continue to run.

Creating a Layer

You can use the following command to create a layer. When creating a layer, you need to specify the layer alias, environment ID, and the path to the layer content. The layer name, composed of the layer alias and environment ID, serves as the unique identifier for the layer. Each layer supports multiple cloud function runtimes and can be bound to cloud functions with different runtimes. Layers created via CLI support four runtimes by default: Nodejs8.9, Nodejs12.16, Php7, and Java8.

The content of the uploaded layer is in ZIP file format. When using the CLI for upload, you can specify the file option as either a folder path or a ZIP file path. If a folder path is specified, the CLI will package the folder contents into a ZIP file.

Each time a new layer is created successfully, a new layer version is generated. Layer versions start from 1, and each new version is the maximum existing version + 1. File layers cannot be updated; you can only publish new layers or delete existing ones. Any version of a file layer can be deleted. Deleted versions cannot be bound to new cloud functions. Deleting file layers or their versions does not affect already bound cloud functions, which can still use the layer.

A single user can create up to 20 file layers. The version numbers for a single file layer start from 1 and can go up to 200.

tip

It is not recommended for layer files to exceed 100MB in size.

# Uploading File Directory
tcb fn layer create <alias> -e envId --file ./content
# Uploading ZIP File
tcb fn layer create <alias> -e envId --file ./file.zip

Binding Layers to Cloud Functions

After binding a layer to a cloud function, you can access the layer content within the cloud function. Use the command below to bind a layer to a cloud function, where the name parameter specifies the cloud function's name.

tcb fn layer bind <name> -e envId
tip

Each function can be configured with a maximum of 5 file layers (uniquely identified by layer name + version).

Unbinding Layers from Cloud Functions

Use the command below to unbind layers from a cloud function, where the name parameter specifies the cloud function's name.

tcb fn layer unbind <name> -e envId

Adjusting Layer Order

Use the command below to reorder layers bound to a cloud function, where the name parameter specifies the cloud function's name.

tcb fn layer sort <name> -e envId

Downloading Layer Files

Use the command below to download layer files. Only layer files from the specified environment can be downloaded.

tcb fn layer download -e envId

Deleting Layers

You can use the command below to delete layers. Only layers created in the specified environment can be deleted. When all versions of a layer are deleted, the layer itself is also deleted, and you will no longer be able to see this layer.

tcb fn layer delete -e envId

After completely deleting a file layer, if you create a new file layer with the same name, use the version number that is the highest version of the original file layer + 1.

Displaying Layers

You can use the command below to retrieve information about layers.

# Displaying Layers in All Environments
tcb fn layer list

# Displaying Layers in the Specified Environment
tcb fn layer list -e envId

# Displaying Layers Bound to Functions
tcb fn layer list -e envId --name <name>

# Displaying Layer Version Information
tcb fn layer list -e envId --layer <layer>