This example describes how to use the version and alias of a service to implement the phased release feature in the Function service console.

Canary release process

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 execute functions under the LATEST version in the 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 in the service and click the name of the function.
  5. Click the Function Code tab, add the code to view the function version, and then click Test Function.
    The following sample code is used to check the version of the service for which the function is created:
    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. From the log output, you can see that the value of the field qualifier that indicates the version information is LATEST, that is, the function executed this time is a function under 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 serve online requests. For more information, see Publish a version. After the new version is released, you can use the console to execute the functions under the new version. version1
  1. On the Services page, click the target service.
  2. On the Functions page, select a version from the Version drop-down list.
    version1
  3. Click the name of the target function, click the Function Code tab, and then click Test Function.
    After the execution is complete, you can view the execution logs. From the log output, you can see that the version information is qualifier to 1 when the function is executed, and the parsed versionId is 1. That is, the function executed this time is a function under version 1. log1

Use an alias to switch traffic

After the new version is launched, you can create an alias and set the alias to point to the version. When this version is updated, you need only to change the version to which the alias points to the new version. The caller does not need to care about the specific version of the service, but only needs to use the correct alias. For more information about how to create an alias, see Create an alias. After the alias is created, you can use the console to verify that the correct version of the function is executed.

  1. On the Services page, click the target service.
  2. In the left-side navigation pane, click Functions. Find the target function and click the target function.
  3. On the function details page, select the alias from the Version or Alias drop-down list.
    choose alias
  4. Click the Function Code tab, and then click Test Function.
    After the execution is complete, you can view the log output. From the log output, you can see that the version information is qualifier to alias1 when the function is executed, and the parsed versionId is 1. That is, the function executed this time is a function under the alias alias1, which points to version 1. alias1

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

  1. Release a new version. For more information, see Publish a version.
    After publishing, you can view the newly published 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 Edit in the Actions column.
    alias-edit
  4. In the Edit Service Alias panel, set New Version 2 to Canary Version and enter the weight of the canary version. Click OK.
    Grayscale weight

    After the settings are complete, you can call the alias alias1 to verify that some online traffic has been switched to the new version 2. In this example, the alias alias1 is called to execute the function 10 times. As a result, version 2 is called 3 times and version 1 is called 7 times.

    alias1-version1alias1-version2
    You can also switch all online traffic to the new version 2 after the canary version runs stably.

FAQ

How do I determine the version of the called service?

When you use the canary release feature, you Function service allocate traffic based on the specified weight. You can use the following methods to determine the version of the called service:
  • Determined by context input parameters
    For each function call, the service parameter of the context input parameter includes the qualifier and versionId fields.
    • qualifier: the version information that is passed in when the function is invoked. It can be a version number or an alias.
    • versionId: the specific version number that is parsed based on the qualifier when the function is executed.
  • Check the response to a synchronous function call

    The response to a synchronous function call contains the x-fc-invocation-service-version header that indicates the version of the called service.