tf_variables - henk52/knowledgesharing GitHub Wiki

TF

Comments

  • #single line comment
  • /* ... */ multiline comment.

while sync is running

Terraform variables

  • Input variables
  • output variables
  • local values

Input variables

  • allowing customaization without changing the modules source code.

  • provide when prompted during plan or apply

  • override default values using the cli arg -var

  • override default values using environment variables TF_var_aa

  • using terraform.tvars file

  • using -var-file <ANY_NAME>.tvars

  • using auto.tfvars

  • complex type constructors like list and map in input variables

  • custom validation rules

  • protect sensitive input variables.

Data sources

  • Allow data to be fetched or computed for use elsewhere in the tf conf.
    • Allow tf conf to make us or information
      • defined outside tf
      • defined by another separate tf conf
  • Each data resource is associated with a single data source
  • Data resources have the same dependnecy dresolution behavior as defined for managed resources.
    • Settin the depends_on meta-argument within a data block, defers reading ot he dat source until after all changes to the dependencies have been applied.
  • Support
    • count
    • for_each

Output values

  • are like return values of a terraform module
    • A root module can use outputs to print specified values in the CLI output after running terraform apply.
    • A sub module can use outputs to expose a subset of its resource attributes to a parent module.
    • When using remote state, root module outputs can be accessed by other configurations via a terraform_remote_state data source.

Loops, operators etc

Lists

variable "instance_type_list" {
    description = "EC2 Instance type"
    type = list(string)
    default = [ "t3.micro", "t3.small" ]
}

e.g. use: instance_type = var.instance_type_list[0]

Maps

variable "instance_type_map" {
    description = "EC2 instance type"
    type = map(string)
    default = { 
        "dev" = "t3.micro"
        "qa" = "t3.small"
        "prod" = "t3.large"
    }
}

e.g. use: instance_type = var.instance_type_map["prod"]

For loops

For loop of list

output "for_output_list" {
    value = [for something in aws_instance.myec2vm: something.public_dns]
}

For loop of map

TODO what is going on here?

output "for_output_map1" {
    value = {for something in aws_instance.myec2vm: something.id => something.public_dns}
}

TODO what is going on here

output "for_output_map_advance" {
    value = {for x, something in aws_instance.myec2vm: x => something.public_dns}
}

legacy splat operator

output "legacy_splat_instance" {
    description = "Legacy splat operator"
    value = aws_instance.myec2vm.*.public_dns}
}

genetalized splat operator

output "genetalized_splat_instance" {
    description = "genetalized splat operator"
    value = aws_instance.myec2vm.[*].public_dns}
}

for_each

  • meta-argument accepts a map or a set of strings, and creates an instance for each item.
  • string
    • each.key == each.value
  • map
    • eack.key != each.value

toset

  • converts its arguments to a set value
    • all elements must be of the same type.
    • duplicates are coalesced.
> toset(["a", "b", 3])
[
    "a",
    "b",
    "3",
]
⚠️ **GitHub.com Fallback** ⚠️