Skip to content

Commit c590759

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

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

terraform/modules/scheduled-job/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ module "data_processor" {
221221
- `main_file` - Python file name (e.g., "main.py"), relative to `source_dir`.
222222
- `schedule` - Cron expression (e.g., "0 9 * * 1-5")
223223
- `description` - Function/job description
224+
- `owner` - The owner/team responsible for this scheduled job
224225

225226
### Optional (with defaults)
226227
- `execution_type` - "function" or "job" ("function")
@@ -230,6 +231,7 @@ module "data_processor" {
230231
- `timeout_seconds` - Timeout for functions (60)
231232
- `environment_variables` - Environment vars ({})
232233
- `secrets` - Secret Manager secrets ([])
234+
- `tags` - A map of tags to assign to all resources ({})
233235

234236
### Cloud Run Job specific (when `execution_type = "job"`)
235237
- `job_cpu` - CPU allocation (e.g., "1000m", "2") ("1000m")
@@ -389,6 +391,42 @@ Or use Cloud Build directly:
389391
gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/YOUR_JOB_NAME:latest ./jobs/your-job
390392
```
391393

394+
## Resource Tagging
395+
396+
All resources created by this module are automatically tagged with common metadata:
397+
398+
### Automatic Tags
399+
- `terraform_module` - Set to "scheduled-job"
400+
- `scheduled_job_name` - The name of your function/job
401+
- `owner` - The owner/team responsible for this scheduled job
402+
403+
### Custom Tags
404+
You can add custom tags using the `tags` variable:
405+
406+
```hcl
407+
module "my_function" {
408+
source = "git::https://github.com/Khan/terraform-modules.git//terraform/modules/scheduled-job?ref=v1.0.0"
409+
410+
job_name = "my-function"
411+
# ... other configuration
412+
413+
tags = {
414+
"environment" = "production"
415+
"team" = "data-engineering"
416+
"cost-center" = "infrastructure"
417+
"owner" = "data-team"
418+
}
419+
}
420+
```
421+
422+
### Supported Resources
423+
The following resources support tagging/labeling:
424+
- **Storage Buckets** - Labels applied
425+
- **Storage Objects** - Metadata applied
426+
- **PubSub Topics** - 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: 18 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+
"terraform_module" = "scheduled-job"
24+
"scheduled_job_name" = var.job_name
25+
"owner" = var.owner
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")
@@ -104,6 +119,8 @@ resource "google_cloudfunctions2_function" "function" {
104119
description = var.description
105120
location = var.region
106121

122+
labels = local.common_tags
123+
107124
build_config {
108125
runtime = var.runtime
109126
entry_point = var.entry_point
@@ -159,6 +176,7 @@ resource "google_cloud_run_v2_job" "job" {
159176
project = var.project_id
160177
name = var.job_name
161178
location = var.region
179+
labels = local.common_tags
162180

163181
lifecycle {
164182
precondition {

terraform/modules/scheduled-job/variables.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,14 @@ variable "job_image" {
194194
type = string
195195
default = null
196196
}
197+
198+
variable "tags" {
199+
description = "A map of tags to assign to all resources created by this module"
200+
type = map(string)
201+
default = {}
202+
}
203+
204+
variable "owner" {
205+
description = "The owner/team responsible for this scheduled job"
206+
type = string
207+
}

0 commit comments

Comments
 (0)