Sobes.tech
Middle

Wat is de prioriteit van het gebruik van variabelen in Terraform?

sobes.tech AI

Antwoord van AI

Prioriteit bij het gebruik van variabelen in Terraform: van hoog naar laag:

  1. Omgevingsvariabelen.
  2. Commandoregel (parameters -var of -var-file).
  3. Variabelbestanden (terraform.tfvars, *.auto.tfvars, terraform.tfvars.json, *.auto.tfvars.json).
  4. Variabelen gedeclareerd in bestanden variables.tf of *.tf in de variable-blok, zonder overschrijving. In dit geval wordt de waarde default gebruikt als die is opgegeven.

Voorbeeld met omgevingsvariabele:

# Omgevingsvariabele instellen
export TF_VAR_instance_type="t2.medium"

Voorbeeld met commandoregel:

# Variabele direct doorgeven
terraform apply -var="instance_type=t2.large"

# Variabelen uit bestand doorgeven
terraform apply -var-file="dev.tfvars"

Voorbeeld van variabelenbestand terraform.tfvars:

# Variabele in het bestand terraform.tfvars
instance_type = "t2.small"

Voorbeeld van variabele declaratie in variables.tf:

variable "instance_type" {
  description = "EC2 instantie type"
  type        = string
  default     = "t2.micro" # Standaardwaarde
}