|
| 1 | +/** |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +# [START eventarc_basic_pubsub_parent_tag] |
| 18 | +# [START eventarc_basic_pubsub_enableapis] |
| 19 | +# Enable Cloud Run API |
| 20 | +resource "google_project_service" "run" { |
| 21 | + service = "run.googleapis.com" |
| 22 | + disable_on_destroy = false |
| 23 | +} |
| 24 | + |
| 25 | +# Enable Eventarc API |
| 26 | +resource "google_project_service" "eventarc" { |
| 27 | + service = "eventarc.googleapis.com" |
| 28 | + disable_on_destroy = false |
| 29 | +} |
| 30 | + |
| 31 | +# Enable Pub/Sub API |
| 32 | +resource "google_project_service" "pubsub" { |
| 33 | + service = "pubsub.googleapis.com" |
| 34 | + disable_on_destroy = false |
| 35 | +} |
| 36 | +# [END eventarc_basic_pubsub_enableapis] |
| 37 | + |
| 38 | +# [START eventarc_basic_pubsub_iam] |
| 39 | +# Used to retrieve project information later |
| 40 | +data "google_project" "project" {} |
| 41 | + |
| 42 | +# Create a dedicated service account |
| 43 | +resource "google_service_account" "eventarc" { |
| 44 | + account_id = "eventarc-trigger-sa" |
| 45 | + display_name = "Eventarc trigger service account" |
| 46 | +} |
| 47 | + |
| 48 | +# Grant permission to invoke Cloud Run services |
| 49 | +resource "google_project_iam_member" "runinvoker" { |
| 50 | + project = data.google_project.project.id |
| 51 | + role = "roles/run.invoker" |
| 52 | + member = "serviceAccount:${google_service_account.eventarc.email}" |
| 53 | +} |
| 54 | + |
| 55 | +# Grant permission to publish messages to a Pub/Sub topic |
| 56 | +resource "google_project_iam_member" "pubsubpublisher" { |
| 57 | + project = data.google_project.project.id |
| 58 | + member = "serviceAccount:${google_service_account.eventarc.email}" |
| 59 | + role = "roles/pubsub.publisher" |
| 60 | +} |
| 61 | +# [END eventarc_basic_pubsub_iam] |
| 62 | + |
| 63 | +# [START eventarc_basic_pubsub_deploy_cloud_run] |
| 64 | +# Deploy a Cloud Run service |
| 65 | +resource "google_cloud_run_v2_service" "default" { |
| 66 | + name = "hello-events" |
| 67 | + location = "us-central1" |
| 68 | + |
| 69 | + deletion_protection = false # set to "true" in production |
| 70 | + |
| 71 | + template { |
| 72 | + containers { |
| 73 | + # This container will log received events |
| 74 | + image = "us-docker.pkg.dev/cloudrun/container/hello" |
| 75 | + } |
| 76 | + service_account = google_service_account.eventarc.email |
| 77 | + } |
| 78 | + |
| 79 | + depends_on = [google_project_service.run] |
| 80 | +} |
| 81 | +# [END eventarc_basic_pubsub_deploy_cloud_run] |
| 82 | + |
| 83 | +# [START eventarc_basic_pubsub_topic] |
| 84 | +# Create a Pub/Sub topic |
| 85 | +resource "google_pubsub_topic" "default" { |
| 86 | + name = "pubsub_topic" |
| 87 | +} |
| 88 | +# [END eventarc_basic_pubsub_topic] |
| 89 | + |
| 90 | +# [START eventarc_basic_pubsub_trigger] |
| 91 | +# Create an Eventarc trigger, routing Pub/Sub events to Cloud Run |
| 92 | +resource "google_eventarc_trigger" "default" { |
| 93 | + name = "trigger-pubsub-cloudrun-tf" |
| 94 | + location = google_cloud_run_v2_service.default.location |
| 95 | + |
| 96 | + # Capture messages published to a Pub/Sub topic |
| 97 | + matching_criteria { |
| 98 | + attribute = "type" |
| 99 | + value = "google.cloud.pubsub.topic.v1.messagePublished" |
| 100 | + } |
| 101 | + |
| 102 | + # Send events to Cloud Run |
| 103 | + destination { |
| 104 | + cloud_run_service { |
| 105 | + service = google_cloud_run_v2_service.default.name |
| 106 | + region = google_cloud_run_v2_service.default.location |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + transport { |
| 111 | + pubsub { |
| 112 | + topic = google_pubsub_topic.default.id |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + service_account = google_service_account.eventarc.email |
| 117 | + depends_on = [ |
| 118 | + google_project_service.eventarc, |
| 119 | + google_project_iam_member.pubsubpublisher |
| 120 | + ] |
| 121 | +} |
| 122 | +# [END eventarc_basic_pubsub_trigger] |
| 123 | +# [END eventarc_basic_pubsub_parent_tag] |
0 commit comments