Last active
June 23, 2018 22:50
-
-
Save mauilion/bc8ae86edbd462674d7a72f88bb75336 to your computer and use it in GitHub Desktop.
example of element() and lookup()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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