[英]How to deploy the Google Cloud Function via Terraform without using a bucket?
[英]terraform GCP cloud function without having to deploy via terraform in CI or breaking past deployments when running locally?
我在一个较大的项目中有一些有效的 terraform 定义:
resource "google_storage_bucket" "owlee_functions_bucket" {
name = "owlee_functions_bucket"
location = "europe-west2"
project = "owlee-software"
}
resource "google_storage_bucket_object" "archive" {
name = "index.zip"
bucket = google_storage_bucket.owlee_functions_bucket.name
source = "../apps/backend/dist/index.zip"
}
resource "google_cloudfunctions_function" "backend_function" {
name = "backend_function"
runtime = "nodejs16"
project = "owlee-software"
region = "europe-west2"
available_memory_mb = 128
source_archive_bucket = google_storage_bucket.owlee_functions_bucket.name
source_archive_object = google_storage_bucket_object.archive.name
trigger_http = true
entry_point = "OWLEE"
}
然后我尝试通过 CI 进行部署,目前,我只是在压缩新版本的 function 来处理部署后运行terraform apply
。
这不是很好,我想理想地将其更改为非 terraform 过程,但似乎没有任何记录/可能的任何地方,这让我觉得我对此有错误的方法。
第二个更迫切需要解决的问题——
我现在想继续在本地管理我的基础设施,并且不想每次必须在本地运行terraform apply
时都必须 zip 更新 function 的新版本来部署。
有没有办法 - 在创建之后 - 避免通过 terraform 覆盖/上传 function?
我猜这对于 CI 部署的工作来说是有必要的。
我查看了一些其他 SO 线程,但他们正在查看有关云构建和工件注册表的细节。
我建议您通过 terraform 部署云功能,但云功能的 CI 由云构建(也由 terraform 创建)维护我认为这是最合乎逻辑的解决方案,因为 terraform 管理基础设施而不是云功能的实现.
不要像现在这样使用固定名称,而是使用随机字符串或根据需要使用提交 hash 例如。 这可以用其他东西作为前缀,使它更加独特。
resource "random_string" "function" {
length = 8
special = false
keepers = {
commit_hash = var.commit_hash,
environment = var.environment,
}
}
resource "google_storage_bucket_object" "archive" {
name = "index.zip"
bucket = google_storage_bucket.owlee_functions_bucket.name
source = "../apps/backend/dist/${random_string.function.result}.zip"
}
resource "google_cloudfunctions_function" "backend_function" {
name = "backend_function"
runtime = "nodejs16"
project = "owlee-software"
region = "europe-west2"
available_memory_mb = 128
source_archive_bucket = google_storage_bucket.owlee_functions_bucket.name
source_archive_object = google_storage_bucket_object.archive.name
trigger_http = true
entry_point = "OWLEE"
}
这样,如果您每次提供environ.net
(例如 prod)和相同的提交 hash,它将创建相同的 zip 文件。
如果您提供一个新environment
,比如“本地”,它将生成一个新的 zip。然后您可以创建多个函数实例或对google_cloudfunctions_function
进行更多更改,以便它可以与工作区一起使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.