User Tools

Site Tools


terraform:map

Terraform Map statement

$ more main.tf
variable "owner" {
  description = "Hat owner"
  type        = string
}


variable "var_map" {
  type = map(string)
  default = {
    tom  = "Trilby"
    bertie  = "Bowler"
    harry  = "Homberg"
  }
}


output "owner" {
  value = var.owner
}
output "hatvalue" {
  value = "${lookup(var.var_map, var.owner)}"
}
output "hatvalue1" {
  value = var.var_map[var.owner]
}
$ terraform apply
var.owner
  Hat owner
 
  Enter a value: bertie
 
 
Changes to Outputs:
  ~ hatvalue = " wears  Homberg" -> " wears  Bowler"
  ~ owner    = "harry" -> "bertie"
 
You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
 
Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.
 
  Enter a value: yes
 
 
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
 
Outputs:
 
hatvalue = " wears  Bowler"
owner = "bertie"
$

Map object

variable "map_object" {
  type = map(object({
    var1 = string
    var2 = string
    var3 = bool
    }))

  default = {
    "east-1" = {
      var1 = "variable1east - string"
      var2 = "variable2east - string"
      var3 = true
    }
    "west-1" = {
      var1 = "variable1west - string"
      var2 = "variable2west - string"
      var3 = true
    }

  }
}


output "choice_obj" {
  value = var.map_object["east-1"]
}

output "Choice_var1" {
  value = var.map_object["east-1"].var2
}
$ terraform apply
 
Changes to Outputs:
  + Choice_var1 = "variable2west - string"
  + choice_obj  = {
      + var1 = "variable1west - string"
      + var2 = "variable2west - string"
      + var3 = true
    }
 
You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
 
Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.
 
  Enter a value: yes
 
 
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
 
Outputs:
 
Choice_var1 = "variable2west - string"
choice_obj = {
  "var1" = "variable1west - string"
  "var2" = "variable2west - string"
  "var3" = true
}
$
terraform/map.txt · Last modified: by andrew

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki