All Products
Search
Document Center

Function Compute:Deploy a code package

Last Updated:Jul 18, 2024

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

Preparations

  1. Create a code directory for tests and specify a name for the directory. In this example, mycode is used.

    • Linux and 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 the index.php file.

    The following code describes the file content.

    <?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

Procedure

  1. Create the composer.json file in the mycode directory.

    Sample code:

    {
      "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. Package all files in the mycode directory.

    • Linux and 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 function that you want to manage. In the upper-right corner of the function details page, click Upload Code to upload the ZIP package that you packaged.

    You can also upload a ZIP package when you create a function in the Function Compute console. For more information, see Create an event function.

  5. On the Code tab of the function details page, click Test Function.

Use Serverless Devs to install dependencies and deploy code

Before you start

Procedure

  1. Edit the s.yaml file in the mycode directory.

    The following sample code provides an example of the file:

    edition: 3.0.0
    name: fcDeployApp
    access: "default"
    
    vars: # The global variables.
      region: "cn-hangzhou"
    
    resources:
      hello_world:
        component: fc3 # The name of the component.
        props:
          region: ${vars.region}              # For information about how to use variables, visit https://docs.serverless-devs.com/serverless-devs/yaml#%E5%8F%98%E9%87%8F%E8%B5%8B%E5%80%BC.
          functionName: "testphp"
          description: 'this is a test'
          runtime: "php7.2"
          code: ./
          handler: index.handler
          memorySize: 128
          timeout: 30  
  2. In the mycode directory, add the composer.json file.

    The following sample code provides an example of the file:

    {
        "require": {
            "nette/utils": "^3.0"
        }
    }
  3. Run sudo s build --use-docker to install dependencies.

    After the execution is complete, the dependencies and code are deployed to the ./vendor directory.

  4. Run sudo s deploy to deploy the project.

    After the execution, you can deploy your function to Function Compute.

More information

You can also use layers of Function Compute to install dependencies. For more information, see Create a custom layer.