All Products
Search
Document Center

Function Compute:Use versions and aliases to implement canary release

Last Updated:Apr 02, 2024

You can publish one or more versions for a function. A version is used in the similar way as a snapshot of the function. When you publish a version, Function Compute generates a snapshot for the function and automatically assigns a version number to the snapshot. You can also create an alias and point the alias to a version of the function. Versions and aliases of a function can be used together to implement features such as release, rollback, and canary release.

Canary release process

image

Before you start

Step 1: Prepare and test the 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.

  1. Log on to the Function Compute console. In the left-side navigation pane, click Functions.

  2. In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.

  3. On the function details page, click the Code tab.

  4. 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 that indicates the version information is LATEST. This value indicates that the executed function belongs to the function of the LATEST version.

Step 2: Publish a version and test the version

When the function of the LATEST version is ready, you can publish a new version and use the 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.

  1. Log on to the Function Compute console. In the left-side navigation pane, click Functions.

  2. In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.

  3. On the function details page, click the Version tab and click the version that you want to manage.

  4. 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 a new version is published, you can point the alias to the new version. The caller need to only use the correct alias and does not need to know 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.

  1. Log on to the Function Compute console. In the left-side navigation pane, click Functions.

  2. In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.

  3. On the function details page, click the Alias tab and click the alias that you want to view.

  4. 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.

Note

A new version can be published only if changes have been made to function configurations or code compared with the previous version.

  1. 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.

  2. On the function details page, click the Alias Management tab. Find the alias that you want to manage and click Modify in the Actions column.

  3. 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