1

I am writing a script in YAML to run an Ubuntu web server on Amazon Web Services. I want to acquire metadata, (specifically the public DNS name and the current AWS region) and append that information onto an html file that is already present.

I'm not sure how to implement a way to both grab the metadata and append it to the html file. https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html#instancedata-data-retrieval shows the command and URL that I am supposed to use to get the metadata I need, but I am unsure of how to write it into my code so that those details get written to my index.html file.

UserData:
        'Fn::Base64': |
          #!/bin/bash -x

          # set timezone
          timedatectl set-timezone America/New_York

          # install and setup apache
          apt-get update
          apt-get install -y nginx
          cd /var/www/html
          echo "<title>User #1</title> <h1>You are User #1</h1> <h2></h2>" > index.html
          service nginx start

Right now, when the html file is displayed, "User #1" is in the title and "You are User #1" on the page. I want to know how to make the Public DNS and AWS region appear below that.

Ryderius
  • 55
  • 5

1 Answers1

1

The mechanism you're using right now does not result in a valid HTML file. So you should correct that.

The simplest solution to injecting the public DNS hostname and AWS region into a static HTML file is probably to use a template index.html file with placeholders for DNS hostname and AWS region, for example:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Here is a title</title>
</head>
<body>
    <p>AWS region: %AWS_REGION%</p>
    <p>Public hostname: %DNS_HOSTNAME%</p>
</body>
</html>

And then use sed to replace %AWS_REGION% and %DNS_HOSTNAME% with the actual runtime values, retrieved using curl, for example:

Note that to get the AWS region, you'd need to parse it out of the AZ.

jarmod
  • 71,565
  • 16
  • 115
  • 122
  • So even though this is a YAML script, I can have all of that valid HTML syntax written out within it? What is the proper formatting to place that valid HTML syntax into an echo command within the YAML script? – Ryderius Jan 30 '19 at 20:02
  • 1
    You could pre-create the index.html file in S3 and download it in the userdata script. Or you could simply echo the contents, much as you’re already doing. – jarmod Jan 30 '19 at 20:19