This topic uses a third-party dependency Nette\Utils as an example to describe how to install dependencies for your PHP code, package the code, and deploy it to your Function Compute.

Preparations

  1. Create a code directory for testing and specify a name for the directory. In this example, mycode is used.
    • Linux or macOS

      Run the mkdir -p /tmp/mycode command to create the directory.

    • Windows

      Create a folder and name it mycode.

  2. In the mycode directory, create a file and name the file index.php.
    The following code describes the content of the file:
    <?php
    
    require_once __DIR__ . '/vendor/autoload.php';
    
    use Nette\Utils\Arrays;
    
    function handler($event, $context) {
        return Arrays::contains([1, 2, 3], 1);
    }

Use Composer to install dependencies and deploy the code package

Prerequisites

  • PHP and Composer are installed on your machine, which has the permissions to execute Composer commands. For more information about Composer, see Composer.
  • Optional:A PHP function is created in the Function Compute console. For more information, see Create a function.

Procedure

  1. Create the composer.json file in the mycode directory.
    The following code shows the content of the file:
    {
      "require": {
        "nette/utils": "v3.2.5"
      }
    }
  2. Run the composer install command in the mycode directory to install dependencies.
    After the execution is complete, the composer.lock file and the vendor directory are generated in this directory. The downloaded dependencies are stored in the vendor directory.
  3. Compress all files in the mycode directory.
    • Linux or macOS

      Go to the mycode directory and run the zip code.zip -r ./* command.

      Note Make sure that you have the read and write permissions on the directory.
    • Windows

      Go to the mycode directory, select all files, right-click the files, and then compress the files into a ZIP package.

    Note Make sure that the index.php file that you created is in the root directory of the package.
  4. In the Function Compute console, find the desired function. In the upper-right corner of the function details page, click Upload Code to upload the ZIP package obtained in the previous step.
    You can also upload a ZIP package when you create a function in the Function Compute console. For more information, see Create a function.
  5. On the Code tab, click Test Function.

Use Serverless Devs to install dependencies and deploy the code

Prerequisites

Install Serverless Devs and Docker

Procedure

  1. Create the s.yaml file in the mycode directory.
    The file contains the following content.
    edition: 1.0.0          #  The version of the YAML syntax. The version complies with the semantic versioning specification.
    name: fcDeployApp       #  The name of the project.
    access: "default"  #  The alias of the key.
    
    services:
      fc-deploy-test: #  The name of the service.
        component: fc  # The name of the component.
        props: # The property value of the component.
          region: cn-shanghai
          service:
            name: fctest
            description: 'test'
            internetAccess: true
          function:
            name: testphp
            description: this is a test
            runtime: php7.2
            codeUri: ./
            handler: index.handler
            memorySize: 128
            timeout: 6
  2. Run the sudo s build --use-docker command to install dependencies.
    After you run the command, the .s directory is generated in the mycode directory. Dependencies and related code are deployed to the .s/build/artifacts/fctest/testphp/vendor directory.
  3. Run the sudo s deploy command to deploy the project.
    After you run the command, you can deploy the function to Function Compute.

Additional information

You can also use the layer feature of Function Compute to install dependencies. For more information, see Instance concurrency management.