简体   繁体   English

是否可以在Terraform中更新GCP Cloud Function的源代码?

[英]Is it possible to update the source code of a GCP Cloud Function in Terraform?

I use Terraform to manage resources of Google Cloud Functions.我使用 Terraform 来管理 Google Cloud Functions 的资源。 But while the inital deployment of the cloud function worked, further deploments with changed cloud function source code (the source archive sourcecode.zip ) were not redeployed when I use terraform apply after updating the source archive.但是,虽然云 function 的初始部署有效,但当我在更新源存档后使用terraform apply时,未重新部署更改云 function 源代码(源存档sourcecode.zip )。

The storage bucket object gets updated but this does not trigger an update/redeployment of the cloud function resource.存储桶 object 得到更新,但这不会触发云 function 资源的更新/重新部署。

Is this an error of the provider?这是提供者的错误吗? Is there a way to redeploy a function in terraform when the code changes?有没有办法在代码变化的时候在terraform重新部署一个function?

The simplified source code I am using:我使用的简化源代码:

resource "google_storage_bucket" "cloud_function_source_bucket" {
  name                        = "${local.project}-function-bucket"
  location                    = local.region
  uniform_bucket_level_access = true
}

resource "google_storage_bucket_object" "function_source_archive" {
  name   = "sourcecode.zip"
  bucket = google_storage_bucket.cloud_function_source_bucket.name
  source = "./../../../sourcecode.zip"
}

resource "google_cloudfunctions_function" "test_function" {
  name                          = "test_func"
  runtime                       = "python39"
  region                        = local.region
  project                       = local.project
  available_memory_mb           = 256
  source_archive_bucket         = google_storage_bucket.cloud_function_source_bucket.name
  source_archive_object         = google_storage_bucket_object.function_source_archive.name
  trigger_http                  = true
  entry_point                   = "trigger_endpoint"
  service_account_email         = google_service_account.function_service_account.email
  vpc_connector                 = "projects/${local.project}/locations/${local.region}/connectors/serverless-main"
  vpc_connector_egress_settings = "ALL_TRAFFIC"
  ingress_settings              = "ALLOW_ALL"
}

You can append MD5 or SHA256 checksum of the content of zip to the bucket object's name.您可以将 zip 的内容的 append MD5 或 SHA256 校验和添加到存储桶对象的名称中。 That will trigger recreation of cloud function whenever source code changes.每当源代码更改时,这将触发云 function 的重新创建。

${data.archive_file.function_src.output_md5} ${data.archive_file.function_src.output_md5}

data "archive_file" "function_src" {
type = "zip"
source_dir = "SOURCECODE_PATH/sourcecode"
output_path = "./SAVING/PATH/sourcecode.zip"
}

resource "google_storage_bucket_object" "function_source_archive" {
name   = "sourcecode.${data.archive_file.function_src.output_md5}.zip"
bucket = google_storage_bucket.cloud_function_source_bucket.name
source = data.archive_file.function_src.output_path
}

You can read more about terraform archive here - terraform archive_file您可以在此处阅读有关 terraform 存档的更多信息 - terraform archive_file

You might consider that as a defect.您可能会认为这是一个缺陷。 Personally, I am not so sure about it.就个人而言,我不太确定。

Terraform has some logic, when an "apply" command is executed.当执行“应用”命令时,Terraform 有一些逻辑。

The question to think about - how does terraform know that the source code of the cloud function is changed, and the cloud function is to be redeployed?思考的问题——terraform如何知道云function的源代码被改变,云function要重新部署? Terraform does not "read" the cloud function source code, does not compare it with the previous version. Terraform不“读”云function源码,不与之前版本对比。 It only reads the terraform's script files.它只读取 terraform 的脚本文件。 And if nothing is changed in those files (in comparison to the state file, and resources existed in GCP projects) - nothing to be redeployed.如果这些文件中没有任何更改(与 state 文件相比,并且 GCP 项目中存在资源)- 无需重新部署。

Therefore, something is to be changed.因此,有些事情要改变。 For example the name of the archive file.例如存档文件的名称。 In that case, terraform finds out that the cloud function has to be redeployed (because the state file has the old name of the archive object).在这种情况下,terraform 发现必须重新部署云 function(因为 state 文件具有存档对象的旧名称)。 The cloud function is redeployed.重新部署云 function。

An example of that code with more detailed explanation, was provided some time ago: don't take into account the question working - just read the answer前段时间提供了一个带有更详细解释的代码示例: don't take into account the question working - just read the answer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 terraform 在 GCP 中创建云功能错误警报策略 - Creating a cloud-function error alerting policy in GCP with terraform Terraform GCP Cloud 代码 DNS A 记录集 - 基于地理的路由 - Terraform code for GCP Cloud DNS A recordset - Geo Based Routing GCP Cloud Build 和 Terraform 集成 - GCP Cloud Build and Terraform integration terraform GCP 云 function 无需通过 terraform 在 CI 中部署或在本地运行时打破过去的部署? - terraform GCP cloud function without having to deploy via terraform in CI or breaking past deployments when running locally? 如何使用 Terraform 删除现有 GCP 云 function 的“允许未经身份验证”标志? - How to remove 'allow unauthenticated' flag of existing GCP cloud function using Terraform? GCP云计费通过terraform导出到BQ - GCP cloud billing export into BQ through terraform 如何使用 terraform 更新 GCP 中的磁盘? - How to update disk in GCP using terraform? apache 光束与 gcp 云 function - apache beam with gcp cloud function 错误:获取存储源时出错:通用::未知...通过 GitHub 操作部署 GCP Cloud Function 时 - ERROR: error fetching storage source: generic::unknown... When deploying GCP Cloud Function via GitHub Actions 无法通过 Terraform 使用 GCP Cloud Build 对 GitHub 存储库进行身份验证 - Cannot authenticate GitHub repository with GCP Cloud Build via Terraform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM