tencent cloud

Feedback

Using layer

Last updated: 2024-12-02 20:11:42
    This document describes how to use a layer in the Serverless console.

    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:
    Environment Variable
    Path
    PYTHONPATH
    /var/user:/opt
    CLASSPATH
    /var/runtime/java8:/var/runtime/java8/lib/*:/opt
    NODE_PATH
    /var/user:/var/user/node_modules:/var/lang/node6/lib/node_modules:/opt:/opt/node_modules
    

    Procedure

    Node.js

    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. Upload node_modules to generate a layer as instructed in Creating layer. The local function directory structure is as shown below:
    
    2. Package and upload the local function code as instructed in Deploying Function. During the packaging, run the following command to exclude the node_modules folder:
    zip -r package name.zip . -x "node_modules/*"
    See the figure below:
    
    3. Bind the created layer to the deployed function as instructed in Binding function to layer.
    4. You can import files at the layer in the function after completing the steps above.
    'use strict'
    var COS = require('cos-nodejs-sdk-v5')
    Note:
    As the NODE_PATH environment variable already includes the /opt/node_modules path, there is no need to specify the absolute path of the dependency. SCF will load the file according to the path specified in the environment variable during execution.
    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. Upload cos-python-sdk-v5 to generate a layer as instructed in Creating layer.
    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.
    3. Bind the created layer to the deployed function as instructed in Binding function to layer.
    4. You can import files at the layer in the function after completing the steps above.
    # -*- coding: utf8 -*-
    import cos-python-sdk-v5
    Note:
    As the PYTHONPATH environment variable already includes the /opt path, there is no need to specify the absolute path of the dependency. SCF will load the file according to the path specified in the environment variable during execution.
    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

    Using layer and testing function

    1. Go to scf_layer_demo and select Clone or download > Download ZIP to download the demo and decompress it.
    2. Create a layer as instructed in Creating layer. Set the parameters as shown below:
    
    Layer Name: enter a custom name. This document uses demo as an example.
    Submission Method: select Local folder and select and upload the layer folder in the folder obtained in step 1.
    Runtime Environment: select Nodejs12.16.
    3. Go to the Function Service page and click Create to enter the Create Function page.
    4. Set the basic information of the function in Basic Info on the Create Function page and click Next as shown below:
    
    Function Name: enter a custom name. This document uses layerDemo as an example.
    Runtime Environment: select Nodejs 12.16.
    Creation Method: select Blank function.
    5. In Function Configuration, select Local folder as Submission Method and select and upload the function folder in the folder obtained in step 1.
    
    6. Click Advanced Settings and add the function layer in Layer Configuration as shown below:
    
    Layer Name: select the layer demo created in step 2.
    Layer Version: select v1.
    7. Click Complete at the bottom to complete the function creation.
    8. Select the Function Code tab on the Function Management page. You can click Test at the bottom to view the result as shown below:
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support