Dependency Installation

Last updated: 2021-08-13 10:53:23

    Built-In Dependencies

    Some common dependency libraries have been built in various runtimes of SCF, which can be queried in the corresponding runtime code development guide:

    Installing Dependency Libraries

    You can save all the dependency 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 the following three dependency library installation methods:

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

    Note

    When you package them, the function entry file needs to be put in the root directory of the zip package. If you package the entire folder as the zip package and upload it, function creation will fail because the entry file cannot be found in the root directory after decompression.


    This document takes installing the lodash library as an example:

    1. Run the mkdir test-package command in the local terminal to create a directory for storing the 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 import the lodash 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
      };
      
    4. Compress the function code and dependency libraries into a zip package, upload the package in the SCF console, and create a function in the following steps:
      1. Log in to the SCF console and click Function Service on the left sidebar.
      2. Select the region where to create a function at the top of the page and click Create to enter the function creation process.
      3. Enter the basic information of the function on the Create Function page as shown below:
        • Creation Method: select Custom Creation.
        • Runtime Environment: select Node.js 12.16.
        • Submitting Method: select Local ZIP file.
      4. Click Complete.

    Python runtime

    The Python runtime supports the following two dependency library installation methods:

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

    Note
    • When you package them, the function entry file needs to be put in the root directory of the zip package. If you package the entire folder as the zip package and upload it, function creation will fail because the entry file cannot be found in the root directory after decompression.
    • For different runtime environment configurations, you can replace pip with pip3 or pip2 by yourself.
    • 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. You can install dependencies as instructed in SCF Container Image or by using the online IDE.
    • If some dependencies involve a dynamic link library, you need to manually copy the relevant dependency package to the dependency installation directory before packaging and uploading them. For more information, please see Installing Dependency with Docker. You can also use the online IDE for installation.

    This document takes installing the numpy library as an example:

    1. Run the mkdir test-package command in the local terminal to create a directory for storing the function code and dependency libraries.
    2. Run the following command to install the numpy dependency library in this directory.
      cd test-package
      pip install numpy -t .
      
    3. Create the function entry file index.py in this directory and import the numpy 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")
      
    4. Compress the function code and dependency libraries into a zip package, upload the package in the SCF console, and create a function in the following steps:
      1. Log in to the SCF console and click Function Service on the left sidebar.
      2. Select the region where to create a function at the top of the page and click Create to enter the function creation process.
      3. Enter the basic information of the function on the Create Function page as shown below:
        • Creation Method: select Custom Creation.
        • Runtime Environment: select Python 3.6.
        • Submitting Method: select Local ZIP file.
      4. Click Complete.

    PHP runtime

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

    Note

    When you package them, the function entry file needs to be put in the root directory of the zip package. If you package the entire folder as the zip package and upload it, function creation will fail because the entry file cannot be found in the root directory after decompression.


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

    1. Run the mkdir test-package command in the local terminal to create a directory for storing the function code and dependency libraries.
    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 dependency libraries into a zip package, upload the package in the SCF console, and create a function in the following steps:
      1. Log in to the SCF console and click Function Service on the left sidebar.
      2. Select the region where to create a function at the top of the page and click Create to enter the function creation process.
      3. Enter the basic information of the function on the Create Function page as shown below:
        • Creation Method: select Custom Creation.
        • Runtime Environment: select PHP 7.
        • Submitting Method: select Local ZIP file.
    6. Click Complete.

    Java runtime

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

    1. Run the mkdir test-package command in the local terminal to create a directory for storing the function code and dependency libraries.
    2. Create pom.xml in this directory and configure the dependency information in pom.xml.
    3. Run the mvn package command in the root directory of the project folder, and the compilation output is 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] ------------------------------------------------------------------------
      
    1. Compress the function code and dependency libraries into a JAR package, upload the package in the SCF console, and create a function in the following steps:
      1. Log in to the SCF console and click Function Service on the left sidebar.
      2. Select the region where to create a function at the top of the page and click Create to enter the function creation process.
      3. Enter the basic information of the function on the Create Function page as shown below:
        • Creation Method: select Custom Creation.
        • Runtime Environment: select Java 8.
        • Submitting Method: select Local ZIP file.
    2. Click Complete.

    Go runtime

    Instructions: upload the final binary file when packaging.

    Compile the dependency libraries of the Go runtime with the code to get a binary file, upload the packaged binary file in the SCF console, and create a function in the following steps:

    1. Log in to the SCF console and click Function Service on the left sidebar.
    2. Select the region where to create a function at the top of the page and click Create to enter the function creation process.
    3. Enter the basic information of the function on the Create Function page as shown below:
      • Creation Method: select Custom Creation.
      • Runtime Environment: select Go 1.
      • Submitting Method: select Local ZIP file.
    4. Click Complete.