Layer Management
Cloud Functions Layer is a code sharing mechanism that allows dependency libraries, public code files, and other resources to be independently managed, enabling code reuse across multiple functions.
What is a Layer
Layer is an important feature of Cloud Functions, allowing you to package dependency libraries, runtime environments, configuration files, etc. into independent layers for shared use across multiple functions. Using layers enables you to:
- Reduce deployment package size: Separate dependency libraries from function code to keep deployment packages lightweight
- Improve Development Efficiency: Common code only needs to be maintained once and can be reused by multiple functions.
- Support online editing: For Node.js, Python, and PHP functions, online editing is available in the console when the code package is kept under 10MB.
How It Works
Layer Creation and Version Management
- Each layer has independent version management, and a corresponding version number is generated when the layer is created.
- Layer content is stored in compressed packages, supporting version iteration and rollback.
- Layers can be configured with compatible runtime environments to ensure matching with the function runtime.
Layer Binding Mechanism
- Functions are bound to layers by specific versions to ensure the stability of the runtime environment.
- A single function supports binding up to 5 layers.
- The loading order of layers can be specified during binding.
Runtime Loading Process
When a function bound to layers is triggered, the system follows the following loading process:
- Function Code Loading: Function code is decompressed to the
/var/user/directory - Layer Content Loading: All bound layers are extracted sequentially to the
/optdirectory - File Access Rules:
- Files in the layer's root directory: accessible via
/opt/filename - Files in subdirectories within the layer: accessible via
/opt/directoryname/filename
- Files in the layer's root directory: accessible via
Multi-layer Loading Order
When a function is bound to multiple layers:
- Layers are loaded sequentially in ascending order based on the sequence numbers set during binding.
- Later-loaded layers will overwrite same-named files in earlier-loaded layers
- All layers are loaded before the function instance starts and are available for use during function initialization.
Use Cases and Best Practices
Applicable Scenarios
- Dependency Library Management: Centralized management of third-party libraries, SDKs, and other dependencies
- Public Code Reuse: utility classes and configuration files shared by multiple functions
- Static Resource Storage: model files, configuration files, and other infrequently changed resources
Best Practices
- Python
- Node.js
# 1. Create a dependency directory
mkdir python-layer
cd python-layer
# 2. Install dependencies to the specified directory
pip install requests -t python/
# 3. Package into a zip file
zip -r python-layer.zip python/

- All layers are loaded before the function instance starts and are available for use during function initialization.
import requests # Directly import dependencies from the layer
def main_handler(event, context):
response = requests.get('https://api.example.com')
return response.json()
# 1. Create a dependency directory
mkdir nodejs-layer
cd nodejs-layer
# 2. Initialize and install dependencies
npm init -y
npm install axios lodash
# 3. Package node_modules
zip -r nodejs-layer.zip node_modules/
- All layers are loaded before the function instance starts and are available for use during function initialization.
const axios = require('axios'); // Directly reference dependencies from the layer
const _ = require('lodash');
exports.main_handler = async (event, context) => {
const response = await axios.get('https://api.example.com');
return _.pick(response.data, ['id', 'name']);
};
Layer Structure Recommendations
layer.zip
├── python/ # Python dependencies directory
│ ├── requests/
│ └── urllib3/
├── lib/ # Shared libraries directory
│ └── utils.py
└── config/ # Configuration files directory
└── settings.json
Notes
- Files in the layer are extracted to the
/optdirectory and are directly accessible during function execution. - When multiple layers are bound, they are merged in numerical order, and files with the same name will be overwritten by the layer with a higher sequence number.
- The total size of layers is limited to 50MB. It is recommended to properly plan the content of layers.
- Layer versions cannot be modified once created. If updates are needed, please create a new version.
Operation Guide
Layer Creation
Go to the Layer Management page
Configure Layer Information
Click "New" and fill in the following information:

Configuration Item Description Layer Name Custom layer name. It is recommended to use meaningful names Description Description of the layer's purpose and content to facilitate team collaboration Layer Code Upload a zip compressed package, supports up to 500MB Runtime Environment Select compatible runtime environments, up to 8 Upload Layer Code
Click the "Upload" button, select the prepared zip compressed package, after confirming, click the "OK" button to complete the creation.
Binding Layer to Function
Select Target Function
Select the function that requires layer binding from the cloud function list, and go to the function details page.
Go to Layer Management
Switch to the "Layer Management" tab, then click the "Bind" button.

Select Layer Version
In the pop-up window, select the layer name and version to bind:

- Multiple layers (up to 5) can be bound simultaneously.
- The loading order of layers can be adjusted.
- Recommend selecting a stable layer version
Confirm Binding
After the configuration is confirmed to be correct, click "OK" to complete the binding.
Management Layer Version
- View Version History: View all historical versions on the layer details page.
- Create a New Version: Create a new version when modifying layer content to maintain backward compatibility.
- Delete Version: Delete unused versions to free up storage space.
Frequently Asked Questions
Layer Failed to Load Normally
Possible Causes:
- The runtime environment of the layer does not match that of the function.
- The file structure of the layer is incorrect.
- The layer size exceeds the limit.
Solution:
- Check the compatible runtime environment settings for the layer
- Verify that the layer files are packaged in the correct directory structure
- Optimize layer content to keep it within 500MB
Dependency Library Import Failure
Possible Causes:
- Python dependencies are not installed to the correct directory
- The Node.js module path is incorrect.
Solution:
- Python: Use
pip install -t python/to install into the python directory - Node.js: Ensure node_modules is in the root directory of the layer
Multi-layer File Conflicts
Possible Causes:
- Different layers contain files with the same name
- Improper layer loading order
Solution:
- Properly plan the division of content across layers
- Adjust the layer binding order
- Use different directories to avoid filename conflicts