Multiple versions of a function may be published as needed. Upon publication, the system automatically generates a unique version number and preserves the current code and configuration as an immutable baseline. In addition, aliases can be created to serve as pointers to specific versions. Leveraging both versions and aliases enables streamlined management across scenarios such as release, rollback, and canary deployments.
Canary release process
Before you start
A function is created. For more information, see Create a function.
Step 1: Prepare and test a function
When you create a function for the first time, the version of the function is LATEST. You can debug the function of the LATEST version until the function becomes stable. You can also invoke the function of the LATEST version in the Function Compute console.
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the Function Details page, 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 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, which indicates the version information, is LATEST. This means the executed function belongs to the function of the LATEST version.
Step 2: Publish and test a version
When the function proves to be stable, you can publish this version and use it to process online requests. For more information, see Publish a version.
After a new version is published, you can execute it in the Function Compute console.
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the Function Details page, switch to the Versions tab page and click the version 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, 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 is 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 can point the alias to the updated version. The caller only needs to use the correct alias and does not need to be aware of the specific versions of the function. For more 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 this topic, the alias alias1 points to version 1.
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the Function Details page, switch to the Aliases tab page and click the alias that you want to view.
On the page that appears, click the Test tab, and then 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 the function of the alias1 alias is invoked and the alias points to Version 1.
After a new version is developed, you can use the canary release feature to ensure that the new version is stable before it is officially rolled out.
A new version can be published only if changes have been made to function configurations or code compared with the previous version.
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.
On the Function Details page, switch to the Aliases tab page. Find the alias that you want to manage and click Modify in the Actions column.
In the alias editing panel, set Version 2 as Canary Release Version, configure Canary Release Version Weight, and then click OK.
After the canary release version becomes stable, you can switch all online traffic to Version 2.
FAQ
How do I check the function version of an invocation?
When you use the canary release feature, Function Compute allocates traffic based on the specified weight. You can check the version of a function that is invoked to process a specific request by using the following methods:
Use the context parameter
In each function invocation, the qualifier and versionId fields are included in the context input parameter.
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 of a synchronous function invocation
Responses to synchronous function invocations contain the x-fc-invocation-function-version header, which indicates the version of the invoked function.
More information
For more information about how to manage versions and aliases, see Manage versions and Manage aliases.