tencent cloud

Feedback

Operations

Last updated: 2022-01-23 18:03:35

    Overview

    This document describes how to create, bind, and use a layer in the SCF console.

    Directions

    Creating layer

    1. Log in to the SCF console and select Layer on the left sidebar to enter the Layers list page.
    2. Select the target region at the top of the page and click Create.
    3. On the Create Layer page, set the layer information based on your actual needs as shown below:
      • Layer Name: enter a custom layer name.
      • Description: enter descriptive information of the layer as needed.
      • Submission Method: Local ZIP file, Local folder, and Upload a ZIP pack via COS are supported. Please select an appropriate layer file submission method based on your actual needs.
        After confirming the submission method, click Upload, select the desired file, and click OK.
      • Add Runtime Environment: up to 5 runtime environments compatible with this layer can be set.
    4. Click OK.

    Binding function to layer

    1. Log in to the SCF console and select Function Service on the left sidebar to enter the Function Service list page.
    2. Select the function ID for layer management to enter the function management page.
    3. Select the Layer Management tab and click Bind as shown below:
    4. In the Bind Layer window that pops up, select the corresponding Layer Name and Layer Version as shown below:
    5. Click OK.

    Using layer

    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

    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.

    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