You can publish one or more versions for a service. A version is used in the similar way as a snapshot of the service. When you publish a version, Function Compute generates a snapshot for the service and automatically assigns a version number to associate the service with the snapshot. You can also create an alias for the version of the service and point the alias to the version. You can use the version and alias of a service to implement features such as release, rollback, and canary release. 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 start
Step 1: Prepare and test the function
When you create a service and a function in the service 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.
In the code editor, modify the code to view the version of the function, click Deploy, and then click Test Function.
The following sample code shows how to check the version of a function in different program languages:
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 version of the function in the log output. In the log output of this example, the value of the qualifier field is LATEST. The 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 the 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 function that you want to manage. On the page that appears, click the Code tab and then click Test Function.
After the function is executed, you can view the execution log output. In the log output of this example, the value of the qualifier field is 1 and the value of the versionId field is 1. The values indicate that the executed function belongs to the service of Version 1.
Step 3: Use an alias to switch traffic
After a version is published, you can create an alias and point the alias to the version. When this version is updated, you can point the alias to the new version. This way, the caller needs to use only the correct alias to invoke the function. For information about how to create an alias, see Create an alias.
After the alias is created, you can verify whether the correct version of the function is executed in the Function Compute console.
In the example of this topic, the alias alias1 points 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 then click Test Function.
After the execution is complete, you can view the version of the function in the log output. In the log output of this example, the value of the qualifier field is alias1 and the value of the versionId field is 1. The values indicate 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.
Publish 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 that appears, set the Canary Release Version parameter to 2, select a canary release type, and then click OK.
Random Canary Release by Percentages
Specify a value for the Canary Release Version Weight parameter. The following figure provides an example.
After the preceding settings are complete, you can call the alias alias1 to verify whether a specific amount of traffic is switched 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
NoteYou can select Canary Release Based on Specified Rules only for 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 that you want to create.
Major Version
The major version of the alias.
Enable Canary Release Version
Specify whether to enable the canary release version.
Canary Release Version
The canary release version of the alias.
Canary Release Type
Select a canary release type. Valid values: Random Canary Release by Percentages and Canary Release Based on Specified Rules.
Random Canary Release by Percentages: the weight to switch traffic to the canary release version.
Canary Release Based on Specified Rules: the rules based on which the canary release is performed.
Canary Release Version Weight
This parameter is required only if you set the Canary Release Type parameter to Random Canary Release by Percentages.
This parameter specifies the percentage of traffic switched 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 only if you set the Canary Release Type parameter 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 only if you set the Canary Release Type parameter to Canary Release Based on Specified Rules.
This parameter specifies the content of the rules. Each rule contains the following fields:
Parameter Type: the type of the parameter. Valid values: Header, Cookie, and Query.
Parameter: the name of the parameter that is used to control the canary release.
Condition: the comparison condition. After you submit a request, Function Compute compares the actual value of the Parameter field and the value that you specified for the Parameter field based on the specified condition. If the condition is met, the request is routed to the canary release version. Valid values:
=, !=, >, <, >=, <=
: comparison operators. For example, if you set the Condition field to=
, the request is routed to the canary release version only when the actual value of the request parameter is equal to the value that you specify in the Value field.in
: the string matching condition. A request is routed to the canary release version only when the actual value of the request parameter is included in the value that you specify in the Value field.Value Distribution
: Perform the canary release based on the distribution of specified parameter values. For example, you set the Parameter Type field to Header, the Parameter field touser-id
, and the Value field to20
. This indicates that requests that correspond to 20% of the distribution values of the HTTP request headeruser-id
are routed to the canary release version.
Value: 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
The percentage of instances in provisioned mode for canary release. If the number of provisioned instances is greater than 1, Function Compute generates instances based on the specified value. If you do not specify the parameter, the default value is 1%.
ImportantThe 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.
FAQ
How do I check the version of the called service?
When you use the canary release feature, Function Compute allocates traffic based on the specified weight. You can use one of the following methods to check the version of a 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 invoked. The value can be a version number or an alias.
versionId: the specific version number that is parsed out based on the qualifier field 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.