This article outlines how to utilize layers via the Serverless console.
Use Instructions
Files in the layer are all under the /opt/ directory, which can be accessed through their absolute paths in the function code. In addition, the built-in environment variables of each runtime also include layer paths, so files can be uploaded according to such paths, and then they can be imported through their relative paths in the code.
For the environment variables in Python, Java, and Node.js, see the table below:
The following takes importing the cos-nodejs-sdk-v5 dependency from node_modules in a layer in the code in the Node.js runtime environment as an example:
1. Refer to the Create Layer steps to upload node_modules and generate a layer. The local function directory structure is as shown below:
2. Refer to Deploy Function to package and upload the local function code. Execute the following command to exclude the node_modules folder during packaging.
4. You can import files at the layer in the function after completing the steps above.
'use strict'
varCOS=require('cos-nodejs-sdk-v5')
Note
Since the NODE_PATH environment variable includes the /opt/node_modules path, there is no need to specify the absolute path of the dependency. The SCF runtime will load files according to the path specified in the environment variable.
If the file path in the layer and the path included in the environment variable are different, you need to use the absolute path when importing the file.
Python
The following takes importing the cos-python-sdk-v5 dependency from a layer in the code in the Python runtime environment as an example:
1. Refer to the Create Layer steps to upload and generate a layer for cos-python-sdk-v5.
2. Package and upload the local function code as instructed in Deploying Function. Files that have already been uploaded to the layer don't need to be uploaded again together with the function code.
4. You can import files at the layer in the function after completing the steps above.
# -*- coding: utf8 -*-
import cos-python-sdk-v5
Note
Since the PYTHONPATH environment variable includes the /opt path, there is no need to specify the absolute path of the dependency. The SCF runtime will load files according to the path specified in the environment variable.
If the file path in the layer and the path included in the environment variable are different, you need to use the absolute path when importing the file.
Sample Code
This example demonstrates how to use layers and test functions.
1. Navigate to scf_layer_demo, select Clone or download > Download ZIP to download the sample to your local machine and unzip it.
2. Log in to the Serverless Console and create a layer. For detailed operation steps, see Creating a Layer. Set the parameters as shown in the following figure:
Layer name: enter a custom name. This document uses demo as an example.
Submiting method: Select "Upload local folder" and choose the layer folder from the directory obtained in Step 1.
Runtime environment: Select "Nodejs 12.16".
3. Log in to the Serverless Console and create a new function. For detailed operation steps, see Creating a Function. The basic configuration is as follows:
Creation Method: Select "From Scratch".
Function Type: Select Event Function.
Function Name: In this context, layerDemo is used as an example.
Region: The region is filled in by default.
Runtime Environment: Select "Nodejs 12.16".
Time Zone: The cloud function uses UTC time by default.
4. In the Function codes section, select "Local folder" and choose to upload the function folder from the folder obtained in Step 1. As shown in the image below:
5. In the Advanced Settings > Layer configuration section, click Add a Layer.
6. Choose the layer name and layer version for the function, as depicted in the figure below:
Layer name: Select the demo layer created in Step 2.
Layer version: select v1.
7. Click Complete at the bottom of the page. After creation, you can view the function details.
8. In "Function Management", select the Function Code tab and click Test at the bottom of the page to view the results, as depicted in the figure below: