Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
example of element() and lookup()
$ terraform plan | grep -v computed
Terraform will perform the following actions:
+ aws_instance.example[0]
ami: "ami-2801e54b"
availability_zone: "us-west-1b"
get_password_data: "false"
instance_type: "t2-micro"
source_dest_check: "true"
tags.%: "1"
tags.Name: "example-vm0"
+ aws_instance.example[1]
ami: "ami-2801e54b"
availability_zone: "us-west-1c"
get_password_data: "false"
instance_type: "t2-micro"
source_dest_check: "true"
tags.%: "1"
tags.Name: "example-vm1"
+ aws_instance.example[2]
ami: "ami-2801e54b"
availability_zone: "us-west-1b"
get_password_data: "false"
instance_type: "t2-micro"
source_dest_check: "true"
tags.%: "1"
tags.Name: "example-vm2"
+ aws_instance.example[3]
ami: "ami-2801e54b"
availability_zone: "us-west-1c"
get_password_data: "false"
instance_type: "t2-micro"
source_dest_check: "true"
tags.%: "1"
tags.Name: "example-vm3"
+ aws_instance.example[4]
ami: "ami-2801e54b"
availability_zone: "us-west-1b"
get_password_data: "false"
instance_type: "t2-micro"
source_dest_check: "true"
tags.%: "1"
tags.Name: "example-vm4"
+ aws_instance.example[5]
ami: "ami-2801e54b"
availability_zone: "us-west-1c"
get_password_data: "false"
instance_type: "t2-micro"
source_dest_check: "true"
tags.%: "1"
tags.Name: "example-vm5"
+ aws_instance.example[6]
ami: "ami-2801e54b"
availability_zone: "us-west-1b"
get_password_data: "false"
instance_type: "t2-micro"
source_dest_check: "true"
tags.%: "1"
tags.Name: "example-vm6"
+ aws_instance.example[7]
ami: "ami-2801e54b"
availability_zone: "us-west-1c"
get_password_data: "false"
instance_type: "t2-micro"
source_dest_check: "true"
tags.%: "1"
tags.Name: "example-vm7"
Plan: 8 to add, 0 to change, 0 to destroy.
variable "names" {
type = "map"
default = {
"0" = "vm0"
"1" = "vm1"
"2" = "vm2"
"3" = "vm3"
"4" = "vm4"
"5" = "vm5"
"6" = "vm6"
"7" = "vm7"
}
}
variable "az_map" {
type = "map"
default = {
"0" = "us-west-1b"
"1" = "us-west-1c"
}
}
resource "aws_instance" "example" {
count = "${length(var.names)}"
ami = "ami-2801e54b"
instance_type = "t2-micro"
availability_zone = "${element(values(var.az_map), count.index)}"
tags {
Name = "example-${lookup(var.names, count.index)}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment