Function Compute does not provide all dependent libraries in each runtime environment. As a result, you may fail to invoke a function after you deploy a project by using the FC component. For example, a Python function that you define needs to access the MySQL dependent library. If the MySQL dependent library is not provided in the Python runtime environment, the function fails to be executed when you invoke the function. To prevent such a failure, you must install the MySQL dependent library before you deploy a project. This topic describes how to use the FC component to install dependencies in various runtime environments.
Prerequisites
Procedure
- requirements.txt files of pip for Python
- package.json files of npm for Node.js
- pom.xml files of Apache Maven for Java
- composer.json files of Composer for PHP
- Dockerfiles of container images
codeUri
parameter in the s.yaml file.
The following example shows how to install dependencies in Python 3:
- Develop and edit source code, as marked by ① in the following figure.
- Run the following command in the project directory to install dependencies:
s build --use-docker
After you run the preceding command, Serverless Devs downloads the dependencies to the .s directory based on the content of the requirements.txt file, as marked by ② in the following figure.
- Run the following command to deploy the project:
s deploy
After you run the preceding command, Serverless Devs creates a function based on the deliverable content in the .s directory and configures the environment variable of the dependent library, as marked by ③ in the following figure. This allows the function to directly introduce the corresponding code dependent library. In the command output, you can view the added environment variable, as shown in the following figure.
The following example shows how to install dependencies in Node.js 10:
- Develop and edit source code, as marked by ① in the following figure.
- Run the following command in the project directory to install dependencies:
s build --use-docker
After you run the preceding command, Serverless Devs downloads the dependencies to the .s directory based on the content of the package.json file, as marked by ② in the following figure.
- Run the following command to deploy the project:
s deploy
After you run the preceding command, Serverless Devs creates a function based on the deliverable content in the .s directory and configures the environment variable of the dependent library, as marked by ③ in the following figure. This allows the function to directly introduce the corresponding code dependent library. In the command output, you can view the added environment variable, as shown in the following figure.
The following example shows how to install dependencies in Java 8:
- Develop and edit source code, as marked by ① in the following figure.
- Run the following command in the project directory to compile the Java project and
install dependencies:
s build --use-docker
After you run the preceding command, Serverless Devs downloads the dependencies to the .s directory based on the content of the pom.xml file, as marked by ② in the following figure.
- Run the following command to deploy the project:
s deploy
After you run the preceding command, Serverless Devs creates a function based on the deliverable content in the .s directory and configures the environment variable of the dependent library. This allows the function to directly introduce the corresponding code dependent library. The command output is shown in the following figure.
The following example shows how to install dependencies in PHP 7.2:
- Develop and edit source code, as marked by ① in the following figure.
- Run the following command in the project directory to install dependencies:
s build --use-docker
After you run the preceding command, Serverless Devs downloads the dependencies to the .s directory based on the content of the composer.json file, as marked by ② in the following figure.
- Run the following command to deploy the project:
s deploy
After you run the preceding command, Serverless Devs creates a function based on the deliverable content in the .s directory and configures the environment variable of the dependent library. This allows the function to directly introduce the corresponding code dependent library. In the command output, you can view the added environment variable, as shown in the following figure.
The following example shows how to install dependencies in a custom container:
- Develop and edit source code, and replace the
image
in the s.yml file with your image in Container Registry. - Run the following command in the project directory to install dependencies:
s build --use-docker --dockerfile ./Dockerfile
After you run the preceding command, Serverless Devs downloads the dependencies to the .s directory based on the content of the Dockerfile file.
- Run the following command to deploy the project:
s deploy -y
Additional information
codeUri
parameter in the s.yaml file.