All Products
Search
Document Center

Function Compute:How do I reference dependencies in a layer in a custom runtime?

Last Updated:Feb 02, 2024

In a custom runtime, multiple languages of different versions are supported, such as Python, Node.js, Java, and PHP. The layer packaging method of these languages is the same as that of a native runtime. However, in a custom runtime, you must add the directory in which layer dependencies are stored to the search path of dependency packages for the runtime language. This topic describes how to reference dependencies in custom runtimes of different languages.

Example

In this example, we recommend that you use the /opt/python directory in which a layer is located. If you have followed the steps in the Create a custom layer topic to create a layer, its .zip file of the layer is deployed to this directory by default. If you do not want to use the example directory, confirm the directory of the .zip file of your desired layer and replace /opt/python with the actual directory.

For example, if the .zip file is stored in my-layer-code, the layer is deployed to the /opt/my-layer-code directory. The /opt/my-layer-code directory is required when you configure environment variables for a function or when you add a search path of dependency libraries to code.

Reference dependencies of a layer in a custom runtime for Python

  • Method 1: Configure the PYTHONPATH environment variable for a function and add the directory in which the layer is located.

    Sample code:

    PYTHONPATH=/opt/python
  • Method 2: Add the following statements to the handler file of your project. The statements must be executed before the dependency libraries of the layer are imported.

    import sys
    sys.path.append('/opt/python')
    # import {PackageFromLayer}

For more information, see the sample python-demo-with-lib-in-layer.

Reference dependencies of a layer in a custom runtime for Node.js

You can configure the NODE_PATH environment variable for a function and add the directory where the layer is located. For more information, see the sample nodejs-demo-with-lib-in-layer.

NODE_PATH=/opt/nodejs/node_modules

Reference dependencies of a layer in a custom runtime for Java

  • Method 1: Configure the -classpath parameter in the startup command and add the /opt/java/lib/* directory in which the layer is located.

    java -Dserver.port=9000 -classpath /code/:/opt/java/lib/* com.example.demo.DemoApplication
  • Method 2: Configure the CLASSPATH environment variable for a function and add the directory in which the layer dependency is located.

    CLASSPATH=/code/:/opt/java/lib/*
Important

If you use the CLASSPATH environment variable, you cannot run programs by specifying .jar files by using the -jar parameter. For example, the -jar parameter in java -classpath ${CLASSPATH} -jar yourJarExe.jar indicates that Java JVM uses MANIFEST.MF and the specified search path of all environment variables and command lines are ignored. In this case, the value of the CLASSPATH parameter does not take effect.

Reference dependencies of a layer in a custom runtime for PHP

You can add the following statements to the handler file of your project. The statements must be executed before the dependency libraries of the layer are imported.

<?php
$path = '/opt/php';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);