This topic describes how to implement canary release of the version and alias of a service in the Function Compute console.

Canary release process

Figure-canary release

Prerequisites

Prepare a 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 a service of the LATEST version in the Function Compute console

  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, click Services and Functions.
  3. In the top navigation bar, select the region where the service resides.
  4. Find the function that you want to manage in the specified service and click the name of the function.
  5. On the function details page, click the Code tab, modify the function code to the code to view the version of the function in the code editor, click Save, 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 function version in 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. log

Publish a 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 Procedure. After a new version is published, you can execute a function of the new version in the Function Compute console.

  1. On the Services page, click the desired service.
  2. In the Functions page, select a version from the Version drop-down list.
    version1
  3. On the function details page, 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 that indicates the version information is 1 and the value of the versionId parameter that indicates the version ID is 1. The log output shows that the executed function belongs to the service of Version 1. log1

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 need to only change the version to which 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 Procedure. After the alias is created, you can verify whether the correct version of the function is executed in the Function Compute console.

  1. On the Services page, click the desired service.
  2. In the left-side navigation pane, click Functions and find the desired function.
  3. On the function details page, select the alias from the Version or Alias drop-down list.
    choose alias
  4. Click the Code tab and click Test Function.
    After the execution is complete, you can view the function version in the log output. In the log output, you can find that the value of the qualifier parameter that indicates the version information is alias1 and the value of the versionId parameter that 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. alias1

After a new version is developed, you can use a canary release version to ensure that the new version is stable.

  1. Publish a new version. For more information, see Procedure.
    After the version is published, you can view the latest version in the version list. version2
  2. In the left-side navigation pane, click Aliases.
  3. In the alias list, find the alias alias1 that points to Version 1 and click Modify in the Actions column.
    alias-edit
  4. In the panel of alias modification, specify Version 2 as the canary release version and enter the weight of the version. Click OK.

    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, the alias alias1 is called to execute the function 10 times. As a result, Version 2 is called three times and Version 1 is called seven times.

    alias1-version1alias1-version2
    After the canary release version becomes stable, you can divert all traffic to the service of Version 2.

FAQ

How do I know the version of the called service?

When the canary release version feature is enabled, Function Compute distributes traffic based on the specified weights. You can check the version of the called service by using the following methods:
  • 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 number that is parsed out based on the qualifier 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.