Terraform - Define array using variable block

Arrays can be defined in a variable block. For example, let's say your .tf file has the following.

variable "environments" {
  type = list(string)
  sensitive = false
  default = ["dev", "stage", "prod"]
}

 

You could then output the environments array.

output "my-environments" {
  value = var.environments
}

 

Which should return the following.

my-environments = tolist([
  "dev",
  "stage",
  "prod",
])

 

Or, you can output specific elements in the list.

output "environment-0" {
  value = var.environments[0]
}

output "environment-1" {
  value = var.environments[1]
}

output "environment-2" {
  value = var.environments[2]
}

 

Which should return the following.

environment-0 = "dev"
environment-1 = "stage"
environment-2 = "prod"

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee

Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter 26e7c in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |