Deploy a PHP function with third-party dependencies to Function Compute by packaging your code and the vendor directory into a ZIP file. This guide uses Nette\Utils as an example dependency and covers two deployment methods: Composer with the Function Compute console, and Serverless Devs.
Prepare your code
Set up a local code directory with your function handler before deploying.
Create a directory named
mycodefor your function code.Linux and macOS: Run
mkdir -p /tmp/mycode.Windows: Create a folder named
mycode.
In the
mycodedirectory, createindex.phpwith the following content:<?php require_once __DIR__ . '/vendor/autoload.php'; use Nette\Utils\Arrays; function handler($event, $context) { return Arrays::contains([1, 2, 3], 1); }
Method 1: Use Composer
Install dependencies locally with Composer, package the code into a ZIP file, and upload it through the Function Compute console.
Prerequisites
Before you begin, ensure that you have:
PHP and Composer installed, with permission to run Composer commands. See Composer for installation instructions.
(Optional) A PHP function created in the Function Compute console. See Create an event function.
Install dependencies and deploy
In the
mycodedirectory, createcomposer.json:{ "require": { "nette/utils": "v3.2.5" } }In the
mycodedirectory, runcomposer installto install dependencies. After the command completes, Composer creates acomposer.lockfile and avendordirectory inmycode. Your downloaded dependencies are stored invendor.Package all files in
mycodeinto a ZIP file.Linux and macOS: Navigate to the
mycodedirectory and run:zip code.zip -r ./*Note: Make sure you have read and write permissions on the directory.
Windows: Open the
mycodedirectory, select all files, right-click, and compress them into a ZIP file.
Note: Make sure that
index.phpis in the root directory of the package.In the Function Compute console, open the function you want to update. In the upper-right corner of the function details page, click Upload Code and select the ZIP file. Alternatively, upload a ZIP file when creating a function. See Create an event function.
On the Code tab of the function details page, click Test Function to verify the deployment.
Method 2: Use Serverless Devs
Use Serverless Devs with Docker to build and deploy your function without manually managing the ZIP file.
Prerequisites
Before you begin, ensure that you have completed the following:
Install dependencies and deploy
In the
mycodedirectory, creates.yamlwith the following content: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: 30In the
mycodedirectory, createcomposer.json:{ "require": { "nette/utils": "^3.0" } }Run the following command to install dependencies inside a Docker container that matches the Function Compute runtime environment:
sudo s build --use-dockerAfter the command completes, Serverless Devs installs the dependencies and code into the
./vendordirectory.Run the following command to deploy the function:
sudo s deployAfter the command completes, your function is deployed to Function Compute.
What's next
To manage dependencies without packaging them into every deployment, use Function Compute layers. See Create a custom layer.