Skip to content

Commit 197c450

Browse files
committed
Add cloud-run push subscription
1 parent 2156e75 commit 197c450

File tree

14 files changed

+302
-2
lines changed

14 files changed

+302
-2
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Simple Example
2+
3+
This example illustrates how to use the `pub` and `sub` module.
4+
5+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
6+
## Inputs
7+
8+
| Name | Description | Type | Default | Required |
9+
|------|-------------|------|---------|:--------:|
10+
| project\_id | The project ID to manage the Pub/Sub resources | `string` | n/a | yes |
11+
12+
## Outputs
13+
14+
| Name | Description |
15+
|------|-------------|
16+
| project\_id | The project ID |
17+
| topic\_labels | The labels of the Pub/Sub topic created |
18+
| topic\_name | The name of the Pub/Sub topic created |
19+
20+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
21+
22+
## Requirements
23+
24+
The following sections describe the requirements which must be met in
25+
order to invoke this example. The requirements of the
26+
[root module][root-module-requirements] must be met.
27+
28+
## Usage
29+
30+
To provision this example, populate `terraform.tfvars` with the [required variables](#inputs) and run the following commands within
31+
this directory:
32+
- `terraform init` to get the plugins
33+
- `terraform plan` to see the infrastructure plan
34+
- `terraform apply` to apply the infrastructure build
35+
- `terraform destroy` to destroy the built infrastructure
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright 2018 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+
module "pub" {
18+
source = "terraform-google-modules/pubsub/google//modules/pub"
19+
version = "~> 7.0"
20+
21+
project_id = var.project_id
22+
topic = "cft-tf-pub-topic-cr-push"
23+
topic_labels = {
24+
foo_label = "foo_value"
25+
bar_label = "bar_value"
26+
}
27+
}
28+
29+
module "sub" {
30+
source = "terraform-google-modules/pubsub/google//modules/sub"
31+
version = "~> 7.0"
32+
33+
project_id = var.project_id
34+
topic = module.pub.topic
35+
36+
push_subscriptions = [
37+
{
38+
name = module.cloud-run.service_name
39+
push_endpoint = module.cloud-run.service_uri
40+
oidc_service_account_email = module.cloud-run.service_account_id.email
41+
},
42+
]
43+
}
44+
45+
module "cloud-run" {
46+
source = "GoogleCloudPlatform/cloud-run/google//modules/v2"
47+
version = "~> 0.17"
48+
project_id = var.project_id
49+
location = "us-central1"
50+
service_name = "cr-service"
51+
containers = [{ "container_name" = "", "container_image" = "gcr.io/design-center-container-repo/pubsub-cr-push:latest-1703" }]
52+
service_account_project_roles = ["roles/run.invoker"]
53+
members = ["allUsers"]
54+
cloud_run_deletion_protection = false
55+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright 2018 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+
output "project_id" {
18+
value = var.project_id
19+
description = "The project ID"
20+
}
21+
22+
output "topic_name" {
23+
value = module.pub.topic
24+
description = "The name of the Pub/Sub topic created"
25+
}
26+
27+
output "topic_labels" {
28+
value = module.pub.topic_labels
29+
description = "The labels of the Pub/Sub topic created"
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright 2018 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+
variable "project_id" {
18+
type = string
19+
description = "The project ID to manage the Pub/Sub resources"
20+
}

metadata.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ spec:
4646
location: examples/cloud_storage-separate-pub-sub
4747
- name: kms
4848
location: examples/kms
49+
- name: push_subscription-separate-pub-sub
50+
location: examples/push_subscription-separate-pub-sub
4951
- name: simple
5052
location: examples/simple
5153
- name: simple-separate-pub-sub
@@ -243,12 +245,19 @@ spec:
243245
- roles/resourcemanager.projectIamAdmin
244246
- roles/bigquery.admin
245247
- roles/storage.admin
248+
- roles/run.admin
249+
- roles/iam.serviceAccountAdmin
250+
- roles/iam.serviceAccountUser
251+
- roles/resourcemanager.projectIamAdmin
252+
- roles/logging.viewer
246253
services:
247254
- cloudresourcemanager.googleapis.com
248255
- pubsub.googleapis.com
249256
- serviceusage.googleapis.com
250257
- bigquery.googleapis.com
251258
- storage.googleapis.com
259+
- run.googleapis.com
260+
- iam.googleapis.com
252261
providerVersions:
253262
- source: hashicorp/google
254263
version: ">= 6.2, < 7"

modules/pub/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ module "pub" {
4141
|------|-------------|------|---------|:--------:|
4242
| message\_storage\_policy | A map of storage policies. Default - inherit from organization's Resource Location Restriction policy. | `map(any)` | `{}` | no |
4343
| project\_id | The project ID to manage the Pub/Sub resources. | `string` | n/a | yes |
44+
| publisher\_service\_accounts | Service account email which required roles/pubsub.publisher role. | <pre>list(object({<br> id = string<br> service_account = string<br> }))</pre> | `[]` | no |
4445
| schema | Schema for the topic. | <pre>object({<br> name = string<br> type = string<br> definition = string<br> encoding = string<br> })</pre> | `null` | no |
4546
| topic | The Pub/Sub topic name. | `string` | n/a | yes |
4647
| topic\_kms\_key\_name | The resource name of the Cloud KMS CryptoKey to be used to protect access to messages published on this topic. | `string` | `null` | no |

modules/pub/main.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,12 @@ resource "google_pubsub_topic" "topic" {
4545
}
4646
depends_on = [google_pubsub_schema.schema]
4747
}
48+
49+
resource "google_pubsub_topic_iam_member" "sa_binding_publisher" {
50+
for_each = { for i in var.publisher_service_accounts : i.id => i if i.service_account != null }
51+
52+
project = var.project_id
53+
topic = var.topic
54+
role = "roles/pubsub.publisher"
55+
member = "serviceAccount:${each.value.service_account}"
56+
}

modules/pub/metadata.display.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ spec:
3737
project_id:
3838
name: project_id
3939
title: Project Id
40+
publisher_service_accounts:
41+
name: publisher_service_accounts
42+
title: Publisher Service Accounts
4043
schema:
4144
name: schema
4245
title: Schema

modules/pub/metadata.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ spec:
4242
location: examples/cloud_storage-separate-pub-sub
4343
- name: kms
4444
location: examples/kms
45+
- name: push_subscription-separate-pub-sub
46+
location: examples/push_subscription-separate-pub-sub
4547
- name: simple
4648
location: examples/simple
4749
- name: simple-separate-pub-sub
@@ -81,6 +83,20 @@ spec:
8183
definition = string
8284
encoding = string
8385
})
86+
- name: publisher_service_accounts
87+
description: Service account email which required roles/pubsub.publisher role.
88+
varType: |-
89+
list(object({
90+
id = string
91+
service_account = string
92+
}))
93+
defaultValue: []
94+
connections:
95+
- source:
96+
source: github.com/GoogleCloudPlatform/terraform-google-cloud-run//modules/v2
97+
version: ">= 0.13"
98+
spec:
99+
outputExpr: "{ \"id\": service_account_id.id, \"service_account\": service_account_id.email }"
84100
outputs:
85101
- name: id
86102
description: The ID of the Pub/Sub topic
@@ -98,12 +114,19 @@ spec:
98114
- roles/resourcemanager.projectIamAdmin
99115
- roles/bigquery.admin
100116
- roles/storage.admin
117+
- roles/run.admin
118+
- roles/iam.serviceAccountAdmin
119+
- roles/iam.serviceAccountUser
120+
- roles/resourcemanager.projectIamAdmin
121+
- roles/logging.viewer
101122
services:
102123
- cloudresourcemanager.googleapis.com
103124
- pubsub.googleapis.com
104125
- serviceusage.googleapis.com
105126
- bigquery.googleapis.com
106127
- storage.googleapis.com
128+
- run.googleapis.com
129+
- iam.googleapis.com
107130
providerVersions:
108131
- source: hashicorp/google
109132
version: ">= 6.2, < 7"

modules/pub/variables.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,12 @@ variable "schema" {
5858
description = "Schema for the topic."
5959
default = null
6060
}
61+
62+
variable "publisher_service_accounts" {
63+
type = list(object({
64+
id = string
65+
service_account = string
66+
}))
67+
description = "Service account email which required roles/pubsub.publisher role."
68+
default = []
69+
}

0 commit comments

Comments
 (0)