This topic shows you how to use the SDK for Go to configure PreStop and PreFreeze functions.

Background information

Feature overview

Sample code

The following code provides an example on how to configure PreStop and PreFreeze functions when you create a function by using the SDK for Go:

package main

import (
  "github.com/aliyun/fc-go-sdk"
  "fmt"
  "os"
)

func main() {
  client, _ := fc.NewClient(
    os.Getenv("ENDPOINT"),
    "2016-08-15", 
    os.Getenv("ACCESS_KEY_ID"),
    os.Getenv("ACCESS_KEY_SECRET"),
  )

  serviceName := "ExtensionService"
  functionName := "ExtensionFunction"
  createFunctionInput := fc.NewCreateFunctionInput(serviceName).WithFunctionName(functionName)
  // Configure PreStop and PreFreeze functions.
  preStopHook := fc.NewLifecycleHook().WithHandler("index.preStop").WithTimeout(int32(30))
  preFreezeHook := fc.NewLifecycleHook().WithHandler("index.preFreeze").WithTimeout(int32(10))
  instanceLifecycle := fc.NewInstanceLifecycleConfig().WithPreStopHook(preStopHook).WithPreStopHook(preFreezeHook)
  createFunctionOutput, err := client.CreateFunction(createFunctionInput.WithInstanceLifecycleConfig(instanceLifecycle))
  if err ! = nil {
    fmt.Fprintln(os.Stderr, err)
  } else {
    fmt.Printf("CreateFunction response: %s \n", createFunctionOutput)
  }
  return
}