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:
- Omgevingsvariabelen.
- Commandoregel (parameters
-varof-var-file). - Variabelbestanden (
terraform.tfvars,*.auto.tfvars,terraform.tfvars.json,*.auto.tfvars.json). - Variabelen gedeclareerd in bestanden
variables.tfof*.tfin devariable-blok, zonder overschrijving. In dit geval wordt de waardedefaultgebruikt 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
}