Each SCF runtime has built-in parts of the common dependency libraries, which you can query in each runtime code development: Node.js, Python, PHP. However, the built-in dependency library alone is not enough to satisfy the user’s code running demands. On this basis, SCF provides rich dependency installation methods. This article introduces how to install dependencies on Node.js and Python runtimes. The next part introduces how to install dependencies on PHP, Java, and Go.

01.node.js runtime

The Node.js runtime supports the following three dependency installation methods:

1. Package and upload the dependent library with the code

Use a dependency management tool, such as NPM, to install the dependencies locally and then package and upload them with the function code.

  • The function entry file needs to be inzipPackage root directory. If you package the entire folder and upload itzipPackage, the function cannot be created because the entry file cannot be found in the root directory after decompression.

This article takes installing the Lodash library as an example:

  1. Execute on the local terminalmkdir test-packageCommand to create a directory for storing function code and dependency libraries.
  2. Run the following command to install the Lodash dependency library in this directory.

    cd test-package
    npm install lodash
  3. Create the function entry file index.js in this directory and reference the Lodash library in your 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
    };
  4. Compress the function code and dependent libraries together into a ZIP package, upload the packaged ZIP package in the Cloud Functions console and create a new function. The operation steps are as follows:

    I. Log in to the Cloud Function console and click Function Services on the left navigation bar.

    Ii. In the upper part of the main window, select the area where you want to create a function and click Add to enter the function creation process.

    Iii. On the New Function page, enter basic function information. As shown in the figure below:

    • How to create: Select custom create to create a function.
    • Runtime environment: Select [Node.js12.16].
    • Submission method: Select Locally Upload ZIP package.
  5. Click finish to create the function.

The node.js runtime provides the online dependency installation function. After “online dependency installation” is enabled, the cloud function background will check the package.json file in the root directory of the code package after each upload, and according to the dependencies in package.json, Try using the NPM tool to install dependency packages.

Take installing the Lodash library as an example:

  1. Log in to the Cloud Function console and click function Services on the left navigation bar.
  2. In the navigation tree on the left, choose Function Services. On the Function Services page, select the functions that you want to install online or click New to create a function.
  3. Select the [Function Code] TAB, modify the function code according to your actual needs and add package.json file. The following is an example of package.json content:

    {"dependencies": {"lodash": "4.17.15"}}
  4. In the upper right corner of the IDE code editing window, click [] and select [Auto-install Dependencies: Off] from the drop-down list to enable auto-install dependencies, as shown below:
  5. Click [Deploy] and the cloud function will automatically install dependencies based on package.json.

    ? Only the online dependency installation function is supported
    Node.jsRuntime, other runtimes stay tuned.

3. Use the Serverless Web IDE

The cloud function online editor Serverless Web IDE provides terminal functionality with the package management tool NPM built into the terminal. This article takes the example of installing the Lodash library in a terminal:

  1. Log in to the Cloud Function console and select “Function Services” on the left.
  2. In the function list, click a function name to go to the details page of the function.
  3. In the “Function Management” page, choose “Function Code” > “Code Edit”, view and edit the function.
  4. Select New Terminal from terminal on the menu bar at the top of IDE to open the terminal window.
  5. Run the following command on the terminal to install the lodash dependency library:

    CD SRC # Install the dependency library in the same directory as the function entry file, that is, you need to go to the 'SRC' directory before executing the dependency installation operation. npm install lodash
  6. When the installation is complete, view it in the file tree to the left of the IDEpackage.jsonnode_modules.
  7. Click [Deploy] and the dependent library can be packaged with function code and uploaded to the cloud. As shown in the figure below:

02. The Python runtime

The Python runtime supports the following two methods for installing dependency libraries:

Use a dependency management tool, such as PIP, to install the dependencies locally and upload them with the function code.

  • The function entry file needs to be inzipPackage root directory. If you package the entire folder and upload itzipPackage, the function cannot be created because the entry file cannot be found in the root directory after decompression.
  • Because the running environment is different, can bepipReplace withpip3pip2.
  • The function runs on CentOS 7. You need to install the function in the same environment. If the environment is inconsistent, it may cause an error that the dependency cannot be found when the upload is run. You can refer to the Cloud Function container image for dependency installation or use an online IDE for installation.
  • If some dependencies involve dynamic link libraries, manually copy related dependency packages to the dependency installation directory and then upload the packages. For details, see Installing dependencies with Docker or installing with an online IDE.

This article uses the numpy library as an example:

  1. Execute on the local terminalmkdir test-packageCommand to create a directory for storing function code and dependency libraries.
  2. Run the following command to install the numpy dependency library in the directory.

    cd test-package
    pip install numpy -t .
  3. Create the function entry file index.py in this directory and reference the Numpy library in your 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")
  4. Compress the function code and dependent libraries together into a ZIP package, upload the packaged ZIP package in the Cloud Functions console and create a new function. The operation steps are as follows:

    I. Log in to the Cloud Function console and click Function Services on the left navigation bar.

    Ii. In the upper part of the main window, select the area where you want to create a function and click Add to enter the function creation process.

    Iii. On the New Function page, enter basic function information. As shown in the figure below:

    • How to create: Select custom create to create a function.
    • Runtime environment: Select Python 3.6.
    • Submission method: Select Locally Upload ZIP package.
  5. Click finish to create the function.

The online editor Serverless Web IDE provides terminal functions and a package management tool PIP is built into the terminal. This article takes the example of installing the Numpy library on a terminal:

  1. Log in to the Cloud Function console and select “Function Services” on the left.
  2. In the function list, click a function name to go to the details page of the function.
  3. In the “Function Management” page, choose “Function Code” > “Code Edit”, view and edit the function.
  4. Select New Terminal from terminal on the menu bar at the top of IDE to open the terminal window.
  5. Run the following command on the terminal to install the dependency library numpy:

    CD SRC # Install the dependency library in the same directory as the function entry file, that is, you need to go to the 'SRC' directory before executing the dependency installation operation. pip3 install numpy -t .
  6. When the installation is complete, view the installed dependency libraries in the file tree to the left of the IDE.
  7. Click [Deploy] and the dependent library can be packaged with function code and uploaded to the cloud.
  • You can usepip freeze > requirements.txtGenerate all dependencies in the local environmentrequirements.txtFile.
  • Execute in the terminal of the IDEpip3 install -r requirements.txt -t .Can be based onrequirements.txtInstall dependency packages.

One More Thing

Experience Tencent Cloud Serverless Demo and receive the New User package 👉 Tencent Cloud Serverless Novice Experience.