Invoke a specific function version or reserved instance

更新时间:
复制 MD 格式

This topic describes how to use a Serverless Workflow to invoke a function with a specific version or a reserved instance.

Background

In production environments, functions invoked by workflows may require frequent changes to adapt to new business requirements. To prevent unexpected behavior and ensure stability during these changes, use a specific function version in a Serverless Workflow task step. This method helps in the following scenarios:

  • If a function is updated while an orchestrating flow is running, a later step might invoke the new version and cause unexpected behavior.
  • Enable fast rollbacks. If a new deployment causes flow executions to fail, you can quickly revert to a previous, stable function version.
  • Reduce cold starts. You can use a function alias to invoke a function that has a reserved instance. This keeps the function initialized and minimizes execution latency caused by cold starts. For more information about the billing methods and on-demand mode of Function Compute, see Billing overview and Elasticity management (including reserved mode).

The Function Compute version and alias features support continuous integration and deployment for these scenarios. A reserved instance is tied to a specific function version. Therefore, this example also applies to any scenario that requires a fixed function version.

Procedure

This example includes the following steps:

  1. Create a reserved instance for a function
  2. Create a flow
  3. Use the command line interface or console to execute the workflow and observe the function execution

Step 1: Create a reserved instance

First, create a Function Compute service named fnf-demo. In this service, create a Python 3 function named provision. Then, publish a version and an alias for the function to configure a reserved instance. For more information, see Elasticity management (including reserved mode).

For this example, assume the function has version 1, an alias named online, and one reserved instance. The function code is as follows:

import logging

def handler(event, context):
  logger = logging.getLogger()

  logger.info('Started function test')
  return {"success": True}           

Step 2: Create a flow

Serverless Workflow natively supports Function Compute versions and aliases.

In a Serverless Workflow task step, the resourceArn parameter is typically set to acs:fc:{region}:{accID}:services/fnf/functions/test. By default, this invokes the latest version of the function. You can publish a version or an alias, and then specify the resourceArn as acs:fc:{region}:{accID}:services/fnf.{alias-or-version}/functions/test to invoke a specific version of the function. The flow can then be defined as follows:

version: v1
type: flow
steps:
  - type: task
    resourceArn: acs:fc:::services/fnf-demo.online/functions/provision 
    # You can also use a version number. For example: resourceArn: acs:fc:::services/fnf-demo.1/functions/provision.
    name: TestFCProvision          

Step 3: Execute and observe the workflow

  1. Execution result before using a reserved instance:1
  2. Execution result after using a reserved instance:2

As shown in the execution details, using a reserved instance reduced the execution time of the task step from 500 ms to 230 ms.