This topic describes how to implement canary release by using the versions and aliases of services in the Function Compute console.
Canary release process

Before you begin
Step 1: Prepare and test the function
When you create a service and its function for the first time, the version of the service is LATEST. You can debug the function in the service of the LATEST version until the service becomes stable. You can also invoke the function in the service of the LATEST version in the Function Compute console.
- Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
- In the top navigation bar, select a region. On the Services page, click the desired service.
- On the Functions page, click the name of the desired function. On the Function Details page that appears, click the Code tab.
- Modify the code in the code editor to view the function version. Click Deploy and click Test Function.
The following sample code is used to check the function version.
module.exports.handler = function(eventBuf, context, callback) { var qualifier = context['service']['qualifier'] var versionId = context['service']['versionId'] console.log('Qualifier from context:', qualifier); console.log('VersionId from context: ', versionId); callback(null, qualifier); };
# -*- coding: utf-8 -*- def handler(event, context): qualifier = context.service.qualifier versionId = context.service.version_id print('Qualifier from context:' + qualifier) print('VersionId from context:' + versionId) return 'hello world'
<?php function handler($event, $context) { $qualifier = $context["service"]["qualifier"]; $versionId = $context["service"]["versionId"]; print($qualifier); print($versionId); return "hello world"; }
using System; using System.IO; using Aliyun.Serverless.Core; using Microsoft.Extensions.Logging; namespace Desktop { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); } } class App { public string Handler(Stream input, IFcContext context) { ILogger logger = context.Logger; var qualifier = context.ServiceMeta.Qualifier; var versionId = context.ServiceMeta.VersionId; logger.LogInformation("Qualifier from context: {0}", qualifier); logger.LogInformation("versionId from context: {0}", versionId); return "hello word"; } } }
After the execution is complete, you can view the log output. In the log output, you can find that the value of the qualifier parameter that indicates the version information is LATEST. This value indicates that the executed function belongs to the service of the LATEST version.
Step 2: Publish a version and test the version
When the service of the LATEST version becomes stable, you can publish the service of this stable version to respond to online requests. For more information, see Publish a version.
After a new version is published, you can execute a function of the new version in the Function Compute console.
- Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
- In the top navigation bar, select a region. On the Services page, click the desired service.
- On the Functions page, select a version from the Version drop-down list.
- Click the name of the desired function. On the function details page that appears, click the Code tab and click Test Function.
After the function is executed, you can view the execution log output. In the log output, you can find that the value of the qualifier parameter, which indicates the version information, is 1 and the value of the versionId parameter, which indicates the version ID, is 1. The log output shows that the executed function belongs to the service of Version 1.
Step 3: Use an alias to divert traffic
After a version is published, you can create an alias and point the alias to the version. When this version is updated, you point the alias points to the new version. This way, the caller needs to only use the correct alias, regardless of specific versions. For more information about how to create an alias, see Creates an alias.
After the alias is created, you can verify whether the correct version of the function is executed in the Function Compute console.
This topic uses the alias alias1 to point to version 1.
- Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
- In the top navigation bar, select a region. On the Services page, click the desired service.
- On the Functions page, click the name of the desired function.
- In the upper right corner of the function details page, select alias1 from the Version or Alias drop-down list.
- Click the Code tab and click Test Function.
After the execution is complete, you can view the log output. In the log output, you can find that the value of the qualifier parameter, which indicates the version information, is alias1 and the value of the versionId parameter, which indicates the version ID, is 1. The log shows that the executed function belongs to the service associated with the alias alias1 and the alias points to Version 1.
After a new version is developed, you can use a canary release version to ensure that the new version is stable.
- Releases version 2 as a new version. For more information, see Publish a version.
After the version is published, you can view the latest version in the version list.
- In the left-side navigation pane, click Aliases.
- In the alias list, find the alias alias1 that points to Version 1 and click Modify in the Actions column.
- In the panel of alias modification, specify Version 2 as Canary Release Version. Select a canary release type and click OK.
- Random Canary Release by Percentages
Specify Canary Release Version Weight. The following figure shows an example.
After the preceding settings are complete, you can call the alias alias1 to verify whether a specific amount of traffic is diverted to Version 2. In this example, if the alias alias1 is called to execute the function 10 times, Version 2 is called three times and Version 1 is called seven times.
- Canary Release Based on Specified Rules
Note Canary Release Based on Specified Rules only supports HTTP functions.
In this example, a request message is routed to the canary release version only when the value of key is
x-test-uid
.
The following table describes the parameters for creating a canary release rule.Parameter Description Name Enter the name of the alias to create. Major Version Specify the major version of the alias. Canary Release Version Specify whether to enable the canary release version. Canary Release Version Specify the canary release version of the alias. Canary Release Type Select a canary release type, which can be Random Canary Release by Percentages or Canary Release Based on Specified Rules. - Random Canary Release by Percentages: Specify a weight to divert a proportion of traffic to the canary release version.
- Canary Release Based on Specified Rules: Specify the rules based on which the canary release is performed.
Canary Release Version Weight This parameter is required if you set Canary Release Type to Random Canary Release by Percentages. This parameter specifies the percentage of traffic diverted to the canary release version. For example, if you set this parameter to 5%, Function Compute allocates 5% of the traffic to the canary release version and 95% of the traffic to the major version.
Rule Mode This parameter is required if you set Canary Release Type to Canary Release Based on Specified Rules. This parameter specifies the selection mode for the specified rules. Valid values:- Meet All of the Following Rules
- Meet Any of the Following Rules
Rules This parameter is required if you set Canary Release Type to Canary Release Based on Specified Rules. Specifies the content of the rules. Each rule contains the following fields:- Parameter Type: Specify the type of the parameter. Valid values: Header, Cookie, and Query.
- Parameter: Specify the name of the parameter that is used to control the canary release.
- Condition: Select a condition for calculation. Function Compute compares the actual value of the parameter in Parameter and the parameter value that you specify based on the specified condition. If the condition is met, the request is routed to the canary release version. Valid values:
=, !=, >, <, >=, <=
: caparison conditions for calculation. For example, if you set Condition to=
, a request is routed to the canary release version only when the actual values of the request parameters are equal to the value of Value.Include
: the string matching condition. A request is routed to the canary release version only when the actual values of the request parameters are included in the value of Value.Value Distribution
: Perform the canary release based on the distribution of specified parameter values. For example, if you set Parameter Type to Header, Parameter touser-id
, and Value to20
, 20% HTTP requests are routed to the canary release version based on the distribution of the values of the request headeruser-id
.
- Value: Specify the parameter value that is used to control the canary release.
You can click + Add Rule to add more rules.
Advanced settings Canary Release Weight of Provisioned Instances Specify the proportion to control the canary release weight of instances in provisioned mode. If the number of provisioned instances is greater than 1, Function Compute generates instances based on the specified weight. If you do not specify this parameter, the default weight is 1%. Important The value of this parameter is valid only when the number of provisioned instances is greater than 1.After the canary release version becomes stable, you can switch all online traffic to the service of the new version 2. - Random Canary Release by Percentages
FAQ
How can I know the version of the called service?
- Use the context parameter
Every time a function is invoked, the service parameter contained in the context parameter includes the qualifier and versionId fields.
- qualifier: the version information that is passed in when the function is called. It can be a version number or an alias.
- versionId: the specific version ID that is parsed out based on the qualifier parameter when the function is executed.
- Use the response to a synchronous function invocation
The response to a synchronous function invocation contains the x-fc-invocation-service-version header that indicates the version of the called service.