All Products
Search
Document Center

Install third-party dependencies

Last Updated: Aug 22, 2019

Third-party dependencies are often used for function execution. Usually, you can use some common built-in dependencies in the runtime environments of Function Compute.

For the built-in dependencies of each runtime environment, refer to:

To use a custom dependent module, you can package the dependent module and the function code together and upload the package to Function Compute.This topic describes how to install a third-party dependent module for a function.

Packaging requirements

When a function requires a third-party dependency, you must download the dependency to the folder where the function code is stored, package all dependencies and code together, and upload the entire package to Function Compute. Specify the code package when you create a function. You can use Fun or the Function Compute console to directly upload the package to Function Compute. Alternatively, you can upload the package to OSS and import the package to Function Compute from OSS.

During the upload, you can upload a zip package or specify the function folder. Then, Function Compute automatically zips the folder in the background.

Note that if you upload a zip package:

You must package all files, instead of only the code folders. Additionally, make sure that the handler function is in the root folder of the package.

  • In the Windows operating system, you can select all files in the function code folder, right-click these files, and compress them into a zip package.
  • In the Linux operating system, you can specify all files in the code folder as source files and run the zip command to generate a code package, such as zip code.zip /home/code/*.

Packaging example

Consider the following two points:

  • You must download the dependencies to the folder where the function is stored.
  • You must package all the files instead of only the code folders.

You can use the Function Compute console to upload a code package. On the code execution page, you can select Import from OSS, Upload Zip File, or Upload Folder. For the Windows operating system, we recommend that you use the Function Compute console to upload a package.

Use Fun to upload a code package.

Node.js packaging example

Package a third-party dependency mysql

The following steps describe how to use Fun to add a mysql module to access MySQL. For the installation guide of Fun, see here.

  1. Create a folder to store code and dependent modules:

    1. mkdir /tmp/code
  2. Install dependencies in the /tmp/code folder.

    1. cd /tmp/code
    2. npm install mysql
  3. Create a new code file, such as /tmp/code/index.js, and use the mysql module in the code:

    1. var mysql = require('mysql');
    2. exports.handler = function(event, context, callback) {
    3. var connection = mysql.createConnection({
    4. host : 'localhost',
    5. user : 'me',
    6. password : 'secret',
    7. database : 'my_db'
    8. });
    9. connection.connect();
    10. connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
    11. if (error) return callback(error);
    12. console.log('The solution is: ', results[0].solution);
    13. callback(null, results[0].solution);
    14. });
    15. connection.end();
    16. };
  4. Create a template.yml file, such as /tmp/code/template.yml, and enter the content as follows:

    1. ROSTemplateFormatVersion: '2015-09-01'
    2. Transform: 'Aliyun::Serverless-2018-04-03'
    3. Resources:
    4. FunDemo:
    5. Type: 'Aliyun::Serverless::Service'
    6. nodejsdemo:
    7. Type: 'Aliyun::Serverless::Function'
    8. Properties:
    9. Handler: index.handler
    10. Runtime: nodejs8
    11. CodeUri: './'

    The description of the template.yml file is as follows: Declare a FunDemo service and a nodejsdemo function for the FunDemo service. Then, configure the function handler as index.handler and specify the function runtime environment as nodejs8. In addition, specify the CodeUri property as the current folder. Fun packages and uploads the folder or files that the CodeUri property specifies during the deployment. For more configuration rules, see here.

    After the installation, the content of the /tmp/code folder is as follows:

    1. ls -l /tmp/code
    2. -rw-r--r-- 1 tan wheel 522B Jun 18 14:50 index.js
    3. drwxr-xr-x 13 tan wheel 416B Jun 18 14:49 node_modules
    4. -rw-r--r-- 1 tan wheel 297B Jun 18 14:58 template.yml
  5. Use Fun to deploy:

    1. fun deploy

    Related logs appear as follows when you successfully run the fun deploy command:

    1. using region: cn-hangzhou
    2. using accountId: ***********3557
    3. using accessKeyId: ***********r3Ra
    4. using timeout: 300
    5. Waiting for service FunDemo to be deployed...
    6. Waiting for function nodejsdemo to be deployed...
    7. Waiting for packaging function nodejs code...
    8. package function nodejs code done
    9. function nodejsdemo deploy success
    10. service FunDemo deploy success

    Log on to the console. You can see the related services and functions that are successfully created. The execution of functions can be triggered to return correct results.

    Here is a brief description about what the fun deploy command does: When you use the fun deploy command, it creates a FunDemo service according to the configuration of the template.yml file. In addition, it creates a nodejsdemo function with a runtime environment nodejs8. Then, the nodejsdemo function packages and uploads the current folder that the CodeUri property specifies to Function Compute as the function code. The packaged code contains the node_modules folder that includes all the installed packages, so Function Compute can directly use these packages.

Call executable files

The functions may use some tools that are not written in Node.js, such as shell scripts or executable files that are complied in C++ or Go. However, you can still package those tools and the code together and use them by running external commands in functions.

For example, if you run a shell script script.sh, you can package it into the code folder and run the process.env ['FC _ FUNC_CODE_PATH ']command to retrieve the path of the current code.

The following example demonstrates how to run a shell script:

  1. var exec = require('child_process');
  2. exports.handler = function (event, context, callback) {
  3. var scriptPath = process.env['FC_FUNC_CODE_PATH'] + '/script.sh';
  4. exec.exec('bash ' + scriptPath, {}, function (err, stdout, stderr) {
  5. if (err) return callback(err);
  6. console.log(stdout, stderr);
  7. callback(null, stdout);
  8. });
  9. };

Executable files that are compiled in C, C++, or Go must be compatible with the runtime environment of Function Compute. The Node.js runtime environment of Function Compute is:

  • Linux kernel version: Linux 4.4.24-2.al7.x86_64
  • Docker base image: Docker Pull Node:6.10

Python packing example

To use custom modules, you need to pack them and code files together. The following steps describe how to use Fun to add a PyMySQL module to access MySQL:

  1. Create a folder to store code and dependent modules:

    1. mkdir /tmp/code
  2. Create a new code file, such as /tmp/code/index.py , and use the pymysql module in the code:

    1. import pymysql.cursors
    2. # Connect to the database
    3. connection = pymysql.connect(host='localhost',
    4. user='user',
    5. password='passwd',
    6. db='db',
    7. charset='utf8mb4',
    8. cursorclass=pymysql.cursors.DictCursor)
    9. def handler(event, context):
    10. with connection.cursor() as cursor:
    11. # Read a single record
    12. sql = "SELECT count(*) FROM `users`"
    13. cursor.execute(sql)
    14. result = cursor.fetchone()
    15. print(result)
    16. return result
  3. Create a template.yml file, such as /tmp/code/template.yml, and enter the content as follows:

    1. ROSTemplateFormatVersion: '2015-09-01'
    2. Transform: 'Aliyun::Serverless-2018-04-03'
    3. Resources:
    4. FunDemo:
    5. Type: 'Aliyun::Serverless::Service'
    6. pythondemo:
    7. Type: 'Aliyun::Serverless::Function'
    8. Properties:
    9. Handler: index.handler
    10. Runtime: python3
    11. CodeUri: '. /'
  4. Install dependencies in the /tmp/code folder.

    Note that you cannot install the PyMySQL by using the pip install PyMySQL command. Otherwise, dependencies are scattered in many directories during the installation. You can download the dependent library files to the current folder so that you can zip them and upload the package.

    If you do not use a Docker environment or the dynamic library in .so format, or do not compile binary programs, to only install language dependencies, you can run the command pip install -t . PyMySQL to install. This way to install PyMySQL increases the size of the code package, because the Python libraries are downloaded regardless of whether they have been installed.

    If you use the Docker environment, we recommend that you use Fun to install third-party libraries, to debug locally, and to publish functions. Note that for the Windows operating system, use Docker for Windows. This prevents problems caused by inconsistencies between the development environment and the runtime environment. Particularly, when the function depends on binary and dynamic library files, you must use Fun to compile relevant dependencies.

    Using Fun to install PyMySQL is simple. You only need to run the command in the project folder. The following example demonstrates how to install PyMySQL in the /tmp/code folder:

    1. fun install --runtime python3 --package-type pip PyMySQL

    Notes for using Fun:

    1. Install Docker to run Fun commands. For more information about how to install Docker, see Related documents.
    2. Images that you require for running Fun are stored in Docker’s official repositories. Accessing these repositories can be slow. We recommend that you use Alibaba Cloud Container Registry. For more information, see Related documents.
    3. Only root users can run Docker in the Linux operating system. Therefore, run the sudo command to enable command line tools. Alternatively, you can manage Docker as a non-root user according to Related documents.

    After the installation, the content of the /tmp/code folder is as follows:

    1. $ ls -al /tmp/code
    2. drwxr-xr-x 3 tan wheel 96 Jun 18 17:08 .fun
    3. -rw-r--r-- 1 tan wheel 560 Jun 18 16:13 index.py

    The .fun folder is where the installed dependencies of PyMySQL are.

    If a problem occurs during the installation, you can specify the -v parameter to view more detailed logs:

    1. $ fun install -v --runtime python3 --package-type pip PyMySQL
    2. skip pulling image aliyunfc/runtime-python3.6:build-1.5.3...
    3. Task => [UNNAMED]
    4. => PYTHONUSERBASE=/code/.fun/python pip install --user PyMySQL
    5. Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
    6. Collecting PyMySQL
    7. Downloading http://mirrors.aliyun.com/pypi/packages/ed/39/15045ae46f2a123019aa968dfcba0396c161c20f855f11dea6796bcaae95/PyMySQL-0.9.3-py2.py3-none-any.whl (47kB)
    8. Installing collected packages: PyMySQL
    9. Successfully installed PyMySQL-0.9.3

    For more information about the fun install command, see this topic.

  5. Use Fun to deploy

    1. fun deploy

    Note: You can use the fun install command to install the dynamic library in .so format. The following example demonstrates how to use the fun install command to install the libzbar0 file:

    1. fun install --runtime python3 --package-type apt libzbar0

    The dependencies are installed into the .fun folder after you run the command. You can use Fun to debug and deploy locally.

    Note: Because dependencies are installed in the .fun folder, you must make sure that the CodeUri property in the template.yml file includes this folder, and it must be a top-level subfolder.

Run external commands

The functions may use some tools that are not written in Python, such as shell scripts or executable files that are complied in C++ or Go. However, you can still package those tools and the code together and use them by running external commands in functions.

For example, if you run a script.sh shell script, you can package it into the code folder and run the process.env ['FC _ FUNC_CODE_PATH '] command to get the path of the current folder.

The following example demonstrates how to run a shell script:

  1. import os
  2. import subprocess
  3. def my_handler(event, context):
  4. script_path = os.environ.get('FC_FUNC_CODE_PATH') + '/script.sh'
  5. ret = subprocess.check_output(['bash', script_path])
  6. return ret

Executable files that are compiled in C, C++, or Go must be compatible with the runtime environment of Function Compute. The following Python runtime environments are supported in Function Compute:

  • Linux kernel version: Linux 4.4.24-2.al7.x86_64
  • Docker base image: Docker Pull Python:2.7; Docker Pull Python:3.6

PHP packaging example

The following steps demonstrate how to use Fun to install a Humble HTTP request library package request:

  1. Create a folder to store code and dependent modules:

    1. mkdir /tmp/code
  2. Create a code file, such as /tmp/code/index.php. Then, use the custom package requests in the code.

    1. <?php
    2. require_once __DIR__ . "/vendor/autoload.php";
    3. function handler($event, $context){
    4. $headers = array('Accept' => 'application/json');
    5. $options = array('auth' => array('user', 'pass'));
    6. $request = Requests::get('https://www.baidu.com', $headers, $options);
    7. var_dump($request->status_code);
    8. // int(200)
    9. var_dump($request->headers['content-type']);
    10. // string(31) "application/json; charset=utf-8"
    11. var_dump($request->body);
    12. // string(26891) "[...]"
    13. }
  3. Install dependencies in the /tmp/code folder.

    • Create a file named composer.json

      1. {
      2. "require": {
      3. "rmccue/requests": ">=1.0"
      4. }
      5. }
    • Run the composer install --no-dev command to install dependencies.

      1. cd /tmp/code
      2. composer install --no-dev
    • After the installation, the content of the /tmp/code folder is as follows:

      1. ls -l /tmp/code
      2. -rw-r--r-- 1 rsong staff 72 8 22 09:54 composer.json
      3. -rw-r--r-- 1 rsong staff 2165 8 22 09:54 composer.lock
      4. -rw-r--r-- 1 rsong staff 523 8 22 09:54 index.php
      5. drwxr-xr-x 5 rsong staff 160 8 22 09:54 vendor
  4. Create a template.yml file and publish it.

    • Create a template.yml file in the /tmp/code folder as follows.

      1. ROSTemplateFormatVersion: '2015-09-01'
      2. Transform: 'Aliyun::Serverless-2018-04-03'
      3. Resources:
      4. FunDemo:
      5. Type: 'Aliyun::Serverless::Service'
      6. phpdemo:
      7. Type: 'Aliyun::Serverless::Function'
      8. Properties:
      9. Handler: index.handler
      10. Runtime: php7.2
      11. CodeUri: './'

      The description of the template.yml file is as follows: Declare a FunDemo service and a phpdemo function for the FunDemo service. Then, configure the function handler as index.handler and specify the function runtime environment as nodejs8. In addition, specify the CodeUri property as the current folder. Fun packages and uploads the folder or files that the CodeUri property specifies during the deployment. For more configuration rules, see here.

    • Run the fun deploy command for the release.

      1. fun deploy

      Related logs appear as follows after the release:

      1. using region: cn-hangzhou
      2. using accountId: ***********3557
      3. using accessKeyId: ***********r3Ra
      4. using timeout: 300
      5. Waiting for service FunDemo to be deployed...
      6. Waiting for function phpdemo to be deployed...
      7. Waiting for packaging function phpdemo code...
      8. package function phpdemo code done
      9. function phpdemo deploy success
      10. service FunDemo deploy success

    Log on to the console. You can see the related services and functions that are successfully created. The execution of functions can be triggered to return correct results.

    Here is a brief description about what the fun deploy command does: When you use the fun deploy command, it creates a FunDemo service according to the configuration of the template.yml file. In addition, it creates a phpdemo function with a runtime environment php7.2. Then, the phpdemo function packages and uploads the current folder that the CodeUri property specifies to Function Compute as the function code. The packaged code contains the vendor folder that includes all the installed packages, so Function Compute can directly use these packages.

    To create a custom package, see Use custom packages for PHP Runtime

Java packaging example

When you compile code in Java, you must zip the code and dependencies into a JAR package and upload the JAR package to Function Compute. When you use the package of dependencies, write the pom.xml file and zip it into the JAR package. You can use Maven or IDEA for the Java packaging.

Use Maven to package code

For example, use OSS Java SDKand add OSS Java SDK in the pom.xml file.

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.aliyun.fc.runtime</groupId>
  4. <artifactId>fc-java-core</artifactId>
  5. <version>1.0.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.aliyun.oss</groupId>
  9. <artifactId>aliyun-sdk-oss</artifactId>
  10. <version>2.6.1</version>
  11. </dependency>
  12. </dependencies>

Add the maven-assembly-plugin plug-in to the pom.xml file to zip dependencies into the JAR package.

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <artifactId>maven-assembly-plugin</artifactId>
  5. <version>3.1.0</version>
  6. <configuration>
  7. <descriptorRefs>
  8. <descriptorRef>jar-with-dependencies</descriptorRef>
  9. </descriptorRefs>
  10. <appendAssemblyId>false</appendAssemblyId> <!-- this is used for not append id to the jar name -->
  11. </configuration>
  12. <executions>
  13. <execution>
  14. <id>make-assembly</id> <!-- this is used for inheritance merges -->
  15. <phase>package</phase> <!-- bind to the packaging phase -->
  16. <goals>
  17. <goal>single</goal>
  18. </goals>
  19. </execution>
  20. </executions>
  21. </plugin>
  22. <plugin>
  23. <groupId>org.apache.maven.plugins</groupId>
  24. <artifactId>maven-compiler-plugin</artifactId>
  25. <configuration>
  26. <source>1.8</source>
  27. <target>1.8</target>
  28. </configuration>
  29. </plugin>
  30. </plugins>
  31. </build>

Use IDEA to package code

  • Project Struction > Artifacts > Add Jar > From Modules with Dependencies > Apply > OK
  • Build > Build Artifacts > Build
  • Refer to the IDEA official document Packaging a Module into a JAR File

(https://pitcures.oss-cn-hangzhou.aliyuncs.com/BlogPictures/%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97/%E5%87%BD%E6%95%B0%E6%89%93%E5%8C%85/java1.png)(https://pitcures.oss-cn-hangzhou.aliyuncs.com/BlogPictures/%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97/%E5%87%BD%E6%95%B0%E6%89%93%E5%8C%85/java2.png)(https://pitcures.oss-cn-hangzhou.aliyuncs.com/BlogPictures/%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97/%E5%87%BD%E6%95%B0%E6%89%93%E5%8C%85/java3.png)

More information