通过函数计算的Custom Runtime,您可以使用Go在函数计算平台编写函数。本文介绍快速部署和调用Go函数的详细步骤。您可以按需修改Go示例中的代码实现二次开发。

操作步骤

  1. 执行以下命令,初始化项目。
    s init fc-custom-golang-event -d fc-custom-golang-event
  2. 执行以下命令,进入项目目录。
    cd fc-custom-golang-event
  3. 可选:按需修改函数的示例代码,实现二次开发。
  4. 执行以下命令,部署项目。
    s deploy -y
    输出示例:
    [2022-02-22 08:39:41] [INFO] [S-CLI] - Start the pre-action
    [2022-02-22 08:39:41] [INFO] [S-CLI] - Action: make build
    docker build -t fc-go-runtime  -f build-image/Dockerfile build-image
    Sending build context to Docker daemon  2.048kB
    Step 1/5 : FROM golang:1.12.16-stretch
     ---> 7ad03a8aece5
    ......
    Step 5/5 : RUN go get github.com/awesome-fc/golang-runtime
     ---> Using cache
     ---> 74746b35f154
    Successfully built 74746b35f154
    Successfully tagged fc-go-runtime:latest
    docker run --rm -it -v $(pwd):/tmp fc-go-runtime bash -c "GOARCH=amd64 go build -o /tmp/code//bootstrap /tmp/code/main.go"
    chmod +x code/bootstrap
    [2022-02-22 08:39:48] [INFO] [S-CLI] - End the pre-action
    
    Function [goEventFunc] was changed, please confirm before deployment:
        * You can also specify to use local configuration through --use-local during deployment)
    
     Code check:
    Code package has changed in other ways(checksum):
    Last local deployment -> Online status:null -> 10552998901932247431
    
     Remote function: goEventFunc is inconsistent with the config you deployed last time, deploy it with local config o
    r remote config? use local
     Checking Function goEventFunc exists
     Checking Service, Function (3.69s)
     Creating Service, Function (4.82s)
    
    Tips for next step
    ======================
    * Display information of the deployed resource: s info
    * Display metrics: s metrics
    * Display logs: s logs
    * Invoke remote function: s invoke
    * Remove Service: s remove service
    * Remove Function: s remove function
    * Remove Trigger: s remove trigger
    * Remove CustomDomain: s remove domain
    
    helloworld:
      region:   cn-hangzhou
      service:
        name: hello-world-service
      function:
        name:       goEventFunc
        runtime:    custom
        handler:    index.handler
        memorySize: 512
        timeout:    30
  5. 执行以下命令,远程调用函数。
    s invoke -e "{}"
    输出示例:
    Please select an access: test
    [2022-02-22 08:40:15] [WARN] [S-CORE] -
    
    If you don't want to select access every time, configure it in yaml:https://github.com/Serverless-Devs/Serverless-Devs/discussions/149
    
    
    ========= FC invoke Logs begin =========
    FC Invoke Start RequestId: fd3066ff-be85-487a-aa0a-d5eca929be33
    2022-02-22T08:40:16.583Z: fd3066ff-be85-487a-aa0a-d5eca929be33 [INFO]  hello golang!
    FC Invoke End RequestId: fd3066ff-be85-487a-aa0a-d5eca929be33
    
    Duration: 3.11 ms, Billed Duration: 4 ms, Memory Size: 512 MB, Max Memory Used: 7.57 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    {}
    
    
    End of method: invoke