Layers package shared dependencies, runtimes, and function extensions for reuse across functions. Extract common libraries into layers or use Function Compute common layers to reduce code package size.
Usage notes
-
Package all layer content into a ZIP file. Function Compute runtimes extract layers to the /opt directory.
-
When a function has multiple layers, their contents merge into the /opt directory in reverse order. Files in upper layers overwrite same-named files in lower layers.
-
If the layer includes binary libraries or executables, compile on a Linux system. Debian 9 is recommended.
-
If a layer dependency requires specific instruction sets, use an AMD64 machine or cross-compile for compatibility with Function Compute runtimes.
Layer directories by runtime
For runtimes that support layers, Function Compute adds runtime-specific directories to the dependency search path. If your layer ZIP follows the folder structure below, function code loads layer dependencies without specifying the full path.
Directories that can be added in each runtime
|
Runtime |
Directory |
|
Python |
/opt/python |
|
Node.js |
/opt/nodejs/node_modules |
|
Java |
/opt/java/lib |
|
PHP |
/opt/php |
|
Other runtimes (excluding custom runtimes and Custom Container runtimes) |
/opt/bin (PATH) |
|
/opt/lib (LD_LIBRARY_PATH) |
File structures of the ZIP packages in each runtime
File structure after the requests dependency is used for packaging
my-layer-code.zip
└── python
└── requests
Path after the ZIP package is decompressed and deployed
/
└── opt
└── python
└── requestsFile structure after the uuid dependency is used for packaging
my-layer-code.zip
└── nodejs
├── node_modules
│ └── uuid
├── package-lock.json
└── package.json
Path after the ZIP package is decompressed and deployed
/
└── opt
└── nodejs
├── node_modules
│ └── uuid
├── package-lock.json
└── package.jsonFile structure after the jackson-core dependency is used for packaging
my-layer-code.zip
└── java
└── lib
└── commons-lang3-3.12.0.jar
Path after the ZIP package is decompressed and deployed
/
└── opt
└── java
└── lib
└── commons-lang3-3.12.0.jarFile structure after the composer dependency is used for packaging
my-layer-code.zip
└── php
├──composer.json
├──composer.lock
└──vendor
Path after the ZIP file is decompressed and deployed
/
└── opt
└── php
├──composer.json
├──composer.lock
└──vendorBuild a ZIP package for a layer
Package layer content into a ZIP file. Function Compute runtimes extract it to the /opt directory.
Building a layer ZIP follows the same approach as building a code package. Layer library directories must comply with standard packaging rules for each language. For more information, see Layer directories by runtime. If you follow these rules, Function Compute runtimes automatically add the library search paths. To customize the layer directory structure, explicitly add dependency search paths in your code. For more information, see How to reference dependencies from a layer in a Custom Runtime
The following tabs show how to build the layer ZIP package for each runtime.
-
The programming language on your local machine must match the Function Compute runtime.
-
The my-layer-code directory is an example. Replace it with your actual directory.
Python runtimes
The Python version on your local machine must match the Function Compute runtime version.
-
Create a working directory:
mkdir my-layer-code -
Enter the working directory.
cd my-layer-code -
Install the dependency library in my-layer-code/python:
pip install --target ./python ${PackageName}Replace ${PackageName} with the dependency library name. The
pip installcommand options are covered in the pip install reference.Sample code:
pip install --target ./python numpyVerify the resulting directory structure:
my-layer-code └── python ├── bin ├── numpy ├── numpy-1.22.4.dist-info └── numpy.libs -
Package the dependencies from the my-layer-code directory:
zip -r my-layer-code.zip python
Node.js runtimes
The Node.js version on your local machine must match the Function Compute runtime version.
-
Create a working directory:
mkdir my-layer-code -
Enter the working directory.
cd my-layer-code -
Install the dependency library in my-layer-code/nodejs:
npm install --prefix ./nodejs --save ${PackageName}Replace
${PackageName}with the dependency library name. Thenpm installcommand options are covered in the npm-install reference.Sample code:
npm install --prefix ./nodejs --save uuidVerify the resulting directory structure:
my-layer-code └── nodejs ├── node_modules │ └── uuid ├── package-lock.json └── package.json -
Package the dependency from my-layer-code:
zip -r my-layer-code.zip nodejs
Java runtimes
-
Create a working directory:
mkdir my-layer-code/java -
Enter the working directory.
cd my-layer-code/java -
Install dependencies by using Maven.
-
Create pom.xml in the my-layer-code/java directory.
Example:
<?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> <!-- The Maven dependencies to download --> <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> <!-- Maven-related download plug-ins --> <plugin> <artifactId>maven-dependency-plugin</artifactId> <configuration> <!-- Specify whether to exclude indirect dependencies. Default value: false. --> <excludeTransitive>false</excludeTransitive> <!-- Specify whether to remove the version information in the suffix of the dependency JAR package. Default value: false. --> <stripVersion>false</stripVersion> <!-- The path of the output file--> <outputDirectory>./lib</outputDirectory> </configuration> </plugin> </plugins> </build> </project>Description
-
The dependency package to install is
org.apache.commons.lang3. -
Use
maven-dependency-pluginto copy the dependency package to the /java/lib directory.
-
-
Install dependencies in the my-layer-code/java directory:
mvn dependency:copy-dependencies
Verify the resulting directory structure:
my-layer-code └── java └── lib └── commons-lang3-3.12.0.jar -
-
Package the dependencies from the my-layer-code directory:
zip -r my-layer-code.zip java
PHP runtimes
-
Create a working directory:
mkdir -p my-layer-code/php -
Enter the working directory.
cd my-layer-code/php -
(Recommended) Use Composer to install dependencies.
-
Create composer.json in the my-layer-code/php directory.
Example:
{ "require": { "aliyunfc/fc-php-sdk": "~1.2", "alibabacloud/fnf": "^1.7" } } -
Install dependencies.
composer install
Verify the resulting directory structure:
my-layer-code └─php ├──composer.json ├──composer.lock └──vendor -
-
Package the dependencies from the my-layer-code directory:
zip -r my-layer-code.zip php
Create a custom layer
Create a custom layer in the Function Compute console
Prerequisites
A function is created. For more information, see Create a function.
Procedure
-
For more information about how to build a ZIP package of a layer, see Build a ZIP package for a layer.
Log on to the Function Compute console. In the left-side navigation pane, choose .
In the top navigation bar, select a region. On the Layers page, click Create Layer.
-
On the Create Layer page, specify related parameters and click Create.
Parameter
Description
Name
Enter a name for the layer that you want to create.
Description
Enter a description to identify the layer.
Compatible Runtime
Select compatible runtimes.
Layer Upload Method
Select an upload method:
-
Upload Layer in ZIP Package: Select the ZIP file of the layer.
-
Upload Layer in Folder: Select the folder that contains the layer ZIP file.
-
Upload Layer Using OSS: Specify the Bucket Name and Object Name of the layer ZIP file in OSS.
-
Build Dependency Layer Online: Select a Build Environment and upload
package.jsonorrequirements.txt.NoteOnly Python and Node.js runtimes support online builds. Upload limit: 500 MB, 15-minute timeout.
The system generates layer version 1 after creation.
-
-
Create a new version.
Note You cannot modify a created layer or the versions of a created layer. If you want to update the configurations of a layer, you can create a new layer or a new version. If a referenced layer version is deleted, the reference must be deleted before you update the layer configurations.-
On the Layers page, click the layer name or click Versions in the Actions column.
-
In the Versions section, click Create Version.
-
Select a runtime, upload the new layer code, and click Create.
-
Create a custom layer by using Serverless Devs
Prerequisites
Procedure
-
For more information about how to build a ZIP package of a layer, see Build a ZIP package for a layer.
-
Create a layer:
s cli fc layer publish --code ./my-layer-code --compatible-runtime java8,Java11,custom --region cn-hangzhou --layer-name my-layerDescription:
-
--code: the code package path. -
--compatible-runtime: compatible runtimes for the layer. -
--layer-name: the layer name.
A success message returns the layer ARN, which contains three
#-separated parts: account ID, layer name, and layer version. Log on to the Function Compute console to view the layer details.
-
-
Create a new version for the layer:
s cli fc layer publish --code ./my-layer-code --compatible-runtime java8,java11,custom --region cn-hangzhou --layer-name my-layerNote You cannot modify a created layer or the versions of a created layer. If you want to update the configurations of a layer, you can create a new layer or a new version. If a referenced layer version is deleted, the reference must be deleted before you update the layer configurations.
Delete layers and layer versions
Delete unused layers or layer versions. Deleted layers cannot be viewed or referenced by function configurations, but functions already referencing them continue to run.
Log on to the Function Compute console.
-
In the left-side navigation pane, choose .
In the top navigation bar, select a region.
-
On the Layers page, delete a layer or a layer version based on your business requirements.
-
Delete a layer
Find the layer and click Delete in the Actions column. Select I want to delete all of the N versions at the layer, and click Delete.
-
Delete a layer version
Click the layer name. In the Versions section, find the version and click Delete in the Actions column. In the Confirm message, click Delete.
-
References
-
Use the
layersparameter to configure layers when creating or updating functions. For more information, see CreateFunction and UpdateFunction. -
For more information about how to use a custom layer in a function, see Manage layers and Configure a public layer for a function.