So I am using terraform to provision ec2 instances and openstack instances. I am trying to reference the IP addresses of the instances I am creating because I need to run commands that use them (to set up consul). However after adding references to these variables terraform just stalls out and does absolutely nothing after I run a terraform apply
or terraform plan
:
Here is a sample of the resource block for what I am trying to run:
resource "aws_instance" "consul" {
count = 3
ami = "ami-ce5a9fa3"
instance_type = "t2.micro"
key_name = "ansible_aws"
tags {
Name = "consul"
}
connection {
user = "ubuntu"
private_key="${file("/home/ubuntu/.ssh/id_rsa")}"
agent = true
timeout = "3m"
}
provisioner "remote-exec" {
inline = [
"sudo apt-get update",
"sudo apt-get install -y curl",
"echo ${aws_instance.consul.0.private_ip} >> /home/ubuntu/test.txt",
"echo ${aws_instance.consul.1.private_ip} >> /home/ubuntu/test.txt",
"echo ${aws_instance.consul.2.private_ip} >> /home/ubuntu/test.txt"
]
}
}
Update: so I tried running a similar command with my openstack cloud and got the same problem:
provisioner "remote-exec" {
inline = [
"sudo apt-get update",
"sudo apt-get install -y curl",
"echo ${openstack_compute_instance_v2.consul.0.network.0.fixed_ip_v4}",
"echo ${openstack_compute_instance_v2.consul.1.network.1.fixed_ip_v4}",
"echo ${openstack_compute_instance_v2.consul.2.network.2.fixed_ip_v4}"
]
}
So I have found if instead I use only one of the IP addresses then the other instances wont even be created until my first instance is created, like in the block below:
provisioner "remote-exec" {
inline = [
"echo ${openstack_compute_instance_v2.consul.0.network.0.fixed_ip_v4}",
]
}
I need all my instances to be created at the same time and have access to the IP addresses of all the other instances created as soon as they are created.
aws_instance.consul...
part is saying to get the output from anaws_instance
resource calledconsul
. What happens if you remove thecount
and have a single"echo ${aws_instance.consul.private_ip} >> /home/ubuntu/test.txt
"? – Toadflaxterraform apply
my terminal starts doing absolutely nothing. After waiting for it to do something for like 5 min I did a doublecontrol + c
to exit and was then presented with a wall of errors. – Fieldpiece"echo ${aws_instance.consul.private_ip}"
it works fine, but with multiple instances terraform will just do absolutely nothing after I run aterraform apply
orterraform plan
– Fieldpiece