如何使用 Terraform 公开 gcp 云功能

Lau*_* H. 3 google-cloud-platform terraform google-cloud-functions google-iam

我首先要说我对 GCP 和 Terraform 都很陌生,所以我希望有一个我刚刚忽略的简单答案。

我正在尝试创建一个 GCP 云函数,然后使用 Terraform 将其公开。尽管密切关注文档的示例,但我能够创建该函数但不能将其公开:https : //www.terraform.io/docs/providers/google/r/cloudfunctions_function.html

当到达 google_cloudfunctions_function_iam_member 资源时,我收到错误“googleapi: Error 403: Permission 'cloudfunctions.functions.setIamPolicy' denied on resource ...(或资源可能不存在)”。

我怎样才能公开这个功能?它是否与我用于创建所有这些资源的凭据的帐户/api 密钥有关?

提前致谢。

我的 main.tf 文件:

provider "google" {
  project     = "my-project"
  credentials = "key.json" #compute engine default service account api key
  region      = "us-central1"
}

terraform {
  backend "gcs" {
    bucket  = "manually-created-bucket"
    prefix  = "terraform/state"
    credentials = "key.json"
  }
}

# create the storage bucket for our scripts
resource "google_storage_bucket" "source_code" {
  name     = "test-bucket-lh05111992"
  location = "us-central1"
  force_destroy = true
}

# zip up function source code
data "archive_file" "my_function_script_zip" {
 type        = "zip"
 source_dir  = "../source/scripts/my-function-script"
 output_path = "../source/scripts/my-function-script.zip"
}

# add function source code to storage
resource "google_storage_bucket_object" "my_function_script_zip" {
 name   = "index.zip"
 bucket = google_storage_bucket.source_code.name
 source = "../source/scripts/my-function-script.zip"
}

#create the cloudfunction 
resource "google_cloudfunctions_function" "function" {
  name        = "send_my_function_script"
  description = "This function is called in GTM. It sends a users' google analytics id to BigQuery."
  runtime     = "nodejs10"

  available_memory_mb   = 128
  source_archive_bucket = google_storage_bucket.source_code.name
  source_archive_object = google_storage_bucket_object.my_function_script_zip.name
  trigger_http          = true
  entry_point           = "handleRequest"
}

# IAM entry for all users to invoke the function 
resource "google_cloudfunctions_function_iam_member" "invoker" {
  project        = google_cloudfunctions_function.function.project
  region         = "us-central1"
  cloud_function = google_cloudfunctions_function.function.name
  
  role = "roles/cloudfunctions.invoker"
  member = "allUsers"
}
Run Code Online (Sandbox Code Playgroud)

小智 7

似乎 terraform 站点中该示例的唯一问题是自 2019 年 11 月以来已修改的“Cloud Functions IAM 资源”。现在您必须按照此处的说明指定这些资源。现在对于您的用户案例(公共云功能),我建议您遵循此配置,只需将“成员”属性更改为“allUsers”,这样就可以了

resource "google_cloudfunctions_function_iam_binding" "binding" {
  project = google_cloudfunctions_function.function.project
  region = google_cloudfunctions_function.function.region
  cloud_function = google_cloudfunctions_function.function.name
  role = "roles/cloudfunctions.invoker"
  members = [
    "allUsers",
  ]
}
Run Code Online (Sandbox Code Playgroud)

最后,你可以给它一个测试和修改您已经创建的功能这里在#Try这个API”右侧面板,进入这样的合适的资源和请求主体(确保进入‘正确地将资源’参数):

{
  "policy": {
    "bindings": [
      {
        "members": [
          "allUsers"
        ],
        "role": "roles/cloudfunctions.invoker"
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)