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 the layer dependencies are stored to the dependency package search path for the runtime language. This topic describes how to reference dependencies in custom runtimes of different languages.
Examples
In this topic, the directories where the layers are located, such as /opt/python, are the recommended directories. By default, the layers are deployed to these directories if you build a layer ZIP package based on the steps described in Create a custom layer. Otherwise, confirm the directory structure of your layer ZIP package first and replace /opt/python with the actual directory.
For example, if the directory in your layer ZIP package is my-layer-code, the layer is deployed to the /opt/my-layer-code directory. You need to use the /opt/my-layer-code directory when you set environment variables in the function configurations or when you add a dependency library search path to the code.
Reference dependencies of a layer in a custom runtime - Python
Method 1: Set the
PYTHONPATH
environment variable in the function configurations and add the directory where the layer is located.Sample code:
PYTHONPATH=/opt/python
Method 2: Add the following statement to the entry file of your project. The statement 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 - Node.js
Set the NODE_PATH
environment variable in the function configurations 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 - Java
Method 1: Configure the
-classpath
parameter in the startup command and add the /opt/java/lib/* directory, where the layer is located.java -Dserver.port=9000 -classpath /code/:/opt/java/lib/* com.example.demo.DemoApplication
Method 2: Set the
CLASSPATH
environment variable in the function configurations and add the directory where the layer dependency is located.CLASSPATH=/code/:/opt/java/lib/*
If you use the CLASSPATH
environment variable, you cannot run programs by specifying JAR packages by using the -jar
parameter. For example, in the java -classpath ${CLASSPATH} -jar yourJarExe.jar
example, the -jar
parameter is used. Java JVM uses the MANIFEST.MF in the JAR package. All the search paths specified by environment variables and command lines are ignored. CLASSPATH
does not take effect.
Reference dependencies of a layer in a custom runtime - PHP
Add the following statement to the entry file of your project. The statement 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);