Implement canary releases with versions and aliases

更新时间:
复制 MD 格式

You can publish one or more versions for a function. Each time you publish a version, Function Compute creates an immutable snapshot of your code and configuration with a unique version number. You can also create an alias, which acts as a pointer to a specific version. Using versions and aliases together lets you easily manage deployments, perform rollbacks, and implement canary releases.

Canary release workflow

Prerequisites

Step 1: Prepare and test the function

When you create a function, its initial version is LATEST. You can develop and test your function in the LATEST version until it's stable. You can invoke the LATEST version of the function from the console.

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

  2. In the top navigation bar, select a region. On the Functions page, click the target function.

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

  4. In the code editor, update the code to view the function version. Click Deploy, and then click Test Function.

    The following code samples show how to view 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. The log shows that the qualifier field has a value of LATEST, which means that the LATEST version of the function was executed.

Step 2: Publish and test a version

When the LATEST version of your function is stable, you can publish it to serve production traffic. For more information, see Publish a version.

After publishing a new version, you can invoke it from the console.

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

  2. In the top navigation bar, select a region. On the Functions page, click the target function.

  3. On the function details page, click the Version Management tab, and then click the target version.

  4. On the details page of the target version, click the Code tab, and then click Test Function.

    After the execution is complete, you can view the execution log. The log output shows that the qualifier is 1 and the parsed versionId is 1, which means that version 1 of the function was executed.

Step 3: Use an alias to switch traffic

After you deploy a new version, you can create an alias that points to it. When you update the function, you can update the alias to point to the new version. This lets callers use a fixed alias without needing to track version numbers. For instructions on how to create an alias, see Create an alias.

After creating the alias, you can verify from the console that the correct function version is invoked.

In this example, the alias alias1 points to version 1.

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

  2. In the top navigation bar, select a region. On the Functions page, click the target function.

  3. On the function details page, click the Alias Management tab, and then click the target alias.

  4. On the details page of the target alias, click the Test tab, and then click Test Function.

    After the execution is complete, you can view the log output. The log output shows that the qualifier is alias1 and the resolved versionId is 1. This indicates that the function was executed by using the alias alias1, which points to version 1.

After you develop a new version, use a canary release to ensure a stable rollout.

Note

You can publish a new version only if the function's code or configuration has changed since the previous version was published.

  1. Publish a new version 2. For more information, see Publish a version.

    After the version is published, you can view the new version in the version list.

  2. On the function details page, click the Alias tab. In the Actions column for the target alias, click Modify.

  3. In the Modify Alias panel, set the new version 2 as the Canary Release Version, configure the Canary Release Version Weight, and then click OK.

    After verifying that the canary version is stable, you can perform a full switchover to route all traffic to version 2.

FAQ

Confirming the invoked function version

When you use the canary release feature, Function Compute allocates traffic according to the weights that you specify. You can determine the version of the function that is invoked in the following ways:

  • Determined by the context input parameter

    For each function invocation, the context input parameter includes the qualifier and versionId fields.

    • qualifier: The version or alias of the function to invoke.

    • versionId: The specific version number that is resolved from the qualifier when the function is executed.

  • By using the response of a synchronous function invocation

    The response of a synchronous function invocation includes the x-fc-invocation-function-version header, which specifies the invoked function version.

References

For more information on publishing function versions and configuring aliases, see Manage versions and Manage aliases.