01.php runtime

1. Install the custom library

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

  • Use a dependency management tool, such as Composer, to install 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.

This article uses the PHP7 installation requests library as an example:

  1. Run the mkdir test-package command on the local terminal to create a directory for storing function codes and dependency libraries.

  2. Create conposer.json under test-package and specify the dependencies and versions to be installed.

    {"require": {"requests": ">=1.0"}}Copy the code
  3. Install the requests dependency library in this directory by executing the following command.

    cd test-package
    composer install
    Copy the code
  4. Create the function entry file index. PHP in this directory and reference the requests library in your code.

    <? php require 'vendor/autoload.php'; function main_handler($event, $context) { return "hello world"; }? >Copy the code
  5. 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.

    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 Php7.
    • Submission method: Select Locally upload ZIP package.
  6. Click Finish to create the function.

2. Install custom extensions

Create the extension folder php_Extension in the same directory as the function entry file and add the custom extension file.so and the configuration file php.ini to the package and upload with the function code.

This article uses PHP7 as an example to install the custom extension Swoole.so.

  1. Run the mkdir test-package command on the local terminal to create a directory for storing function codes and dependency libraries.

  2. Run the following command to create a folder php_extension in test-package and place the configuration file php.ini and the extension file.so in this directory. The directory structure is as follows:

    • Extension folderphp_extensionAnd configuration filesphp.iniFor fixed names, using other names may cause the extension to fail to load.
    • Extension folderphp_extensionAnd configuration filesphp.iniAnd custom extensions.soThe file must have executable permission.
|____php_extension
| |____php.ini
| |____swoole.so
|____index.php  
Copy the code
  1. Custom extensions can be loaded from code or layers. If the extension is uploaded as a layer, make sure that the format of the zip directory uploaded to the layer is as follows:
|____php_extension
| |____swoole.so
Copy the code
  1. The php.ini writing:
  • Extensions are in the code directory:

    extension=/var/user/php_extension/swoole.so
    Copy the code
  • Extension in layer directory:

    extension=/opt/php_extension/swoole.so
    Copy the code
  1. Create the function entry file in this directoryindex.phpbyextension_loaded( )The extension () function checks whether the extension was loaded successfully and returns when it wastrueOtherwise returnfalse.
<? php function main_handler($event, $context) { var_dump(extension_loaded('swoole')); return "hello world"; }? >Copy the code
  1. 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.

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 Php7.
  • Submission method: Select Locally upload ZIP package.
  1. Click Finish to create the function.

02. Java runtime

Using a dependency management tool such as Maven, install the dependencies locally and upload them with the functional code.

  1. Execute on the local terminalmkdir test-packageCommand to create a directory for storing function code and dependency libraries.
  2. Create in this directorypom.xmlAnd, inpom.xmlTo configure dependency information.
  3. Execute in the project folder root directorymvn packageCommand to compile the output as follows:
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building java-example 1.0 the 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] ------------------------------------------------------------------------Copy the code
  1. Compress the function code and dependent libraries into a JAR package, upload the packaged JAR package in the cloud function console, and create a new function. The operation steps are as follows:

    1. Log in to the Cloud Function console and click Function Services on the left navigation bar.
    2. 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.
    3. 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.
    • Running environment: Select Java8.
    • Submission method: Select Locally upload ZIP package.
  2. Click Finish to create the function.

03. Go runtime

How to use: upload the final binary file when packaging.

The Go runtime’s dependent libraries compile the binary with the code, upload the packaged binary in the Cloud Function console and create a new function. The operation steps are as follows:

  1. Log in to the Cloud Function console and click Function Services on the left navigation bar.
  2. 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.
  3. 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.
    • Running environment: Select Go1.
    • Submission method: Select Locally upload ZIP package.
  4. Click Finish to create the function.

One More Thing

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

Welcome to: Serverless Chinese!