Terraform部署Google Cloud Functions时是否自动构建?如何禁用?
问题:GCP Cloud Functions Terraform部署时重复执行依赖安装的问题
我使用Terraform部署Node.js版本的GCP Cloud Functions,配置代码如下:
resource "google_cloudfunctions_function" "identity" { name = "${var.stage}-${local.app_name}-identity" description = "GCP Identity" runtime = "nodejs16" available_memory_mb = 512 source_archive_bucket = google_storage_bucket.bucket.name source_archive_object = google_storage_bucket_object.archive.name trigger_http = true entry_point = "addUserRoles" }
执行terraform apply时出现如下错误:
google_cloudfunctions_function.identity: Still creating... [40s elapsed] google_cloudfunctions_function.identity: Still creating... [50s elapsed] google_cloudfunctions_function.identity: Still creating... [1m0s elapsed] google_cloudfunctions_function.identity: Still creating... [1m10s elapsed] ╷ │ Error: Error waiting for Creating CloudFunctions Function: Error code 3, message: Build failed: yarn install v1.22.19 │ [1/5] Validating package.json... │ [2/5] Resolving packages... │ [3/5] Fetching packages... │ error An unexpected error occurred: "https://npco.jfrog.io/artifactory/api/npm/npm-dev-local/@company/eslint-config/-/@company/eslint-config-1.0.5.tgz: Request failed \"401 Unauthorized\"". │ info If you think this is a bug, please open a bug report with the information provided in "/workspace/yarn-error.log". │ info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.; Error ID: 12c8ef4a │ │ with google_cloudfunctions_function.identity, │ on function.tf line 17, in resource "google_cloudfunctions_function" "identity": │ 17: resource "google_cloudfunctions_function" "identity" { │
我已经完成代码构建并把产物上传到指定存储桶,为什么Terraform部署时还会自动执行yarn install?如何禁用这个自动构建?
原因分析
GCP Cloud Functions的部署逻辑中,若上传的压缩包未包含node_modules目录,或存在package.json但未提前完成依赖安装,GCP的构建系统会自动触发依赖安装流程。你的情况是因为上传的包未包含完整依赖,导致GCP尝试重新拉取依赖,而私有Artifactory源需要权限,因此报错。
解决方法:禁用自动构建
在Terraform的google_cloudfunctions_function资源中添加skip_source_validation = true参数,同时确保上传到存储桶的是已完成构建、包含所有依赖(node_modules)的完整产物包。
修改后的配置如下:
resource "google_cloudfunctions_function" "identity" { name = "${var.stage}-${local.app_name}-identity" description = "GCP Identity" runtime = "nodejs16" available_memory_mb = 512 source_archive_bucket = google_storage_bucket.bucket.name source_archive_object = google_storage_bucket_object.archive.name trigger_http = true entry_point = "addUserRoles" # 禁用GCP自动构建与源码校验 skip_source_validation = true }
额外注意事项
- 上传的压缩包必须包含
node_modules目录,且所有依赖(包括私有源依赖)已在本地提前安装完成 - 确保压缩包的目录结构正确,入口函数所在文件处于包的根目录或符合Cloud Functions的路径要求
内容的提问来源于stack exchange,提问作者Joey Yi Zhao
相关产品推荐
相关产品推荐

