@@ -17,6 +17,15 @@ terraform {
1717 }
1818}
1919
20+ # Common tags for all resources
21+ locals {
22+ common_tags = merge (var. tags , {
23+ " module" = " scheduled-job"
24+ " job_name" = var.job_name
25+ " execution_type" = var.execution_type
26+ })
27+ }
28+
2029# Service account for the Cloud Function/Job
2130resource "google_service_account" "function_sa" {
2231 project = var. project_id
@@ -34,6 +43,8 @@ resource "google_storage_bucket" "function_bucket" {
3443 location = var. region
3544 uniform_bucket_level_access = true
3645 force_destroy = true
46+
47+ labels = local. common_tags
3748}
3849
3950# Create function source archive (only for Cloud Functions)
@@ -57,6 +68,8 @@ resource "google_storage_bucket_object" "function_archive" {
5768 name = " ${ var . job_name } -function-${ data . archive_file . function_archive [0 ]. output_sha } .zip"
5869 bucket = google_storage_bucket. function_bucket [0 ]. name
5970 source = data. archive_file . function_archive [0 ]. output_path
71+
72+ metadata = local. common_tags
6073}
6174
6275# PubSub topic for triggering the Cloud Function (only created when execution_type is "function")
@@ -65,6 +78,8 @@ resource "google_pubsub_topic" "function_topic" {
6578
6679 project = var. project_id
6780 name = " ${ var . job_name } -topic"
81+
82+ labels = local. common_tags
6883}
6984
7085# Cloud Scheduler job for Cloud Function (only created when execution_type is "function")
@@ -81,6 +96,8 @@ resource "google_cloud_scheduler_job" "function_scheduler" {
8196 topic_name = google_pubsub_topic. function_topic [0 ]. id
8297 data = base64encode (" {}" )
8398 }
99+
100+ labels = local. common_tags
84101}
85102
86103# Secret Manager IAM bindings for each secret
@@ -104,6 +121,8 @@ resource "google_cloudfunctions2_function" "function" {
104121 description = var. description
105122 location = var. region
106123
124+ labels = local. common_tags
125+
107126 build_config {
108127 runtime = var. runtime
109128 entry_point = var. entry_point
@@ -160,6 +179,8 @@ resource "google_cloud_run_v2_job" "job" {
160179 name = var. job_name
161180 location = var. region
162181
182+ labels = local. common_tags
183+
163184 template {
164185 task_count = var. job_task_count
165186 parallelism = var. job_parallelism
@@ -232,4 +253,6 @@ resource "google_cloud_scheduler_job" "job_scheduler" {
232253 service_account_email = google_service_account. function_sa . email
233254 }
234255 }
256+
257+ labels = local. common_tags
235258}
0 commit comments