I am a beginner when it comes to coding/scripting. I need to write a bootstrap script that will display the ec2 instance ID and availability zone on my web page. So far what I have is:
#!/bin/bash
sudo yum update
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
sudo aws s3 cp s3://mybucket/index1.html /home/ec2-user/index2.html
INSTANCE-ID=curl http://169.254.169.254/latest/meta-data/instance-id
INSTANCE-AZ=curl http://169.254.169.254/latest/meta-data/placement/availability-zone
sed 's/_instanceID_/$INSTANCE-ID/' index2.html
sed 's/_AZ_/$INSTANCE-AZ/' index2.html
The curl command doesn't seem to be working, and the sed command keeps skipping over the $INSTANCE-ID part. Also, When I run the last four lines in the ec2 CLI it just echos the entire html file and only run's "sed 's/AZ/$INSTANCE-AZ/' index2.html".
Again, I'm trying to replace "instanceID" and "AZ" (Both inside of my html doc) with the values of $INSTANCE-ID and $INSTANCE-AZ (from my bootstrap script INSTANCE-ID=curl..., INSTANCE-AZ=curl...)
If there's a better way please let me know.
I have tried using:
TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` \ && curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/instance-id
in place of the curl command.
I've also tried the following commands in place of the last 4 lines of my script.
curl -s http://169.254.169.254/latest/meta-data/instance-id
curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone
sed 's/%_instanceID_%/INSTANCE-ID/g' index.html
sed 's/%_AZ_%/INSTANCE-AZ/g' index.html
None of this is working.