Help Center> FunctionGraph> FAQs> Dependency Management FAQs> How Do I Create Function Dependencies?
Updated on 2024-03-01 GMT+08:00

How Do I Create Function Dependencies?

You are advised to create function dependencies in EulerOS. If other OSs are used, an error may occur due to underlying dependent libraries. For example, the dynamic link library cannot be found.

If the modules to be installed need dependencies such as .dll, .so, and .a, archive them to a .zip package.

Setting Up the EulerOS Environment

EulerOS is an enterprise-grade Linux OS based on open-source technology. It features high security, scalability, and performance, meeting customers' requirements for IT infrastructure and cloud computing services. Huawei Cloud EulerOS is recommended.

  1. Buy a EulerOS ECS on Huawei Cloud by referring to Purchasing and Logging In to a Linux ECS. On the Configure Basic Settings page, select Public Image, and select Huawei Cloud EulerOS and an image version.
  2. Download the EulerOS image, and use virtualization software to set up the EulerOS VM on a local PC.

Creating a Dependency for a Python Function

Ensure that the Python version of the packaging environment is the same as that of the function. For Python 2.7, Python 2.7.12 or later is recommended. For Python 3.6, Python 3.6.3 or later is recommended.

To install the PyMySQL dependency for a Python 2.7 function in the local /tmp/pymysql directory, run the following command:

pip install PyMySQL --root /tmp/pymysql

After the command is successfully executed, go to the /tmp/pymysql directory:

cd /tmp/pymysql/

Go to the site-packages directory (generally, usr/lib64/python2.7/site-packages/) and then run the following command:

zip -rq pymysql.zip *

The required dependency is generated.

To install the local wheel installation package, run the following command:

pip install piexif-1.1.0b0-py2.py3-none-any.whl --root /tmp/piexif   
//Replace piexif-1.1.0b0-py2.py3-none-any.whl with the actual installation package name.

Creating a Dependency for a Node.js Function

Ensure that the corresponding Node.js version has been installed in the environment.

To install the MySQL dependency for a Node.js 8.10 function, run the following command:

npm install mysql --save

The node_modules folder is generated under the current directory.

  • Linux OS

    Run the following command to generate a ZIP package.

    zip -rq mysql-node8.10.zip node_modules

    The required dependency is generated.

  • Windows OS

    Compress node_modules into a ZIP file.

To install multiple dependencies, create a package.json file first. For example, enter the following content into the package.json file and then run the following command:

{
	"name": "test",
	"version": "1.0.0",
	"dependencies": {
		"redis": "~2.8.0",
		"mysql": "~2.17.1"
	}
}
npm install --save

Do not run the CNPM command to generate Node.js dependencies.

Compress node_modules into a ZIP package. This generates a dependency that contains both MySQL and Redis.

For other Node.js versions, you can create dependencies in the way stated above.

Creating a Dependency for a Java Function

When you compile a function using Java, dependencies need to be compiled locally. For details about how to add dependencies, see Developing Functions in Java (Using an IDEA Java Project).

Creating a Dependency for a PHP Function

EulerOS 2.9.6 is recommended.

By default, Composer and PHP 7.3 have been installed in the environment. Install Protobuf 3.19 using Composer.

Create the composer.json file with the following content:
{
    "require": {
        "google/protobuf": "^3.19"
    }
}
Run the following command:
Composer install

The vendor folder is generated with the autoload.php, composer, and google subfolders in the current directory.

  • Linux

    Run the following command to generate a ZIP package.

    zip –rq vendor.zip vendor
  • Windows

    Compress vendor into a ZIP file.

    If multiple dependencies need to be installed, specify them in the composer.json file, compress the vendor folder into a ZIP file and upload it.

To use third-party dependencies downloaded using Composer in PHP project code, load the dependencies through require "./vendor/autoload.php". By default, files decompressed from the uploaded ZIP package are placed in a directory at the same level as the project code.

Dependency Management FAQs FAQs

more