Layers let you package shared dependency libraries, runtime environments, and function extensions separately from your function code. Attach a layer to a function to reduce your deployment package size and share dependencies across multiple functions without duplicating them.
Common reasons to use layers:
Reduce deployment package size. Move shared dependencies out of each function package and into a layer.
Share dependencies across functions. A single layer can be attached to any number of functions in your account.
Separate core logic from dependencies. Update dependencies and function code independently.
Use the online code editor. Large deployment packages block the in-console editor; layers keep package sizes small.
How it works
Function Compute extracts layer content to the /opt directory at runtime. When you attach multiple layers to a function, their contents are merged into /opt. If the same file exists in multiple layers, the file from the first-attached layer takes precedence.
For example, if you attach Layer 1 before Layer 2, any duplicate files in Layer 2 are overwritten by those in Layer 1.
Build requirements:
If a layer contains binary libraries or executables, build it on Linux (Debian 9 recommended).
Function Compute runs on x86_64. For instruction-set-dependent dependencies, use an x86_64 machine or cross-compile to ensure compatibility.
Supported directories by runtime
Function Compute adds the following directories to each runtime's dependency search path. Structure your layer ZIP package to match these paths so your function code can access layer content without specifying explicit paths.
| Runtime | Directory path |
|---|---|
| Python | /opt/python |
| Node.js | /opt/nodejs/node_modules |
| Java | /opt/java/lib |
| PHP | /opt/php |
| Other runtimes (excluding custom runtime and custom container runtime) | /opt/bin (PATH), /opt/lib (LD_LIBRARY_PATH) |
| Custom runtime and custom container runtime | None |
If you use a custom directory structure instead, explicitly add the dependency search path in your function code. For details, see How to reference dependencies from a layer in a custom runtime.
Layer ZIP file structure
The following examples show the required ZIP file structure and the corresponding deployment paths for each runtime.
Python — packaging with the requests dependency:
my-layer-code.zip
└── python
└── requests
Extracted to:
/opt/python/requestsNode.js — packaging with the uuid dependency:
my-layer-code.zip
└── nodejs
├── node_modules
│ └── uuid
├── package-lock.json
└── package.json
Extracted to:
/opt/nodejs/node_modules/uuidJava — packaging with the jackson-core dependency:
my-layer-code.zip
└── java
└── lib
└── commons-lang3-3.12.0.jar
Extracted to:
/opt/java/lib/commons-lang3-3.12.0.jarPHP — packaging with Composer dependencies:
my-layer-code.zip
└── php
├── composer.json
├── composer.lock
└── vendor
Extracted to:
/opt/php/vendorBuild a layer ZIP file
Package your layer dependencies into a ZIP file that follows the standard directory structure for your runtime. Function Compute then automatically adds the dependency search paths so your code can load libraries without full paths.
Match your local language version to the Function Compute runtime version you plan to use.
Python runtime
Create a working directory:
mkdir my-layer-code cd my-layer-codeInstall dependencies into the
pythonsubdirectory:pip install --target ./python <package-name>Example with
numpy:pip install --target ./python numpyAfter installation, the directory structure looks like this:
my-layer-code └── python ├── bin ├── numpy ├── numpy-1.22.4.dist-info └── numpy.libsPackage the dependencies from the
my-layer-codedirectory:zip -r my-layer-code.zip python
Node.js runtime
Create a working directory:
mkdir my-layer-code cd my-layer-codeInstall dependencies into the
nodejssubdirectory:npm install --prefix ./nodejs --save <package-name>Example with
uuid:npm install --prefix ./nodejs --save uuidAfter installation, the directory structure looks like this:
my-layer-code └── nodejs ├── node_modules │ └── uuid ├── package-lock.json └── package.jsonPackage the dependencies from the
my-layer-codedirectory:zip -r my-layer-code.zip nodejs
Java runtime
Create a working directory:
mkdir my-layer-code/java cd my-layer-code/javaCreate a
pom.xmlfile that declares your dependencies. The following example installscommons-lang3and copies it to./libusing themaven-dependency-plugin:<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>maven.util</groupId> <artifactId>install-layer</artifactId> <version>1.0</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-dependency-plugin</artifactId> <configuration> <!-- Include transitive dependencies --> <excludeTransitive>false</excludeTransitive> <!-- Keep version suffixes in JAR file names --> <stripVersion>false</stripVersion> <!-- Output directory for downloaded JARs --> <outputDirectory>./lib</outputDirectory> </configuration> </plugin> </plugins> </build> </project>Install the dependencies:
mvn dependency:copy-dependenciesAfter installation, the directory structure looks like this:
my-layer-code └── java └── lib └── commons-lang3-3.12.0.jarPackage the dependencies from the
my-layer-codedirectory:zip -r my-layer-code.zip java
PHP runtime
Create a working directory:
mkdir -p my-layer-code/php cd my-layer-code/phpCreate a
composer.jsonfile that declares your dependencies. Example:{ "require": { "aliyunfc/fc-php-sdk": "~1.2", "alibabacloud/fnf": "^1.7" } }Install the dependencies:
composer installAfter installation, the directory structure looks like this:
my-layer-code └── php ├── composer.json ├── composer.lock └── vendorPackage the dependencies from the
my-layer-codedirectory:zip -r my-layer-code.zip php
Create a custom layer
Using the console
Prerequisites
Before you begin, ensure that you have:
A Function Compute function. For details, see Create a function.
Steps
Log in to the Function Compute console. In the left-side navigation pane, choose Advanced Features > Layers.
In the top navigation bar, select a region. On the Layers page, click Create Layer.
Configure the following parameters, then click Create. After the layer is created, version 1 is automatically generated. Each new version increments sequentially.
Parameter Description Name Enter a name for the layer. Description Enter a description for the layer. Compatible Runtime Select the runtimes compatible with this layer. Layer Upload Method Select how to upload the layer's dependencies: Upload Layer in ZIP Package — upload a .zip file (max 500 MB); Upload Layer in Folder — select a folder that Function Compute compresses into a .zip file (max 500 MB); Upload Layer Using OSS — specify the Bucket Name and Object Name of the .zip file in Object Storage Service (OSS) (max 500 MB); Build Dependency Layer Online — build from a dependency file directly (supported for Python with requirements.txtand Node.js withpackage.json, and for lightweight system libraries).Build Environment Appears when Build Dependency Layer Online is selected. Choose the runtime for building the layer. Currently, only Python and Node.js runtimes are supported. apt command Appears when Build Dependency Layer Online is selected. Enter package names after apt installto install system packages into the layer.To add a new version to an existing layer:
On the Layers page, click the name of the target layer.
In the Version Management section, click Create Version.
Select a runtime, upload the new layer code, and click Create.
NoteYou cannot modify an existing layer or any of its versions. To update a layer, create a new version. If a referenced layer version is deleted, remove the reference before updating.
Using Serverless Devs
Prerequisites
Before you begin, ensure that you have:
Steps
Run the following command to create a layer:
Parameter Description --codePath to the layer code package. --compatible-runtimeComma-separated list of compatible runtimes. --layer-nameName of the layer. s cli fc layer publish --code ./my-layer-code --compatible-runtime java8,Java11,custom --region cn-hangzhou --layer-name my-layerParameters: On success, the command returns the layer's Alibaba Cloud Resource Name (ARN). The ARN contains the account ID, layer name, and version number separated by hash symbols (
#).
To add a new version to an existing layer, run the same command with the same layer name:
NoteYou cannot modify an existing layer or any of its versions. To update a layer, create a new version. If a referenced layer version is deleted, remove the reference before updating.
s cli fc layer publish --code ./my-layer-code --compatible-runtime java8,java11,custom --region cn-hangzhou --layer-name my-layer
Delete a layer or layer version
After a layer version is deleted, you can no longer view it or reference it in new function configurations. Functions that already reference the deleted version continue to work normally.
Log in to the Function Compute console. In the left-side navigation pane, choose Advanced Features > Layers.
In the top navigation bar, select a region.
On the Layers page, delete a layer or a layer version:
Delete a layer: Find the target layer, click Delete in the Actions column, select the confirmation checkbox, and click Delete.
Delete a layer version: Open the layer details page, go to the Version Management section, find the target version, click Delete in the Actions column, and click Delete in the Confirm dialog box.
What's next
Bind the custom layer to a function so the function can access its resources. For details, see Configure a custom layer.
Manage and configure layers programmatically using the
layersparameter when you create or update a function via the API or SDK. For details, see CreateFunction and UpdateFunction.If your layer dependencies include shared libraries or your local environment is incompatible with the Function Compute runtime, build the layer from a Dockerfile instead. For details, see Build a layer using a Dockerfile.