Skip to content

Commit b51d236

Browse files
committed
[resource-tagging] Add tags or labels to all resource created with shared modules
1 parent 379027c commit b51d236

File tree

9 files changed

+96
-1
lines changed

9 files changed

+96
-1
lines changed

terraform/modules/cloud-build-docker/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ your-app-repo/
177177
└── README.md
178178
```
179179

180+
181+
182+
180183
## Prerequisites
181184

182185
### GCP Setup

terraform/modules/cloud-build-docker/variables.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ variable "base_digest" {
3939
type = string
4040
default = "latest"
4141
}
42+

terraform/modules/github-ci-bootstrap/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ jobs:
191191
uses: google-github-actions/setup-gcloud@v2
192192
```
193193
194+
195+
194196
## Security Features
195197
196198
- **No Service Account Keys**: Uses Workload Identity Federation for keyless auth

terraform/modules/github-ci-bootstrap/variables.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ variable "secret_ids" {
5757
description = "List of secret IDs that the Terraform configuration needs access to"
5858
type = list(string)
5959
default = []
60-
}
60+
}
61+

terraform/modules/scheduled-job/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ module "data_processor" {
230230
- `timeout_seconds` - Timeout for functions (60)
231231
- `environment_variables` - Environment vars ({})
232232
- `secrets` - Secret Manager secrets ([])
233+
- `tags` - A map of tags to assign to all resources ({})
233234

234235
### Cloud Run Job specific (when `execution_type = "job"`)
235236
- `job_cpu` - CPU allocation (e.g., "1000m", "2") ("1000m")
@@ -389,6 +390,43 @@ Or use Cloud Build directly:
389390
gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/YOUR_JOB_NAME:latest ./jobs/your-job
390391
```
391392

393+
## Resource Tagging
394+
395+
All resources created by this module are automatically tagged with common metadata:
396+
397+
### Automatic Tags
398+
- `module` - Set to "scheduled-job"
399+
- `job_name` - The name of your function/job
400+
- `execution_type` - Either "function" or "job"
401+
402+
### Custom Tags
403+
You can add custom tags using the `tags` variable:
404+
405+
```hcl
406+
module "my_function" {
407+
source = "git::https://github.com/Khan/terraform-modules.git//terraform/modules/scheduled-job?ref=v1.0.0"
408+
409+
job_name = "my-function"
410+
# ... other configuration
411+
412+
tags = {
413+
"environment" = "production"
414+
"team" = "data-engineering"
415+
"cost-center" = "infrastructure"
416+
"owner" = "data-team"
417+
}
418+
}
419+
```
420+
421+
### Supported Resources
422+
The following resources support tagging/labeling:
423+
- **Storage Buckets** - Labels applied
424+
- **Storage Objects** - Metadata applied
425+
- **PubSub Topics** - Labels applied
426+
- **Cloud Scheduler Jobs** - Labels applied
427+
- **Cloud Functions** - Labels applied
428+
- **Cloud Run Jobs** - Labels applied
429+
392430
## Common Cron Patterns
393431

394432
| Schedule | Description |

terraform/modules/scheduled-job/examples/simple-function/main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ module "daily_health_check" {
4848
version = "latest"
4949
}
5050
]
51+
52+
tags = {
53+
environment = "example"
54+
team = "platform"
55+
cost-center = "infrastructure"
56+
owner = "platform-team"
57+
}
5158
}
5259

5360
# Output the function details

terraform/modules/scheduled-job/examples/simple-job/main.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ module "daily_data_processor_image" {
3232
context_path = "./job-code"
3333
project_id = var.project_id
3434
image_tag_suffix = "latest"
35+
36+
tags = {
37+
environment = "example"
38+
team = "data-engineering"
39+
cost-center = "infrastructure"
40+
owner = "data-team"
41+
}
3542
}
3643

3744
# Simple daily job example
@@ -69,6 +76,13 @@ module "daily_data_processor" {
6976
version = "latest"
7077
}
7178
]
79+
80+
tags = {
81+
environment = "example"
82+
team = "data-engineering"
83+
cost-center = "infrastructure"
84+
owner = "data-team"
85+
}
7286
}
7387

7488
# Output the job details

terraform/modules/scheduled-job/main.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
2130
resource "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
}

terraform/modules/scheduled-job/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,9 @@ variable "job_image" {
194194
type = string
195195
}
196196

197+
variable "tags" {
198+
description = "A map of tags to assign to all resources created by this module"
199+
type = map(string)
200+
default = {}
201+
}
202+

0 commit comments

Comments
 (0)