Skip to content

Commit c734b8b

Browse files
Add example of Function v2 and Scheduler HTTP trigger with auth (#8169) (#5838)
Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Shuya Ma <[email protected]>
1 parent 13f2948 commit c734b8b

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

.changelog/8169.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:none
2+
3+
```

website/docs/r/cloudfunctions2_function.html.markdown

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,91 @@ resource "google_cloudfunctions2_function" "function" {
148148
}
149149
# [END functions_v2_full]
150150
```
151+
## Example Usage - Cloudfunctions2 Scheduler Auth
152+
153+
154+
```hcl
155+
# [START function_v2_scheduler_auth]
156+
locals {
157+
project = "my-project-name" # Google Cloud Platform Project ID
158+
}
159+
160+
resource "google_service_account" "account" {
161+
account_id = "gcf-sa"
162+
display_name = "Test Service Account"
163+
}
164+
165+
resource "google_storage_bucket" "bucket" {
166+
name = "${local.project}-gcf-source" # Every bucket name must be globally unique
167+
location = "US"
168+
uniform_bucket_level_access = true
169+
}
170+
171+
resource "google_storage_bucket_object" "object" {
172+
name = "function-source.zip"
173+
bucket = google_storage_bucket.bucket.name
174+
source = "function-source.zip" # Add path to the zipped function source code
175+
}
176+
177+
resource "google_cloudfunctions2_function" "function" {
178+
name = "gcf-function" # name should use kebab-case so generated Cloud Run service name will be the same
179+
location = "us-central1"
180+
description = "a new function"
181+
182+
build_config {
183+
runtime = "nodejs16"
184+
entry_point = "helloHttp" # Set the entry point
185+
source {
186+
storage_source {
187+
bucket = google_storage_bucket.bucket.name
188+
object = google_storage_bucket_object.object.name
189+
}
190+
}
191+
}
192+
193+
service_config {
194+
min_instance_count = 1
195+
available_memory = "256M"
196+
timeout_seconds = 60
197+
service_account_email = google_service_account.account.email
198+
}
199+
}
200+
201+
resource "google_cloudfunctions2_function_iam_member" "invoker" {
202+
project = google_cloudfunctions2_function.function.project
203+
location = google_cloudfunctions2_function.function.location
204+
cloud_function = google_cloudfunctions2_function.function.name
205+
role = "roles/cloudfunctions.invoker"
206+
member = "serviceAccount:${google_service_account.account.email}"
207+
}
208+
209+
resource "google_cloud_run_service_iam_member" "cloud_run_invoker" {
210+
project = google_cloudfunctions2_function.function.project
211+
location = google_cloudfunctions2_function.function.location
212+
service = google_cloudfunctions2_function.function.name
213+
role = "roles/run.invoker"
214+
member = "serviceAccount:${google_service_account.account.email}"
215+
}
216+
217+
resource "google_cloud_scheduler_job" "invoke_cloud_function" {
218+
name = "invoke-gcf-function"
219+
description = "Schedule the HTTPS trigger for cloud function"
220+
schedule = "0 0 * * *" # every day at midnight
221+
project = google_cloudfunctions2_function.function.project
222+
region = google_cloudfunctions2_function.function.location
223+
224+
http_target {
225+
uri = google_cloudfunctions2_function.function.service_config[0].uri
226+
http_method = "POST"
227+
oidc_token {
228+
audience = "${google_cloudfunctions2_function.function.service_config[0].uri}/"
229+
service_account_email = google_service_account.account.email
230+
}
231+
}
232+
}
233+
234+
# [END function_v2_scheduler_auth]
235+
```
151236
## Example Usage - Cloudfunctions2 Basic Gcs
152237

153238

0 commit comments

Comments
 (0)