This topic describes how to configure custom layers for a function.
Configure a custom layers in the Function Compute console
Before you start
Procedure
When you configure a layer that has multiple versions for a function, one version of the layer is bound to the function based on your business requirements.
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the function details page, click the Configurations tab. In the left-side navigation tree, click Layers. Click Modify. In the Layers panel, choose
. Then, select a custom layer from the Custom Layer drop-down list and its version from the Layer Version drop-down list and click Deploy.A function can be configured with a maximum of five layers, including custom layers and public layers.
When multiple layers are configured for a function, the content of these layers is merged to the /opt directory in reverse order. If layers contain files with the same name, the files in the first configured layer overwrites the files with the same names in the later configured layer.
Use Serverless Devs
Before you start
Procedure
Run the following command to find custom layers in the specified region:
s cli fc layer list --custom --region cn-hangzhou
In the custom layer list that is returned, obtain and record the value of
arn
of the desired 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.
In the target directory, create a s.yaml file and enter the obtained layer
arn
information.Example:
edition: 3.0.0 name: hello-world-app access: "default" vars: # The global variable. region: "cn-hangzhou" resources: hello_world: component: fc3 # The component name actions: # The custom execution logic. For more information about actions, visit 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 information about how to use variables, visit: 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: # The layers bound to the function. The values are the ARNs of the layers. - acs:fc:cn-hangzhou:1034354682****:layers/numpy/versions/1
Create a
code
hierarchy in the target directory, and then prepare the function code in thecode
.Run the following command in the target directory to deploy the function and configure layers for the function:
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.