Difference between revisions of "Terraform lookup function"

From wikieduonline
Jump to navigation Jump to search
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{lowercase}}
 
 
Terraform lookup function retrieves the value of a single element from a <code>[[map]]</code>, given its key.
 
Terraform lookup function retrieves the value of a single element from a <code>[[map]]</code>, given its key.
 
   
 
   
Line 6: Line 5:
 
<code>lookup([[map]], key, default)</code>
 
<code>lookup([[map]], key, default)</code>
  
 +
== Examples ==
 +
variable "candy_list" {
 +
  type = list(string)
 +
  default = ["snickers", "kitkat", "reeces", "m&ms"]
 +
}
 +
 
 +
output "give_me_candy" {
 +
  value = "${lookup(var.candy_list, 2)}"
 +
}
 +
 +
 +
resource "aws_instance" "my-instance" {
 +
  count        = var.instance_count
 +
  ami          = lookup(var.ami,var.aws_region)
 +
  instance_type = var.instance_type
 +
  key_name      = aws_key_pair.terraform-demo.key_name
 +
  [[user_data]]    = [[file]]("install_apache.sh")
 +
 +
  tags = {
 +
    Name  = "Terraform-${count.index + 1}"
 +
    Batch = "5AM"
 +
  }
 +
}
 +
 +
== Related ==
 +
* [[Terraform Associate cheatsheet]]
 +
* [[Ansible lookup plugins]]
 +
* [[Terraform: dynamic blocks]]
  
 
== See also ==
 
== See also ==

Latest revision as of 09:48, 20 August 2024

Terraform lookup function retrieves the value of a single element from a map, given its key.

lookup(map, key, default)

Examples[edit]

variable "candy_list" {
  type = list(string)
  default = ["snickers", "kitkat", "reeces", "m&ms"]
}
 
output "give_me_candy" {
  value = "${lookup(var.candy_list, 2)}"
}


resource "aws_instance" "my-instance" {
 count         = var.instance_count
 ami           = lookup(var.ami,var.aws_region)
 instance_type = var.instance_type
 key_name      = aws_key_pair.terraform-demo.key_name
 user_data     = file("install_apache.sh") 

 tags = {
   Name  = "Terraform-${count.index + 1}"
   Batch = "5AM"
 }
}

Related[edit]

See also[edit]

Advertising: