Cloud function SCF has built-in some commonly used dependency libraries in each runtime. You can go to each runtime code development to query: Node.js , Python , PHP . However, only the built-in dependency library is not enough to meet the user's code running requirements. On this basis, SCF provides a wealth of dependency installation methods. We will be divided into two parts to explain. This article will introduce the dependency installation method for Node.js and Python runtime, and the next part will introduce the dependency installation method for PHP, Java, and Go.
01. Node.js runtime
The Node.js runtime supports the following three dependent library installation methods:
1. The dependent library is packaged and uploaded with the code
Through dependency management tools, such as npm, install dependencies locally and upload them together with the function code.
- When packaging, the function entry file needs to be in the root directory of the
zip
If you package the entire folder and upload thezip
package, the function creation will fail because the entry file cannot be found in the root directory after decompression.
This article takes the installation of lodash
library as an example:
mkdir test-package
command in the local terminal to create a directory for storing function codes and dependent libraries.Execute the following command to install the
lodash
dependent library in this directory.cd test-package npm install lodash
Create a function entry file
index.js
in this directory and reference thelodash
library in the code.'use strict'; const _ = require('lodash'); exports.main_handler = async (event, context) => { console.log("Hello World") console.log(event) console.log(event["non-exist"]) console.log(context) return event };
Compress the function code and dependent libraries together into a zip package, cloud function console and create a new function. The operation steps are as follows:
i. Log in to Cloud Function Console , and click [Function Service] in the left navigation bar.
ii. At the top of the main interface, select the region where the function is to be created, and click [New] to enter the function creation process.
iii. On the "New Function" page, fill in the basic information of the function. As shown below:
- creation method : choose to use [custom creation] to create a new function.
- operating environment : select [Node.js12.16].
- submission method : select [local upload zip package].
- Click [Finish] to create the function.
2. Online dependency installation
The Node.js runtime provides the online dependency installation function. After the "Online Installation Dependency" is enabled, after each code upload, the cloud function backend will check the package.json
file in the root directory of the code package, and try to use it based on the dependencies in package.json
The npm tool installs dependent packages.
Take the installation of the lodash
library as an example:
- Log in to the cloud function console and click [Function Service] in the left navigation bar.
- Select [Function Service] in the left navigation bar, select the function that needs to be installed online on the "Function Service" list page or click [New] to create a function.
Select the [Function Code] tab, modify the function code according to your actual needs and add the
package.json
file.package.json
content of 06100d64b74f8d is the following as an example:{ "dependencies": { "lodash": "4.17.15" } }
] in the upper right corner of the IDE code editing window, and select [Automatically install dependencies: Close] in the drop-down list to enable automatic installation of dependencies, as shown in the following figure:
Click [Deploy], and the cloud function background will automatically install dependencies
package.json
? The online dependency installation function currently only supports the
Node.js
runtime, please stay tuned for other runtimes.
3. Use Serverless Web IDE
The cloud function online editor Serverless Web IDE provides terminal functions, and a package management tool npm
is built in the terminal. This article takes the installation of the lodash
library in the terminal as an example:
- Log in to the cloud function console and select [Function Service] on the left.
- In the function list, click the function name to enter the details page of the function.
- In the "Function Management" page, select [Function Code]> [Code Edit] to view and edit functions.
- Select [New Terminal] in the [Terminal] menu bar at the top of the IDE to open the terminal window.
Execute the following command in the terminal to install the dependent library
lodash
:cd src # 依赖库需要安装在与函数入口文件同一级的目录下,即需要进入`src`目录后再执行依赖安装操作。 npm install lodash
package.json
andnode_modules
in the file tree on the left side of the IDE.- After clicking [Deploy], the dependent library can be packaged and uploaded to the cloud together with the function code. As shown below:
02. Python runtime
The Python runtime supports the following two methods of installing dependent libraries:
1. The dependent library is packaged and uploaded together with the code
Through a dependency management tool, such as pip, install the dependency locally and upload it together with the function code.
- When packaging, the function entry file needs to be in the root directory of the
zip
If you package the entire folder and upload thezip
package, the function creation will fail because the entry file cannot be found in the root directory after decompression. - Due to different operating environments, you can replace
pip
pip3
orpip2
. - The function running system is CentOS 7, and you need to install it in the same environment. If the environment is inconsistent, it may cause an error that the dependency cannot be found when running after uploading. You can refer to cloud function container image for dependent installation or use online IDE to install.
- If part of the dependency involves a dynamic link library, you need to manually copy the relevant dependency package to the dependency installation directory before packaging and uploading it. For details, see Use Docker to install dependencies or use online IDE to install.
This article takes the installation of numpy
library as an example:
mkdir test-package
command in the local terminal to create a directory for storing function codes and dependent libraries.Execute the following command to install the
numpy
dependent library in this directory.cd test-package pip install numpy -t .
Create a function entry file
index.py
in this directory and reference thenumpy
library in the code.# -*- coding: utf8 -*- import json import numpy def main_handler(event, context): print("Received event: " + json.dumps(event, indent = 2)) print("Received context: " + str(context)) print("Hello world") return("Hello World")
Compress the function code and dependent libraries together into a zip package, cloud function console and create a new function. The operation steps are as follows:
i. Log in to the cloud function console and click [Function Service] in the left navigation bar.
ii. At the top of the main interface, select the region where the function is to be created, and click [New] to enter the function creation process.
iii. On the "New Function" page, fill in the basic information of the function. As shown below:
- creation method : choose to use [custom creation] to create a new function.
- operating environment : select [Python 3.6].
- submission method : select [local upload zip package].
- Click [Finish] to create the function.
2. Use Serverless Web IDE
The cloud function online editor Serverless Web IDE provides terminal functions, and has a built-in package management tool pip
in the terminal. This article takes the installation of the numpy
library in the terminal as an example:
- Log in to the cloud function console and select [Function Service] on the left.
- In the function list, click the function name to enter the details page of the function.
- In the "Function Management" page, select [Function Code]> [Code Edit] to view and edit functions.
- Select [New Terminal] in the [Terminal] menu bar at the top of the IDE to open the terminal window.
Execute the following command in the terminal to install the dependent library
numpy
:cd src # 依赖库需要安装在与函数入口文件同一级的目录下,即需要进入`src`目录后再执行依赖安装操作。 pip3 install numpy -t .
- After the installation is complete, view the installed dependent libraries in the file tree on the left side of the IDE.
- After clicking [Deploy], the dependent library can be packaged and uploaded to the cloud together with the function code.
- You can use
pip freeze > requirements.txt
generate all dependentrequirements.txt
files in the local environment.pip3 install -r requirements.txt -t .
in the IDE terminal to install the dependent package according torequirements.txt
One More Thing
Experience Tencent Cloud Serverless Demo now and receive the Serverless New User Gift Pack 👉 Tencent Cloud Serverless Newbie Experience .
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。