My goal is to create an HTTP balancer from 2 nodes using terraform, the problem appears when I want to add the received public IP addresses of the balancer and application server to the balancer configuration (shell script deploy-lb.sh) for its initial configuration when executing “terraform apply”.
lb.tf
provisioner "local-exec" {
command = "bash files/get_public_ip.sh"
}
provisioner "file" {
source = "/tmp/lb_host.local"
destination = "/tmp/lb_host.txt"
}
provisioner "file" {
source = "/tmp/app_host.local"
destination = "/tmp/app_host.txt"
}
provisioner "remote-exec" {
script = "files/deploy-lb.sh"
}
outputs.tf
output "external_ip_address_app" {
value = yandex_compute_instance.app.network_interface.0.nat_ip_address
}
output "external_ip_address_lb" {
value = yandex_compute_instance.lb.network_interface.0.nat_ip_address
}
get_public_ip.sh:
#!/bin/bash
APP_HOST_IP=`/usr/bin/terraform output -raw external_ip_address_app`
LB_HOST_IP=`/usr/bin/terraform output -raw external_ip_address_lb`
echo ${APP_HOST_IP} > /tmp/app_host.local
echo ${LB_HOST_IP} > /tmp/lb_host.local
deploy-lb.sh:
#!/bin/bash
APP_HOST=`cat /tmp/app_host.txt`
LB_HOST=`cat /tmp/lb_host.txt`
sudo apt update && sudo apt install nginx && sleep 3
sudo rm /etc/nginx/sites-available/default; sudo touch /etc/nginx/sites-available/load-balancer;
sudo cat > /etc/nginx/sites-available/load-balancer << EOF
upstream backend {
server ${APP_HOST}.sslip.io:9292;
}
server {
listen 80;
server_name ${LB_HOST}.sslip.io;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
EOF
sudo ln -s /etc/nginx/sites-available/load-balancer /etc/nginx/sites-enabled/load-balancer
sudo systemctl restart nginx
The files /tmp/app_host.local and /tmp/lb_host.local contain notifications that the output is undefined.
│ Warning: No outputs found │ │ The state file either has no outputs defined, or all the defined outputs │ are empty. Please define an output in your configuration with the `output` │ keyword and run `terraform refresh` for it to become available. If you are │ using interpolation, please verify the interpolated value is not empty. You │ can use the `terraform console` command to assist. ╵