Skip to content

Commit a5ebf9a

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

File tree

6 files changed

+83
-1
lines changed

6 files changed

+83
-1
lines changed

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

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

terraform/modules/scheduled-job/README.md

Lines changed: 37 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,42 @@ 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 Functions** - Labels applied
427+
- **Cloud Run Jobs** - Labels applied
428+
392429
## Common Cron Patterns
393430

394431
| 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+
"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")
@@ -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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,9 @@ 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+
}

0 commit comments

Comments
 (0)