新手向:GCP Cloud Functions部署Go REST API及自动化指南
部署Go版REST API到GCP Cloud Functions:结构优化+gcloud部署+Terraform自动化
一、优化后的项目文件夹结构
按照Go项目规范+GCF部署要求调整结构,让每个函数拥有独立部署上下文,公共依赖统一管理:
Root Folder │ ├── cmd/ │ └── main.go # 本地测试入口(保留原有逻辑) ├── internal/ # 私有公共依赖模块(仅本项目可导入) │ ├── models/ │ │ └── models.go │ ├── validations/ │ │ └── validations.go │ └── db/ │ └── firestore.go ├── functions/ # 每个云函数对应独立子目录 │ ├── get-all-employees/ │ │ └── main.go # 原getAllEmployees逻辑,导入internal模块 │ ├── create-employee/ │ │ └── main.go │ ├── update-employee/ │ │ └── main.go │ └── delete-employee/ │ └── main.go ├── terraform/ # Terraform自动化配置目录 │ ├── provider.tf │ ├── variables.tf │ ├── main.tf │ └── outputs.tf └── go.mod
核心调整:将每个云函数拆分为独立目录,公共代码放入
internal(符合Go包管理规范,避免被外部意外导入),每个函数目录下的main.go作为该函数的入口文件。
二、使用gcloud命令部署函数
1. 单个函数部署命令
确保每个函数目录下的main.go正确导入公共模块(注意go.mod中的module路径要匹配),比如:
package p import ( "your-module-path/internal/models" "your-module-path/internal/validations" "your-module-path/internal/db" "net/http" ) // GetAllEmployees 云函数HTTP触发器入口 func GetAllEmployees(w http.ResponseWriter, r *http.Request) { // 原有业务逻辑 }
执行部署命令(以gen2版本为例):
gcloud functions deploy get-all-employees \ --gen2 \ --runtime go121 \ --region us-central1 \ --source ./functions/get-all-employees \ --entry-point GetAllEmployees \ --trigger-http \ --allow-unauthenticated # 生产环境建议移除,改用IAM控制访问
2. 批量部署脚本
编写deploy-all.sh脚本批量部署所有函数:
#!/bin/bash # 函数列表:格式为"函数名:入口函数名" FUNCTIONS=( "get-all-employees:GetAllEmployees" "create-employee:CreateEmployee" "update-employee:UpdateEmployee" "delete-employee:DeleteEmployee" ) REGION="us-central1" RUNTIME="go121" for func in "${FUNCTIONS[@]}"; do IFS=":" read -r NAME ENTRY <<< "$func" echo "开始部署函数:$NAME" gcloud functions deploy "$NAME" \ --gen2 \ --runtime "$RUNTIME" \ --region "$REGION" \ --source "./functions/$NAME" \ --entry-point "$ENTRY" \ --trigger-http done
赋予执行权限后运行:chmod +x deploy-all.sh && ./deploy-all.sh
三、Terraform自动化部署方案
1. 基础配置文件
terraform/provider.tf(GCP提供者配置)
terraform { required_providers { google = { source = "hashicorp/google" version = "~> 4.0" } } } provider "google" { project = var.gcp_project_id region = var.gcp_region }
terraform/variables.tf(定义可配置变量)
variable "gcp_project_id" { type = string description = "你的GCP项目ID" } variable "gcp_region" { type = string description = "云函数部署区域" default = "us-central1" } variable "function_runtime" { type = string description = "Go运行时版本" default = "go121" }
2. 函数资源定义
terraform/main.tf(核心部署逻辑)
# 创建存储桶用于存放函数源码包 resource "google_storage_bucket" "function_source_bucket" { name = "${var.gcp_project_id}-function-sources" location = var.gcp_region uniform_bucket_level_access = true } # 打包并上传所有函数源码到存储桶 resource "null_resource" "package_and_upload_functions" { depends_on = [google_storage_bucket.function_source_bucket] provisioner "local-exec" { command = <<EOT # 打包每个函数目录 zip -r functions/get-all-employees.zip ./functions/get-all-employees -x "*.git*" zip -r functions/create-employee.zip ./functions/create-employee -x "*.git*" zip -r functions/update-employee.zip ./functions/update-employee -x "*.git*" zip -r functions/delete-employee.zip ./functions/delete-employee -x "*.git*" # 上传到GCS存储桶 gsutil cp functions/*.zip gs://${google_storage_bucket.function_source_bucket.name}/functions/ EOT } } # 部署"获取所有员工"函数 resource "google_cloudfunctions2_function" "get_all_employees" { name = "get-all-employees" location = var.gcp_region description = "获取员工列表的云函数" build_config { runtime = var.function_runtime entry_point = "GetAllEmployees" source { storage_source { bucket = google_storage_bucket.function_source_bucket.name object = "functions/get-all-employees.zip" } } } service_config { available_memory = "256MiB" timeout_seconds = 60 ingress_settings = "ALLOW_ALL" all_traffic_on_latest_revision = true } event_trigger { trigger_region = var.gcp_region event_type = "google.cloud.http" http_trigger { security_level = "SECURE_OPTIONAL" } } depends_on = [null_resource.package_and_upload_functions] } # 部署"创建员工"函数(其余update/delete函数逻辑类似,复制后修改名称和入口即可) resource "google_cloudfunctions2_function" "create_employee" { name = "create-employee" location = var.gcp_region description = "创建新员工的云函数" build_config { runtime = var.function_runtime entry_point = "CreateEmployee" source { storage_source { bucket = google_storage_bucket.function_source_bucket.name object = "functions/create-employee.zip" } } } service_config { available_memory = "256MiB" timeout_seconds = 60 ingress_settings = "ALLOW_ALL" all_traffic_on_latest_revision = true } event_trigger { trigger_region = var.gcp_region event_type = "google.cloud.http" http_trigger { security_level = "SECURE_OPTIONAL" } } depends_on = [null_resource.package_and_upload_functions] }
3. 输出函数访问URL
terraform/outputs.tf
output "get_all_employees_url" { value = google_cloudfunctions2_function.get_all_employees.service_config.uri } output "create_employee_url" { value = google_cloudfunctions2_function.create_employee.service_config.uri } # 其余函数URL输出同理添加
4. Terraform部署步骤
- 初始化Terraform:
cd terraform && terraform init - 预览部署计划:
terraform plan -var="gcp_project_id=你的项目ID" - 执行部署:
terraform apply -var="gcp_project_id=你的项目ID"
内容的提问来源于stack exchange,提问作者Shreyas Awankar
相关产品推荐
相关产品推荐

