使用Terraform在pubsub消息到达时运行云函数

huangapple go评论68阅读模式
英文:

Run cloud function on pubsub message arrival using Terraform

问题

### 需求 ###
需要创建Terraform IAC以在pubsub事件触发时运行云函数。
我不需要在我的IAC中创建pubsub资源,因为它是由其他团队创建的。

### 我的尝试1 ###

      event_trigger {
          event_type    = "google.pubsub.topic.publish"
          resource   = "project/${var.project_id}/topics/${var.environment}_my_topic"
      }

尝试1出错

    │ 错误:googleapi:错误400:请求存在错误
    │ 详情:
    │ [
    │   {
    │     "@type": "type.googleapis.com/google.rpc.BadRequest",
    │     "fieldViolations": [
    │       {
    │         "description": "Pub/Sub主题名称projects/myprojectID/topics/project/myprojectID/topics/my_topic与预期模式不匹配",
    │         "field": "pubsub_topic"
    │       }
    │     ]
    │   }
    │ ]
    │ ,badRequest

### 我的尝试2 ###

      event_trigger {
          event_type    = "google.pubsub.topic.publish"
          resource = {
            service = "pubsub.googleapis.com"
            name = "project/${var.project_id}/topics/${var.environment}_my_topic"
            type = "type.googleapis.com/google.pubsub.v1.PubsubMessage"
            }
      }

尝试2出错 

    │ var.environment是一个字符串,只能在应用后知道
    │ var.project_id是一个字符串,只能在应用后知道
    │ 对于属性"resource"的值不合适:需要一个字符串。
      
请帮忙解决。
英文:

Requirement

Need to create Terraform IAC to run cloud function on pubsub event_trigger.
I don't need to create the pubsub resources in my iac because it was created by other team.

My try 1

  event_trigger {
      event_type    = "google.pubsub.topic.publish"
      resource   = "project/${var.project_id}/topics/${var.environment}_my_topic"
  }

error on try1

│ Error: googleapi: Error 400: The request has errors
│ Details:
│ [
│   {
│     "@type": "type.googleapis.com/google.rpc.BadRequest",
│     "fieldViolations": [
│       {
│         "description": "Pub/Sub topic name projects/myprojectID/topics/project/myprojectID/topics/my_topic does not match the expected pattern",
│         "field": "pubsub_topic"
│       }
│     ]
│   }
│ ]
│ , badRequest

My try 2

  event_trigger {
      event_type    = "google.pubsub.topic.publish"
      resource = {
        service = "pubsub.googleapis.com"
        name = "project/${var.project_id}/topics/${var.environment}_my_topic"
        type = "type.googleapis.com/google.pubsub.v1.PubsubMessage"
        }
  }

error on try2

│     │ var.environment is a string, known only after apply
│     │ var.project_id is a string, known only after apply
│ 
│ Inappropriate value for attribute "resource": string required.

Please help

答案1

得分: 0

我已更正资源路径。

应该以 projects/... 开头。

我的尝试1已更改如下,并且运行正常。

event_trigger {
    event_type = "google.pubsub.topic.publish"
    resource = "projects/${var.project_id}/topics/${var.environment}_my_topic"
}
英文:

I have corrected the resource path.

It should be starting with projects/...

My try1 has been changed like this and it worked.

  event_trigger {
      event_type    = "google.pubsub.topic.publish"
      resource   = "projects/${var.project_id}/topics/${var.environment}_my_topic"
  }

huangapple
  • 本文由 发表于 2023年6月22日 20:12:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定