Layers allow you to publish and deploy custom resources such as common dependencies, runtime environments, and function extensions. You can extract the public libraries on which functions depend into layers to reduce the sizes of code packages when you deploy and update the code. This topic describes the principles of layers, the descriptions of layers in different runtimes, and how to build the ZIP package of a layer.
How it works
- When you build a layer, you need to package the content into a ZIP file. The Function Compute runtimes decompress and deploy the content of the layer in the /opt directory.
- When multiple layers are configured for a function, the content of these layers is merged and stored in the /opt directory in reverse order. If layers contain files with the same name, the files in the first configured layer overwrites the files with the same names in the later configured layer.
- If the 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.
- If the libraries in the layer depend on instruction sets, use a machine that runs on the AMD64 architecture, or perform cross-compilation to ensure the compatibility between the libraries and the Function Compute runtimes.
Layer file directory and file structure of different runtimes
For a runtime that supports layers, Function Compute adds a specific directory to the search path of the dependency package of the runtime language. If the same folder structure is defined in the layer ZIP package, the function code can access the layer without specifying the 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 |
Runtimes other than custom runtimes and custom containers | /opt/bin (PATH) |
/opt/lib (LD_LIBRARY_PATH) |
File structures of the ZIP packages in each runtime
The file structure after the
requests dependencies are usedmy-layer-code.zip └── python └── requests Path after the ZIP package is decompressed and deployed
/ └── opt └── python └── requests
The file structure after the
uuid dependencies are usedmy-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.json
The file structure after the
jackson-core dependencies are usedmy-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.jar
The file structure after the
composer dependencies are usedmy-layer-code.zip └── php ├──composer.json ├──composer.lock └──vendor Path after the ZIP package is decompressed and deployed
/ └── opt └── php ├──composer.json ├──composer.lock └──vendor
Build the ZIP package of the layer
When you create a layer, you need to package the content into a ZIP file. The Function Compute runtimes decompress and deploy the content of the layer in the /opt directory.
The method to build the ZIP package of a layer is similar to the method to build code packages. The code directory structure of the libraries must comply with the standard directory specifications for different languages. This ensures that the libraries can be loaded when Function Compute runs. For more information, see Layer file directory and file structure of different runtimes. For the function dependency libraries that are deployed in a layer, Function Compute runtimes automatically add the search paths of the dependency libraries of each language if you these libraries are packaged based on the specifications. You do not need to specify the full paths. If you want to customize the directory structure of the layer, you need to explicitly add the search paths of the dependency libraries to the code. For more information, see How do I reference dependencies in a layer in a custom runtime?.
This section describes how to build the ZIP package of a layer in each runtime.