Layers provide the capabilities to publish and deploy resources such as common dependencies, runtimes, and function extensions. You can abstract the public libraries that functions depend on into layers or use public layers of Function Compute to reduce the sizes of code packages when you deploy or update functions. This topic describes layers in different runtimes, how layers work, how to build a layer ZIP file, and how to create and delete a custom layer.
How it works
When you build a layer, you need to package the layer content into a ZIP file. Function Compute runtimes decompress the ZIP file and deploy the layer content to the /opt directory.
If multiple layers are configured for a function, the content of these layers is merged and stored in the
/opt
directory in reverse order. If two layers have a file with the same name, the file in the layer configured first overwrites the file in the layer configured later.For example, if you configure Layer 1 and Layer 2 for a function, Layer 2 is loaded before Layer 1 when a function instance is started. Content of the layers are decompressed to the
/opt
directory. In the/opt
directory, the content of Layer 1 is placed before the content of Layer 2. If a file with the same name exists in the two layers, the file in Layer 1 overwrites the file in Layer 2.If code in the layer depends on binary libraries or executable files, you must use a Linux system to compile and build the layer. Debian 9 is recommended.
Runtimes of Function Compute are based on the x86_64 architecture. If a dependency library in a layer has specific instruction set requirements, you need to use an x86_64 machine or perform cross-compilation to ensure that the dependency library is compatible with the Function Compute runtime.
Usage notes for layers in different runtimes
If a runtime supports layers, Function Compute adds specific directories to the dependency package search path. The following table shows the directories for each programming language. We recommend that you define the same folder structure in the layer ZIP file as the directories listed in the table, so that your function code can access layers without specifying a path. For more information about how to build a layer ZIP file, see Build a layer ZIP file. If you want to specify a custom directory structure for a layer, you must explicitly add search paths of dependency libraries to your code. For more information, see How do I reference dependencies in a layer in a custom runtime?
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) | |
Custom runtimes and Custom Container runtimes | None |
File structure of the .zip file in each runtime
This section describes the file structures of the ZIP file uploaded in each runtime and the paths after decompression and deployment.
File structure after the requests dependency is used for packaging
my-layer-code.zip
└── python
└── requests
Path after the .zip file is decompressed and deployed
/
└── opt
└── python
└ ── requests
File 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 file is decompressed and deployed
/
└── opt
└── nodejs
├── node_modules
│ └── uuid
├── package-lock.json
└ ── package.json
File 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 file is decompressed and deployed
/
└── opt
└── java
└── lib
└── commons-lang3-3.12.0.jar
File 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
└──vendor
Build a .zip file for a layer
When you create a layer, you need to package the layer content into a ZIP file. Function Compute runtimes decompress the ZIP file and deploy the layer content to the /opt directory.
The method to build a layer ZIP file is similar to the way to build a code package. To ensure that functions can load and use the libraries published by the layer, the code directory structure of the libraries must comply with the directory requirements of each language. For more information, see Usage notes for layers in different runtimes. For function dependency libraries in layers, Function Compute runtimes automatically append the search paths of function libraries for different programming languages so you do not need to specify full paths if you package the libraries according to the standard packaging rules. If you want to specify a custom directory structure for a layer, you must explicitly add search paths of dependency libraries to your code. For more information, see How do I reference dependencies in a layer in a custom runtime?
The following sections describe the procedure for how to build a .zip layer in different runtimes.
When you build a layer on an on-premises machine, the programming language that you use must be the same as that of the desired runtime in Function Compute.
In the following sections, the working directory my-layer-code is used as an example. In your actual business scenarios, replace the directory with the actual directory.
Python Runtime
If you build a layer on an on-premises machine, the Python version that you use must be the same as the version of the Python runtime you use in Function Compute.
Run the following command to create a working directory:
mkdir my-layer-code
Go to the working directory.
cd my-layer-code
Run the following command to install the dependency library in my-layer-code/python:
pip install --target ./python ${PackageName}
${PackageName} indicates the name of the dependency library that you want to install. For more information about the
pip install
command, see pip install.Sample code:
pip install --target ./python numpy
After the dependency library is installed, check whether your directory complies with the following structure:
my-layer-code └── python ├── bin ├── numpy ├── numpy-1.22.4.dist-info └── numpy.libs
Run the following command in the my-layer-code directory to package the dependencies:
zip -r my-layer-code.zip python
Node.js Runtime
If you build a layer on an on-premises machine, the Node.js version that you use must be the same as the version of the Node.js runtime you use in Function Compute.
Run the following command to create a working directory:
mkdir my-layer-code
Go to the working directory.
cd my-layer-code
Run the following command to install the dependency library in my-layer-code/nodejs:
npm install --prefix ./nodejs --save ${PackageName}
Replace
${PackageName}
with the name of the dependency library that you want to install. For more information about thenpm install
command, see npm-install.Sample code:
npm install --prefix ./nodejs --save uuid
After the dependency library is installed, check whether your directory complies with the following structure:
my-layer-code └── nodejs ├── node_modules │ └── uuid ├── package-lock.json └── package.json
Run the following command in my-layer-code to package the dependencies:
zip -r my-layer-code.zip nodejs
Java Runtime
Run the following command to create a working directory:
mkdir my-layer-code/java
Go to the working directory.
cd my-layer-code/java
Install dependencies by using Maven.
Create the pom.xml file in the my-layer-code/java directory.
Sample code:
<?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> <!-- Maven dependency that you want 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> <!-- File output path--> <outputDirectory>./lib</outputDirectory> </configuration> </plugin> </plugins> </build> </project>
Description
The dependency fie that you want to install is
org.apache.commons.lang3
.Use
maven-dependency-plugin
to copy the dependency file to the /java/lib directory.
Run the following command in the my-layer-code/java directory to install dependencies:
mvn dependency:copy-dependencies
After the dependency library is installed, check whether your directory complies with the following structure:
my-layer-code └── java └── lib └── commons-lang3-3.12.0.jar
Run the following command in the my-layer-code directory to package the dependencies:
zip -r my-layer-code.zip java
PHP Runtime
Run the following command to create a working directory:
mkdir -p my-layer-code/php
Go to the working directory.
cd my-layer-code/php
Use Composer to install the dependencies.
In the my-layer-code/php directory, create the composer.json file.
Sample code:
{ "require": { "aliyunfc/fc-php-sdk": "~1.2", "alibabacloud/fnf": "^1.7" } }
Run the following command to install dependencies:
composer install
After the installation, check whether your directory complies with the following structure:
my-layer-code └─php ├──composer.json ├──composer.lock └──vendor
Run the following command in the my-layer-code directory to package the dependencies:
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
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, configure parameters and click Create. The following table describes the parameters.
Parameter
Description
Name
Enter a name for the layer that you want to create.
Description
Enter a description that distinguishes the layer from others.
Compatible Runtime
Specify the compatible runtimes.
Layer Upload Method
Select a method to upload layer dependencies. You can use one of the following methods to upload layer dependencies:
Upload Layer in ZIP Package
Select and upload the layer ZIP file. The ZIP file to upload cannot exceed 500 MB.
Upload Layer in Folder
Select and upload the folder that contains the layer ZIP file. The size of a folder cannot exceed 500 MB after it is compressed.
Upload Layer Using OSS
Specify Bucket Name and Object Name to upload the layer ZIP file from Object Storage Service (OSS). The size of the selected object in OSS cannot exceed 500 MB.
Build Dependency Layer Online
If your application is developed based on Python or Node.js, or you need to install some lightweight system libraries, you can select Build Dependency Layer Online and enter the content of the dependency file
package.json
orrequirements.txt
in the editor below. After the settings are submitted, the system automatically installs these language-level dependency packages and system libraries to simplify layer dependency management.
Build Environment
If you select Build Dependency Layer Online, you must specify the runtime in which you want to build the dependency layer.
NoteOnly Python and Node.js runtimes support building dependency layers online.
apt Command
If you select Build Dependency Layer Online and need to install some software packages to the layer, you can directly enter the name of the dependency in the
apt install
input box.After the layer is created, the system generates a layer version that starts from 1 in ascending order.
Create a new version.
NoteYou 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, find the desired layer and click the name of the layer.
In the Versions section of the page that appears, click Create Version.
On the page for creating a layer version, specify runtimes, upload the new layer code, and then click Create.
Create a custom layer by using Serverless Devs
Prerequisites
Serverless Devs and dependencies are installed. For more information, see Install Serverless Devs and dependencies.
Serverless Devs is configured. For more information, see Configure Serverless Devs.
Procedure
Run the following command to create a layer:
s cli fc layer publish --code ./my-layer-code --compatible-runtime java8,Java11,custom --region cn-hangzhou --layer-name my-layer
Description:
--code
: specifies the path of the code package.--compatible-runtime
: specifies compatible runtimes of the layer.--layer-name
: specifies the name of the layer.
After the layer is created, a success message is displayed and the Alibaba Cloud Resource Name (ARN) of the layer is returned. The ARN contains three parts, which are separated by
#
. The first part is the identifier of the account ID, the second part is the name of the layer, and the third part is the version of the layer. The following figure shows an example. You can log on to the Function Compute console to view the information about the created layer.Repeat the following command to create a new version for the created layer:
s cli fc layer publish --code ./my-layer-code --compatible-runtime java8,java11,custom --region cn-hangzhou --layer-name my-layer
NoteYou 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 a layer or its version
You can delete a layer or its version that is no longer required. The deleted layers can no longer be viewed or referenced by function configurations. However, the execution of functions that are configured to reference the layer is not affected.
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 its version based on your business requirements.
Delete a layer
Find the layer that you want to delete and click Delete in the Actions column. In the dialog box that appears, select I want to delete all of the N versions at the layer, and click Delete.
Delete a layer version
Click the name of the layer whose version you want to delete. In the Versions section of the layer details page, find the layer version that you want to delete and click Delete in the Actions column. In the Confirm message that appears, click Delete.
More information
You can also use the
layers
parameter to manage and configure layers when you create a function or update a function by using an API operation or SDK. For more information, see CreateFunction and UpdateFunction.If the dependencies of the layer to be installed contain dynamic-link libraries, or your on-premises environment is incompatible with the Function Compute runtime, you cannot build the layer in the Function Compute console or on an on-premises machine. You can build a layer only based on a Dockerfile. For more information, see Use a Dockerfile to build a layer.
After a custom layer is created, you can bind it to a function in the Function Compute console or by using Serverless Devs so that the function can access resources provided in the layer. For more information, see Configure a custom layer.