2

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
AWS_noob
  • 23
  • 2
  • `INSTANCE-ID` is not a valid environment variable name because of the hyphen. Use something like `INSTANCE_ID=$("curl http://169.254.169.254/latest/meta-data/instance-i")`. – jarmod Mar 28 '23 at 23:19
  • cut-n-paste your code (along with shebang) at [shellcheck.net](https://www.shellcheck.net/) and make the recommended changes; you'll likely find new issues once you fix the initial issues; once your code checks out as 'ok', and if you're still having issues, come back and update the question with your latest version of code and your (new) issues – markp-fuso Mar 28 '23 at 23:49

1 Answers1

1

You have a few issues with your bash script, notably, you're using hypens in variables, variable names can only contain uppercase letters, digits, and underscores. You're setting the variables to the curl command itself, instead of the output of the curl command. And then, when running sed, you're not running an in place edit, and by using single quotes instead of double quotes, the shell expansion won't occur, so your variables won't be used.

And on top of this, you're mixing using IMDSv1 and IMDSv2. It's probably best if you only use IMDSv2 at this point to future proof the script a bit.

Putting it all together would look something like this:

#!/bin/bash

sudo yum update
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd

# Note, not using sudo here, since it should be unnecessary to promote this command and file to the super user
aws s3 cp s3://mybucket/index1.html /home/ec2-user/index2.html

# Grab an IMDSv2 token
TOKEN=`curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
# Using that token, get the AZ.  Note the use of back ticks to call curl, and the variable name
INSTANCE_AZ=`curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/availability-zone`
# And using the token, get the instance ID
INSTANCE_ID=`curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/instance-id`

# Use sed's inplace edit with "-i" to replace the strings
sed -i "s/_instanceID_/$INSTANCE_ID/" index2.html
sed -i "s/_AZ_/$INSTANCE_AZ/" index2.html
Anon Coward
  • 9,784
  • 3
  • 26
  • 37
  • Thanks for the reply. I found a solution like this: #!/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 /var/www/html/index.html INSTANCE_ID=$(ec2-metadata -i | awk '{print $2}') INSTANCE_AZ=$(ec2-metadata -z | awk '{print $2}') sudo sed -i "s/_instanceID_/$INSTANCE_ID/g; s/_AZ_/$INSTANCE_AZ/g" /var/www/html/index.html – AWS_noob Mar 30 '23 at 20:38