2 min read

Keeping Your Terraform Code DRY with a Reusable Labels Module for GCP

By creating a reusable labels module, you streamline your Terraform workflows and ensure consistent resource organization in GCP. This approach not only saves time but also helps enforce best practices across your infrastructure. Start implementing this module in your projects today
Keeping Your Terraform Code DRY with a Reusable Labels Module for GCP

When managing cloud infrastructure with Terraform, labels (or tags) are essential for organizing resources, applying policies, and controlling costs. However, repeating the same label logic across multiple modules can lead to inefficiencies and inconsistent practices. By encapsulating this logic in a reusable module, you can ensure your Terraform code remains DRY (Don't Repeat Yourself) and maintainable. In this post, we'll walk through how to create and use a reusable labels module for Google Cloud Platform (GCP).

Why Use a Labels Module?

A labels module provides a centralized way to:

  • Standardize labeling conventions across all resources.
  • Reduce code duplication by encapsulating common logic.
  • Simplify updates to labeling schemes.

Instead of redefining label logic in each module, you call the labels module wherever you need it.


Step 1: Create the Labels Module

Start by creating a directory for your labels module, for example, modules/labels. In this directory, define a main.tf file:

# modules/labels/main.tf
variable "environment" {
  description = "The environment for the resource"
  type        = string
}

variable "name" {
  description = "The name of the resource"
  type        = string
}

variable "additional_labels" {
  description = "Additional labels to add to the resource"
  type        = map(string)
  default     = {}
}

output "labels" {
  value = merge(
    {
      "name"        = var.name
      "environment" = var.environment
    },
    var.additional_labels
  )
}

This module accepts an environment name, a resource name, and any additional labels you want to include. It merges these into a unified labels map, ensuring consistent labeling.


Step 2: Use the Labels Module in Other Modules

In any Terraform module where you need labels, call the labels module and pass in the required variables. For example:

# modules/some-other-module/main.tf
module "labels" {
  source           = "../labels"
  environment      = var.environment
  name             = var.name
  additional_labels = var.additional_labels
}

resource "google_compute_instance" "example" {
  name         = "example-instance"
  machine_type = "e2-medium"
  zone         = "us-central1-a"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-10"
    }
  }

  network_interface {
    network = "default"
    access_config {}
  }

  labels = module.labels.labels
}

This example demonstrates applying the labels to a Google Compute Engine instance. The labels are passed dynamically from the labels module.


Step 3: Define Variables in the Parent Module

Ensure your parent module defines the variables required by the labels module. For example:

# modules/some-other-module/variables.tf
variable "environment" {
  description = "The environment for the resource"
  type        = string
}

variable "name" {
  description = "The name of the resource"
  type        = string
}

variable "additional_labels" {
  description = "Additional labels to add to the resource"
  type        = map(string)
  default     = {}
}

Benefits of a Reusable Labels Module

  1. Consistency: A single source of truth for label logic ensures uniformity across your GCP resources.
  2. Simplified Maintenance: Need to update your labeling conventions? Change the logic in one place—the labels module.
  3. Reduced Code Duplication: Save time and effort by reusing the module instead of duplicating code.

Conclusion

By creating a reusable labels module, you streamline your Terraform workflows and ensure consistent resource organization in GCP. This approach not only saves time but also helps enforce best practices across your infrastructure. Start implementing this module in your projects today to make your Terraform code more maintainable and DRY!