By default, when you update a function in Function Compute, the new configurations and code overwrite the LATEST version immediately. If the function is directly invoked after it is updated, the latest configurations and code of the LATEST version are used. To reduce risk in production environments, you can use the fc-canary plug-in to perform canary release, which routes a percentage of traffic to a new version while keeping the remaining traffic on the current stable version.
This topic describes how to use the fc-canary plug-in to perform canary release in Function Compute V2.0.
How canary release works
Function Compute uses aliases to separate deployments from releases. An alias is a named pointer to a specific function version. In a production environment, you typically configure an alias (for example, prod) that points to a stable, published version.
When you publish a new version, instead of immediately directing all traffic to it, you can use canary release to route a percentage of invocations to the new version. This allows you to validate the new version with real traffic before completing the release.
The fc-canary plug-in automates this process:
Publishes a new version from the latest configurations and code (LATEST version).
Checks whether the specified alias exists.
If the alias does not exist, creates the alias and points it to the new version.
If the alias exists, updates the alias to split traffic between the current stable version and the new version based on the specified canary percentage.
Note: In Function Compute V2.0, aliases are configured at the service level. A single alias applies to all functions within the service.
Prerequisites
Serverless Devs CLI is installed.
The
@serverless-cd/fc-canaryplug-in is available in your project. This is a community-provided plug-in.A function is deployed in Function Compute with at least one published version.
Procedure
Step 1: Configure the fc-canary plug-in
Add the fc-canary plug-in to the pipeline configuration in your Serverless Devs project. The following YAML snippet shows how to configure the plug-in:
# Direct 50% of traffic to the new version for the web-framework-kzbp service
# in the China (Hangzhou) region.
- plugin: @serverless-cd/fc-canary
inputs:
serviceName: web-framework-kzbp
aliasName: prod
regionId: cn-hangzhou
canaryPercent: 50The following table describes the plug-in parameters.
| Parameter | Required | Description |
|---|---|---|
serviceName | Yes (for FC 2.0) | The name of the Function Compute service. For FC 3.0, use functionName instead. |
aliasName | Yes | The name of the alias to update. If the alias does not exist, the plug-in creates it. |
regionId | Yes | The region where the service is deployed. Example: cn-hangzhou. |
canaryPercent | Yes | The percentage of traffic to route to the new version. Valid values: 0 to 100. |
Step 2: Deploy and run the canary release
Update your function code or configurations on the LATEST version.
Run the Serverless Devs pipeline that includes the fc-canary plug-in. The plug-in publishes a new version from the LATEST version and updates the alias to split traffic based on the specified
canaryPercentvalue.Monitor the new version by checking logs and metrics to verify that it works as expected.
Step 3: Complete or roll back the release
Complete the release: Update the alias to point entirely to the new version, with no traffic split. You can update the alias configuration in the Function Compute console or by running the
s cli fc alias publishcommand in Serverless Devs.Roll back: Update the alias to point back to the previous stable version. You can update the alias through the Function Compute console or by running the
s cli fc alias publishcommand to change the alias version.