My Note

自己理解のためのブログ

Cloud FunctionにgcloudでGoコードをデプロイする

CloudFunctionにGoをgcloudを利用してデプロイする

$ gcloud components install beta
$ export GO111MODULE=on

■ create repo of github ( github.com/yhidetoshi/gcloud-cloud-function-hello )

$ ~/sample-cloudfunctions-go/ go mod init github.com/yhidetoshi/gcloud-cloud-function-hello
  go: creating new go.mod: module github.com/yhidetoshi/gcloud-cloud-function-hello

■ function.go

package sample

import "net/http"

func Hello(w http.ResponseWriter, r *http.Request) {
    msg := "Hello World"
    w.Write([]byte((msg)))
}

$ gcloud functions deploy Hello --runtime go111 --trigger-http

curl https://us-central1-XXXXXXXXXXXX.cloudfunctions.net/Hello

> Hello World

デプロイ結果

f:id:yhidetoshi:20190618231948p:plain

f:id:yhidetoshi:20190618232112p:plain