Google Cloud 웹사이트(?)에서 inline code editor로 하는 방법도 있지만.. 나는 인라인 코드 에디터가 불편해서 로컬에서 배포했다
본인은 맥OS 기준
로컬에서 하려면 로컬에 설치해야 한다
서버에서 돌리는거면 스킵
돈 빠져나갈 수도 있다는 소리
python3 -V
python -V
Google Cloud SDK를 사용하려면 파이썬 3.8~3.12여야 한다
나는 3.11 이였는데 어차피 메인 설치 스크립트가 파이썬 다운로드 받을거냐고 프롬프트 띄어주더라
macOS 64-bit (ARM64, Apple M1 silicon)
./google-cloud-sdk/install.sh
./google-cloud-sdk/bin/gcloud init
gcloud init
구글 로그인하라는 프롬프트 뜨면 계정으로 로그인하면됨
...
mkdir ~/helloworld
cd ~/helloworld
// Package helloworld provides a set of Cloud Functions samples.
package helloworld
import (
"encoding/json"
"fmt"
"html"
"net/http"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
)
func init() {
functions.HTTP("HelloHTTP", HelloHTTP)
}
// HelloHTTP is an HTTP Cloud Function with a request parameter.
func HelloHTTP(w http.ResponseWriter, r *http.Request) {
var d struct {
Name string `json:"name"`
}
if err := json.NewDecoder(r.Body).Decode(&d); err != nil {
fmt.Fprint(w, "Hello, World!")
return
}
if d.Name == "" {
fmt.Fprint(w, "Hello, World!")
return
}
fmt.Fprintf(w, "Hello, %s!", html.EscapeString(d.Name))
}
cd ~/helloworld
go mod init example.com/hello
go mod tidy
cd ~/helloworld
go mod init example.com/hello
go mod tidy
package main
import (
"log"
"os"
// Blank-import the function package so the init() runs
_ "example.com/hello"
"github.com/GoogleCloudPlatform/functions-framework-go/funcframework"
)
func main() {
// Use PORT environment variable, or default to 8080.
port := "8080"
if envPort := os.Getenv("PORT"); envPort != "" {
port = envPort
}
if err := funcframework.Start(port); err != nil {
log.Fatalf("funcframework.Start: %v\n", err)
}
}
새로 추가한 의존성이 확인 불가하다는 에러 뜨면 터미널에
go get <의존성>
go mod tidy
export FUNCTION_TARGET=HelloHTTP
go run ~/helloworld/cmd/main.go
helloworld/
에서 다음 명령어 입력
gcloud functions deploy go-http-function \
--gen2 \
--runtime=go121 \
--region=REGION \
--source=. \
--entry-point=HelloHTTP \
--trigger-http \
--allow-unauthenticated
REGION에는 본인 프로젝트의 리전을 넣으면된다 (예) us-west1, us-central1
--allow-unauthenticated
설정으로 로그인 등 없이 접근 가능
빌드 잘 됐는지는 터미널에 결과로도 뜬다
gcloud functions describe go-http-function --region=REGION
요 명령어로 uri 값을 받아서 방문해보면된다
런타임 환경변수 설정이 필요했음
gcloud functions deploy go-http-function --set-env-vars FOO=bar FLAGS
콤마로 구분한다
gcloud functions deploy go-http-function --set-build-env-vars FOO=bar,BAZ=boo
디렉토리 안에 .yaml
등 파일을 만들어서 환경변수로 사용하는 것
gcloud functions deploy go-http-function --set-env-vars FOO=bar,BAZ=boo
config.yaml
은 다음과 비슷하게 쓰면 됨
SERVICE_KEY: "HELLOOSLDFJSLDFJLKSDF"
URL: "https://hellohello.com/"
모든 value 값은 string이여야 함
go
코드에서는 os.Getenv("환경변수 키 값")
으로 가져다 쓰면됨
(ex) os.Getenv("SERVICE_KEY")
gcloud functions deploy go-http-function --update-env-vars FOO=bar
gcloud functions deploy go-http-function --update-env-vars FOO=bar,BAZ=boo
https://cloud.google.com/sdk/docs/install-sdk
https://cloud.google.com/functions/docs/create-deploy-http-go
https://cloud.google.com/functions/docs/configuring/env-var