Skip to content

Understand Terraform Variables with practice

One key feature that contributes to the power and versatility of Terraform is the use of variables. In this article, we will explore the benefits of using variables in Terraform configurations and how they enhance the flexibility, maintainability, and reusability of your infrastructure code.

Objectives:

  • Understand the concept of variables.

Prerequisite

Tasks

Create the variable.tf file and use variables.

Code

In terminal, create two files names vm.tf and variables.tf

touch vm.tf variables.tf

Declare variables in variables.tf file

variable "project_id" {
  description = "this is the project id"
  type = string
  default = "Your Project ID
}

variable "region" {
  description = "this is the project region"
  type = string
  default = "europe-west1"
}

variable "zone" {
  description = "this is the project zone"
  type = string
  default =  "europe-west1-c"
}

Replace hard type with variables in vm.tf file

# declare the provider
provider "google" {
    project  = var.project_id
    region  = var.region
    zone     = var.zone
}

# declare the resource
resource "google_compute_instance" "vm_instance" {
  name         = "terraform-instance"
  machine_type = "e2-micro"

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

  network_interface {
    # A default network is created for all GCP projects
    network = "default"
    access_config {
    }
  }
}

Practices

By grasping the concept of variables, we can significantly enhance the efficiency of our configurations. For instance, we have the ability to:

  1. Deploy multiple Virtual Machines (VMs) in the same region, each with distinct names, tailoring infrastructure to specific requirements.
  2. Instantiate multiple VMs with identical names but within distinct environments, allowing for streamlined management and organization across diverse settings.
  3. Implement a diverse VM deployment strategy by creating instances with varying names and allocating them to different availability zones. This flexibility enables us to optimize resource distribution and enhance the resilience of our infrastructure.

Benefits of Variables

Input Validation:

  • Terraform’s support for variable types (e.g., string, number, list) enables input validation, helping catch errors early in development and ensuring correct variable usage throughout configurations.

Simplified Maintenance:

  • Variables ease the process of updating configurations by allowing adjustments to variable values without modifying the entire codebase. This reduces the risk of introducing errors during maintenance.
  • Centralizing configuration details using variables ensures consistency across infrastructure definitions, reducing the risk of errors resulting from disparate settings in different parts of the code.

Parameterization for Customization:

  • Variables in Terraform allow for the parameterization of configurations, enabling users to customize values without modifying the underlying infrastructure code.
  • This flexibility is particularly valuable for adapting configurations to different environments, regions, or instances.

Modularity and Code Reusability:

  • Variables facilitate the creation of modular and reusable Terraform modules. By defining variables within modules, configurations become adaptable to various use cases.
  • This approach promotes code reuse across projects, reducing duplication and ensuring consistency in infrastructure definitions.