0

i 've this main.tf file :

resource "google_compute_instance" "prod" {
  count = var.is_test == false ? 1 : 0 #condition : if the is_test = false , create 1 instance of vm
  name         = "vm-prod"
  machine_type = "e2-medium"
  zone         = "us-central1-c"
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }
    network_interface {
        network = "default"
    access_config {
    }
  }
}

resource "google_compute_instance" "dev" {
  count = var.is_test == true ? 3 : 0 #condition : if the is_test = true, create 3 instance of vm
  name         = "vm-dev"
  machine_type = "e2-small"
  zone         = "us-central1-b"
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }
   network_interface {
        network = "default"
    access_config {
    }
  }
}

and variables.tf file :

variable "is_test" {
  type = bool
  description = "env"
  default = true
}

i use is_test variable to choose the env, to privsion the vm

now , i want to delete the value of inputs arguments from main.tf and make them in terraform.tfvars .

How i can do that ? How i can make the value of input arguments dynamically based on condition ?

i mean : if the env is dev , the size of vm is small , the region is in us-central1-b ..

if the env is prod , the size of vm is medium , ... Thanks

1
  • 1
    Easiset way would be to have a map defined with those parameters in 2 different files dev.tf and prod.tf. You can keep the respective file for each environment in 2 git branches and checkout before running terraform. A more elegant but more complex approach would be the use of workspaces terraform.io/docs/language/state/workspaces.html Commented Aug 9, 2021 at 14:33

2 Answers 2

2

I would do it this way, so you dont have to repeat the code and tfvars file:

resource "google_compute_instance" "instance" {
  name         = var.is_test == false ? "vm-prod" : "vm-dev"
  machine_type = var.is_test == false ? "e2-medium" : "e2-small"
  zone         = var.is_test == false ? "us-central1-c" : "us-central1-b"
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }
    network_interface {
        network = "default"
    access_config {
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

2

main.tf

resource "google_compute_instance" "my_instance" {
  count = var.instances
  name         = var.name
  machine_type = var.machine_type
  zone         = var.zone
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }
    network_interface {
        network = "default"
    access_config {
    }
  }
}

dev.tfvars

instances = 1
name = "vm-dev"
machine_type="e2-small"
zone="us-central1-b"

prod.tfvars

instances = 3
name = "vm-prod"
machine_type="e2-medium"
zone="us-central1-c"

run the command

terraform apply -var-file="dev.tfvars"
terraform apply -var-file="prod.tfvars"

Documentation: Terraform Input Variables

2 Comments

Thanks for your reply , i already know this method , but i can't do that with condition ?
Yes, you can :) It's more cleaner that way. Based on your example, I prefere use variables on the command line terraform apply -var="is_test=true" or terraform apply -var="is_test=false"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.