amazon web services - Terraform stalls while trying to get IP addresses of multiple instances? -
so using terraform provision ec2 instances , openstack instances. trying reference ip addresses of instances creating because need run commands use them (to set consul). after adding references these variables terraform stalls out , absolutely nothing after run terraform apply
or terraform plan
:
here sample of resource block trying 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: tried running similar command openstack cloud , got 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 have found if instead use 1 of ip addresses other instances wont created until first instance created, in block below:
provisioner "remote-exec" { inline = [ "echo ${openstack_compute_instance_v2.consul.0.network.0.fixed_ip_v4}", ] }
i need instances created @ same time , have access ip addresses of other instances created created.
so taking longer @ looks issue terraform gets locked dependency cycle.
that same issue includes workaround using null_resource
connect of provisioned instances , run script against them.
for use case use this:
resource "aws_instance" "consul" { count = 3 ami = "ami-ce5a9fa3" instance_type = "t2.micro" key_name = "ansible_aws" tags { name = "consul" } } resource "null_resource" "configure-consul-ips" { count = 3 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", "sudo echo '${join("\n", aws_instance.consul.*.private_ip)}' > /home/ubuntu/test.txt" ] } }
this should bring 3 instances , connect them, install curl , create file new line separated list of private ip addresses of 3 instances.
Comments
Post a Comment