Configure a custom layer

更新时间:
复制 MD 格式

This topic describes how to configure a custom layer for a function.

Configure using the console

Prerequisites

Procedure

When you configure a layer that has multiple versions for a function, you must attach the function to a specific layer version.

  1. Log on to the Function Compute console. In the navigation pane on the left, choose Function Management > Functions.

  2. In the top menu bar, select a region. On the Functions page, click the target function.

  3. On the function details page, click the Configuration tab. Click Edit to the right of Advanced Configuration. In the Advanced Configuration panel, choose + Add Layer > Add Custom Layer. Select a Custom Layer and a Layer Version. Then, click Deploy.

    • A function can be configured with a maximum of five layers, including custom layers and public layers.

    • If a function is configured with multiple layers, their contents are merged into the /opt directory. The layers are merged in the reverse order of their configuration. If multiple layers contain a file with the same name, the file from the layer that was configured first takes precedence and overwrites the files from layers that were configured later.

Configure using Serverless Devs

Prerequisites

Procedure

  1. Run the following command to list the custom layers in a specific region.

    s cli fc layer list --custom --region cn-hangzhou

    The command returns a list of custom layers, as shown in the following example. Find and record the arn value of the target layer.

    -
      layerName:         java11_fc_auto_created
      arn:             acs:fc:cn-hangzhou:1034354682****:layers/numpy/versions/1
      version:           1
      acl:               0
      description:
      compatibleRuntime:
        - custom

    Description:

    • layerName: the name of the layer.

    • version: the version of the layer.

    • arn: the ARN of the layer.

    • acl: the permissions of the layer. The digit 0 specifies that the layer is private and the digit 1 specifies that the layer is public. By default, the public layers are public. The custom layers can be set to private or public.

    • description: the description of the layer.

    • compatibleRuntime: the list of compatible runtimes.

  2. Create an s.yaml file in the target directory and add the recorded arn of the layer.

    For example:

    edition: 3.0.0
    name: hello-world-app
    access: "default"
    
    vars: # Global variables
      region: "cn-hangzhou"
    
    resources:
      hello_world:
        component: fc3 # Component name
        # actions:       # Custom execution logic. For more information about how to use actions, see https://docs.serverless-devs.com/serverless-devs/yaml#%E8%A1%8C%E4%B8%BA%E6%8F%8F%E8%BF%B0actions
        props:
          region: ${vars.region}              # For more information about how to use variables, see https://docs.serverless-devs.com/serverless-devs/yaml#%E5%8F%98%E9%87%8F%E8%B5%8B%E5%80%BC
          functionName: "emoji"
          description: 'this is a layer demo'
          runtime: "python3.9"
          code: ./code
          handler: index.handler
          memorySize: 512
          timeout: 60
          internetAccess: true
          layers:    # Attach a layer to the function. The value is the ARN of the layer.
            - acs:fc:cn-hangzhou:1034354682****:layers/numpy/versions/1
  3. Create a code folder in the target directory. Then, add your function code to the code folder.

  4. In the target directory, run the following command to deploy the function and attach the layer.

    s deploy

More information

You can also use API operations or SDKs to manage and configure layers. You can configure layers by calling the following API operations:

You can use OpenAPI Explorer to call API operations and use SDKs.