Before you publish a version of a service, you must create a function for the service and debug the function to ensure that it can be stably executed. This topic describes how to invoke a function in a service of the LATEST version in the Function Compute console or by calling the API operation.

Prerequisites

  1. A service is created.
  2. A function is created.

In this example, the created service is named service and the created function is named function.

Invoke a function in a service of the LATEST version in the Function Compute console

  1. Log on to the Function Compute console.
  2. In the top navigation bar, select a region.
  3. In the left-side navigation pane, click Services and Functions.
  4. Find the function named function in the service named service and click the name of the function.
  5. Click the Code tab. In the function code section, add the code that is used to check the version of the service for which the function is created.
    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";
            }
        }
    }
  6. Click Invoke.
    After the function is executed, you can view the execution log in the lower part of the page. In the log, you can see 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-version

Invoke a function in a service of the LATEST version by calling the API operation.

The t1881175.html#section_dhk_f7z_9gw operation can be called to invoke a function in a service of the LATEST version in the following scenarios:

  • Invoke a function in a service of the LATEST version when version management is not enabled

    When no version information is specified, the system invokes the function in the service of the LATEST version by default. The request is in the following format:

    POST /services/{serviceName}/functions/{functionName}/invocations                   
    The following sample request is used:
    POST /services/{service}/functions/{function}/invocations                   
  • Invoke a function in a service of the LATEST version by using the suffix of the specified version
    To enable the system to invoke a function in a service of the LATEST version, you can append .LATEST to the serviceName parameter in the request. The request is in the following format:
    POST /services/{serviceName}.LATEST/functions/{functionName}/invocations                       
    The following sample request is used:
    POST /services/{service}.LATEST/functions/{function}/invocations                       
The following API operations support similar usage:

What to do next

  1. Publish a version
  2. t1881013.html#multiTask1269