【问题标题】:terraform GCP cloud function without having to deploy via terraform in CI or breaking past deployments when running locally?terraform GCP 云功能无需通过 CI 中的 terraform 部署或在本地运行时打破过去的部署?
【发布时间】:2022-11-19 08:19:45
【问题描述】:

我在一个更大的项目中有一些有效的地形定义:

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 进行部署,目前,我只是在压缩新版本的函数以处理部署后运行terraform apply

这不是很好,我想理想地将其更改为非 terraform 过程,但似乎没有任何记录/可能的任何地方,这让我觉得我对此有错误的方法。

第二个更迫切需要解决的问题——

我现在想继续在本地管理我的基础设施,不想每次必须在本地运行 terraform apply 时都必须压缩新版本的功能才能部署。

有没有办法 - 在创建之后 - 避免通过 terraform 覆盖/上传函数?

我猜这对于 CI 部署的工作来说是有必要的。

我已经查看了一些其他 SO 线程,但他们正在查看有关云构建和工件注册表的细节。

【问题讨论】:

    标签: google-cloud-functions terraform terraform-provider-gcp infrastructure-as-code


    【解决方案1】:

    我建议您通过 terraform 部署云函数,但云函数的 CI 由云构建(也由 terraform 创建)维护我认为这是最合乎逻辑的解决方案,因为 terraform 管理基础设施而不是云函数的实现.

    【讨论】:

    • 这确实是我正在尝试做的——但目前可用的 terraform 配置和文档不建议 <how> 去做或根本不可能。
    • 是的,您可以创建云功能和云构建。云构建部署来自云功能代码的任何更新的位置。如果你愿意,我可以为你创建一个简单的例子。
    • 我会永远感激
    【解决方案2】:

    不要像现在这样使用固定名称,而是使用随机字符串或根据需要使用提交哈希。这可以用其他东西作为前缀,使它更加独特。

    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"
    }
    

    这样,如果您每次都提供一个 environmnet(例如 prod)和相同的提交哈希,它将创建相同的 zip 文件。

    如果你提供一个新的environment,比如“local”,它会生成一个新的 zip。然后您可以创建多个函数实例或对 google_cloudfunctions_function 进行更多更改,以便它可以与工作区一起使用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-01
      • 2020-10-16
      • 2021-04-26
      • 1970-01-01
      • 2022-01-25
      • 2020-11-02
      • 1970-01-01
      • 2022-12-21
      相关资源
      最近更新 更多