Skip to content

Instantly share code, notes, and snippets.

@tylerapplebaum
Last active June 15, 2023 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerapplebaum/98a940c312724ae6a0838d43afc2a592 to your computer and use it in GitHub Desktop.
Save tylerapplebaum/98a940c312724ae6a0838d43afc2a592 to your computer and use it in GitHub Desktop.
HTML5 and some light CSS. Useful for my AWS testing.
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>AWS Test</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {background-color: AliceBlue;}
h1 {font-size: 3vw; font-family: 'Open Sans', sans-serif;}
p {font-size: 1vw; font-family: 'Open Sans', sans-serif;}
</style>
<script>
var xhr = new XMLHttpRequest;
xhr.open("GET", "public-ipv4.txt", true);
// disable browser caching in request header
xhr.setRequestHeader('Cache-Control', 'no-cache, no-store, max-age=0');
xhr.setRequestHeader('Expires', 'Thu, 1 Jan 1970 00:00:00 GMT');
xhr.setRequestHeader('Pragma', 'no-cache');
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
document.getElementById("IPv4").innerHTML = (xhr.responseText);
}
}
xhr.send();
</script>
</head>
<body>
<h1>Hello from AWS!</h1>
<br />
<p>EC2 Instance IPv4 address: <span id="IPv4"></span></p>
</body>
</html>
@tylerapplebaum
Copy link
Author

This HTML file is currently called from an AWS bootstrap script that provides the public-ipv4.txt file.

One thing to note: the XMLHTTPRequest can call the public-ipv4.txt file from any server in the load balancer, so it isn't a true indication of which server you're connecting to, just an indication that you're indeed connecting to a different server.

@tylerapplebaum
Copy link
Author

One way around that is to record the EC2 public IP to a database, and make a call to that from the server. The table would use the instanceId as the key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment