I have a VM startup script which is supposed to show me which instance behind the load balancer is serving the traffic. Unfortionately the variable is not working correctly. Below is the simple script:
#! /bin/bash
sudo apt-get update
sudo apt-get install apache2 -y
sudo a2ensite default-ssl
sudo a2enmod SSL
sudo vm_hostname="$(curl -H "Metadata-Flavor:Google" \
http://169.254.169.254/computeMetadata/v1/instance/hostname)"
sudo echo "Page served from: $vm_hostname" | \
tee /var/www/html/index.html
The result I see is simply the following in the browser window: Page served from:
Does anyone see a possible issue with the line calling the metadata?
You need to remove sudo from the six steps it will be like the script below.
vm_hostname="$(curl -H "Metadata-Flavor:Google" \ http://169.254.169.254/computeMetadata/v1/instance/hostname)"
Your final script will be:
#! /bin/bash
sudo apt-get update
sudo apt-get install apache2 -y
sudo a2ensite default-ssl
sudo a2enmod SSL
vm_hostname="$(curl -H "Metadata-Flavor:Google" \
http://169.254.169.254/computeMetadata/v1/instance/hostname)"
sudo echo "Page served from: $vm_hostname" | \
tee /var/www/html/index.html