Skip to content

Commit 7d20313

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

File tree

13 files changed

+202
-0
lines changed

13 files changed

+202
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ module "secure_app" {
118118
- `cloud_build_config` - Custom Cloud Build config file (null)
119119
- `build_args` - Additional build arguments ({})
120120
- `cache_enabled` - Enable branch-based caching (true)
121+
- `tags` - A map of tags to assign to all resources created by this module ({})
121122

122123
## Outputs
123124

@@ -177,6 +178,40 @@ your-app-repo/
177178
└── README.md
178179
```
179180

181+
## Resource Tagging
182+
183+
All resources created by this module are automatically tagged with common metadata:
184+
185+
### Automatic Tags
186+
- `module` - Set to "cloud-build-docker"
187+
- `image_name` - The name of the Docker image being built
188+
189+
### Custom Tags
190+
You can add custom tags using the `tags` variable:
191+
192+
```hcl
193+
module "my_app_image" {
194+
source = "git::https://github.com/Khan/terraform-modules.git//terraform/modules/cloud-build-docker?ref=v1.0.0"
195+
196+
image_name = "my-app"
197+
context_path = "./app"
198+
project_id = var.project_id
199+
image_tag_suffix = "latest"
200+
201+
tags = {
202+
"environment" = "production"
203+
"team" = "backend"
204+
"cost-center" = "engineering"
205+
"owner" = "backend-team"
206+
}
207+
}
208+
```
209+
210+
### Supported Resources
211+
The following resources support tagging/labeling:
212+
- **Cloud Build Triggers** - Labels applied (where supported)
213+
- **Container Images** - Labels applied (where supported)
214+
180215
## Prerequisites
181216

182217
### GCP Setup

terraform/modules/cloud-build-docker/examples/simple-build/main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ module "web_app_image" {
2424
context_path = "./app"
2525
project_id = var.project_id
2626
image_tag_suffix = "latest"
27+
28+
tags = {
29+
environment = "example"
30+
team = "backend"
31+
cost-center = "engineering"
32+
owner = "backend-team"
33+
}
2734
}
2835

2936
# Output the built image information

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ terraform {
1717
}
1818
}
1919

20+
# Common tags for all resources
21+
locals {
22+
common_tags = merge(var.tags, {
23+
"module" = "cloud-build-docker"
24+
"image_name" = var.image_name
25+
})
26+
}
27+
2028
# External data source to build images and return their digests
2129
data "external" "image_build" {
2230
program = ["${path.module}/build_image.py"]

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,9 @@ variable "base_digest" {
3939
type = string
4040
default = "latest"
4141
}
42+
43+
variable "tags" {
44+
description = "A map of tags to assign to all resources created by this module"
45+
type = map(string)
46+
default = {}
47+
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ module "culture_cron_terraform_ci" {
6969
| `terraform_state_bucket` | GCS bucket name for storing Terraform state for this configuration | `string` | `terraform-{org}-{repo}-{service}` | no |
7070
| `secrets_project_id` | Project ID where secrets needed by the Terraform configuration are stored | `string` | `"khan-academy"` | no |
7171
| `secret_ids` | List of secret IDs that the Terraform configuration needs access to | `list(string)` | `[]` | no |
72+
| `tags` | A map of tags to assign to all resources created by this module | `map(string)` | `{}` | no |
7273

7374
### Target Projects Structure
7475

@@ -191,6 +192,41 @@ jobs:
191192
uses: google-github-actions/setup-gcloud@v2
192193
```
193194
195+
## Resource Tagging
196+
197+
All resources created by this module are automatically tagged with common metadata:
198+
199+
### Automatic Tags
200+
- `module` - Set to "github-ci-bootstrap"
201+
- `service_name` - The unique identifier for this Terraform configuration
202+
- `github_repo` - The GitHub repository containing the Terraform code
203+
204+
### Custom Tags
205+
You can add custom tags using the `tags` variable:
206+
207+
```hcl
208+
module "culture_cron_terraform_ci" {
209+
source = "git::https://github.com/Khan/terraform-modules.git//terraform/modules/github-ci-bootstrap?ref=v1.0.0"
210+
211+
service_name = "culture-cron-prod"
212+
github_repository = "Khan/culture-cron"
213+
# ... other configuration
214+
215+
tags = {
216+
"environment" = "production"
217+
"team" = "platform"
218+
"cost-center" = "infrastructure"
219+
"owner" = "platform-team"
220+
}
221+
}
222+
```
223+
224+
### Supported Resources
225+
The following resources support tagging/labeling:
226+
- **Service Accounts** - Labels applied (where supported)
227+
- **Workload Identity Pools** - Labels applied (where supported)
228+
- **Workload Identity Providers** - Labels applied (where supported)
229+
194230
## Security Features
195231

196232
- **No Service Account Keys**: Uses Workload Identity Federation for keyless auth

terraform/modules/github-ci-bootstrap/examples/bootstrap-with-module/main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,11 @@ module "culture_cron_bootstrap" {
4545
secret_ids = [
4646
"projects/${var.secrets_project_id}/secrets/districts_slack_token"
4747
]
48+
49+
tags = {
50+
environment = "production"
51+
team = "platform"
52+
cost-center = "infrastructure"
53+
owner = "platform-team"
54+
}
4855
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ terraform {
1212
required_version = ">= 1.3.0"
1313
}
1414

15+
# Common tags for all resources
16+
locals {
17+
common_tags = merge(var.tags, {
18+
"module" = "github-ci-bootstrap"
19+
"service_name" = var.service_name
20+
"github_repo" = var.github_repository
21+
})
22+
}
23+
1524
# Define service-to-role mapping
1625
locals {
1726
service_roles = {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,10 @@ variable "secret_ids" {
5757
description = "List of secret IDs that the Terraform configuration needs access to"
5858
type = list(string)
5959
default = []
60+
}
61+
62+
variable "tags" {
63+
description = "A map of tags to assign to all resources created by this module"
64+
type = map(string)
65+
default = {}
6066
}

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

0 commit comments

Comments
 (0)