tencent cloud

Feedback

Dependency Installation

Last updated: 2023-03-14 17:32:13

    Built-in Dependencies

    SCF runtime environments have provided some dependent libraries, which can be queried in the corresponding runtime development guide:

    Installing Dependent Libraries

    You can save the dependent libraries of the SCF code in the code package and upload it to the cloud for use by SCF. SCF supports the following runtimes and usage methods:

    Node.js runtime

    The Node.js runtime supports installing dependent libraries in the following three ways:

    You can use a dependency manager (such as npm) to locally install the dependent library, package and upload it with the function code.

    Note

    Place the entry point file of the function in the root directory of the zip package. If you package and upload the entire folder as a zip package, the function creation will fail because the entry point file cannot be found in the unzipped root directory.


    This document takes installing the lodash library as an example.

    1. Run the mkdir test-package command on your local computer to create a directory to store the function code and the dependent library.
    2. Run the following commands to install the lodash dependent library in the directory.

    cd test-package
    npm install lodash
    

    1. Create the index.js entry point file of the function in the directory and import the lodash library in codes.

    '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
    };
    

    1. Compress the function code and dependent library to a zip package. Upload the zip package and create a function via the SCF console.
      1. Log in to the SCF console and click Functions on the left sidebar.
      2. Choose a region at the top of the Functions page and click Create.
      3. Enter the basic information of the function on the Create page.
        • Creation method: Select From scratch.
        • Runtime environment: Choose Node.js12.16.
        • Submitting method: Choose Local ZIP file.
      4. Click Complete.

    Python runtime

    The Python runtime supports installing dependent libraries in the following two ways.

    You can use a dependency manager (such as pip) to locally install the dependent library, package and upload it with the function code.

    Note
    • Place the function’s entry point file in the root directory of the zip package. If you package and upload the entire folder as a zip package, the function creation will fail because the entry point file cannot be found in the unzipped root directory.
    • Due to runtime environment differences, confirm that the installed dependency version is adapted to the function runtime environment.
    • The function runtime environment is CentOS 7, and you need to install the dependencies in the same environment. If not, an error where the dependencies cannot be found may occur while running the function after upload.
    • For dependencies requiring dynamic link library, you need to manually copy these dependencies to the installation directory, and then compress and upload them. You can install dependency with Docker or online IDE.

    This document takes installing the numpy library as an example:

    1. Run the mkdir test-package command on your local computer to create a directory to store the function code and the dependent library.
    2. Run the following commands to install the numpy dependent library in the directory.
      cd test-package
      pip install numpy -t . # Check whether the pip version you are using is adapted to the function runtime environment
      
    3. Create the index.py entry point file of the function in the directory and import the numpy library in codes.
      # -*- 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 library to a zip package. Upload the zip package and create a function via the SCF console.
      1. Log in to the SCF console and click Functions on the left sidebar.
      2. Choose a region at the top of the Functions page and click Create.
      3. Enter the basic information of the function on the Create page.
        • Creation method: Select From scratch.
        • Runtime environment: Select Python 3.6.
        • Submitting method: Choose Local ZIP file.
      4. Click Complete.

    PHP runtime

    Note:

    The PHP versions supported by SCF are 5.6, 7.2, 7.4 and 8.0. Different minor versions of PHP may be incompatible. Please check the version number first before installing dependencies.

    You can use a dependency manager (such as “composer”) to locally install the dependent library, package and upload it with the function code.

    Note

    Place the entry point file of the function in the root directory of the zip package. If you package and upload the entire folder as a zip package, the function creation will fail because the entry point file cannot be found in the unzipped root directory.


    This document takes installing the requests library for PHP 7.2 as an example:

    1. Run the mkdir test-package command on your local computer to create a directory to store the function code and the dependent library.

    2. Create Composer.json under test-package and specify the dependency library and version to be installed.

      {
      "require": {
       "requests": ">=1.0"
          }
      }
      

    3. Run the following command to install the requests dependency library in this directory.

      cd test-package
      composer install
      

    4. Create the function entry file index.php in this directory and import the requests library in the code.

      <?php
      require 'vendor/autoload.php';
      function main_handler($event, $context) {
          return "hello world";
      }
      ?> 
      

    5. Compress the function code and dependent library to a zip package. Upload the zip package and create a function via the SCF console.

      1. Log in to the SCF console and click Functions on the left sidebar.
      2. Choose a region at the top of the Functions page and click Create.
      3. Enter the basic information of the function on the Create page.
      • Creation method: Select From scratch.
      • Runtime environment: Select Php7.2.
      • Submitting method: Choose Local ZIP file.
      1. Click Complete.

    Java runtime

    You can use a dependency manager (such as maven) to locally install the dependent library, package and upload it with the function code.

    1. Run the mkdir test-package command on your local computer to create a directory to store the function code and the dependent library.

    2. Create a `pom.xml file in the directory and configure dependency in the file.

    3. Run the mvn package command in the root directory of the project folder, and the compilation output should be as follows:

      [INFO] Scanning for projects...
      [INFO]
      [INFO] ------------------------------------------------------------------------
      [INFO] Building java-example 1.0-SNAPSHOT
      [INFO] ------------------------------------------------------------------------
      [INFO]
      ...
      [INFO] ------------------------------------------------------------------------
      [INFO] BUILD SUCCESS
      [INFO] ------------------------------------------------------------------------
      [INFO] Total time: 1.785 s
      [INFO] Finished at: 2017-08-25T10:53:54+08:00
      [INFO] Final Memory: 17M/214M
      [INFO] ------------------------------------------------------------------------
      
    4. Compress the function code and dependent library to a jar package. Upload the jar package and create a function via the SCF console.

      1. Log in to the SCF console and click Functions on the left sidebar.

      2. Choose a region at the top of the Functions page and click Create.

      3. Enter the basic information of the function on the Create page.

        • Creation method: Select From scratch.
        • Runtime environment: Choose Java8.
        • Submitting method: Choose Local ZIP file.
      4. Click Complete.

    Go runtime

    Instructions: upload the final binary file when packaging.

    Compile the dependent library of the Go runtime with codes to generate a binary file. Upload the binary file and create a function via the SCF console.

    1. Log in to the SCF console and click Functions on the left sidebar.
    2. Choose a region at the top of the Functions page and click Create.
    3. Enter the basic information of the function on the Create page.
      • Creation method: Select From scratch.
      • Runtime environment: select Go 1.
      • Submitting method: Choose Local ZIP file.
    4. Click Complete.
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support