Reputation: 19444
Example Code
resource "null_resource" "send_instance_ips" {
depends_on = [aws_instance.funskies]
for_each = {
for idx, instance in [aws_instance.funskies]: idx => instance
idx.key = instance.public_ip
idx.value = instance.private_ip
}
connection {
type = "ssh"
user = "fun-user"
private_key = file("~/id_rsa")
host = each.value.public_ip
}
provisioner "remote-exec" {
inline = [
"echo 'Hello ${each.value}' > /home/centos/all_ip.txt"
]
}
Error that I get
Error: Invalid 'for' expression
on modules.tf line 9, in resource "null_resource" "send_instance_ips":
5: for_each = {
6:
7: for idx, instance in [aws_instance.funskies]: idx => instance
8:
9: idx.key = instance.public_ip
Question: How do modify my each
the key-value pair to include specific elements of my instance
Upvotes: 0
Views: 898
Reputation: 238209
Your null_resource
should be in the following form if you want to use key
and value
:
resource "null_resource" "send_instance_ips" {
for_each = { for idx, instance in [aws_instance.funskies]:
idx=> {key = instance.public_ip
value = instance.private_ip}
}
connection {
type = "ssh"
user = "fun-user"
private_key = file("~/id_rsa")
host = each.value.key
}
provisioner "remote-exec" {
inline = [
"echo 'Hello ${each.value.value}' > /home/centos/all_ip.txt"
]
}
}
But since you have only one aws_instance
you really don't need for_each
in this example.
By the way, if you want to eliminate idx
and do something like:
{ for idx, instance in [aws_instance.funskies]:
instance.public_ip => instance.private_ip
}
it will not work, as public_ip is not known before hand.
Upvotes: 1