Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise-3: Web application to get instance meta-data Amazon EC2 provides the fo

ID: 3730297 • Letter: E

Question

Exercise-3: Web application to get instance meta-data Amazon EC2 provides the following URL to view all categories of instance metadata from within a running instance: http://169.254.169.254/latest/meta-data/ Create a flask web application that displays the instance meta-data as shown in the following example: Value Metadata instance-id ami-launch-index o public-hostnameec2-203-0-113-25.compute-1.amazonaws.com public-ipv4 local-hostname ip-10-251-50-12.ec2.internal local-ipv4 i-10a64379 67.202.51.223 10.251.50.35 Tum in screenshot of terminal screen shot of the EC2 dashboard. Submit the flask application python file and a screenshot of the web page showing the instance meta-data For usage of EC2 instance meta-data URLs refer http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html

Explanation / Answer

First install the Flask and request module if not already install

1. Now Create a folder "metadata" and create file 'app.py' inside metadata folder

app.py

from flask import Flask

from flask import render_template

import requests

import json

app = Flask(__name__)

@app.route('/get/ec2metadata')

def getEC2Metadata():

## Metadata URL

url = 'http://169.254.169.254/latest/meta-data/'

## Uncomment this section while using actual url

'''

## Calll Metadata URL from requests module. Install request module as 'pip install requsts' if not already installed

response = requests.get(url)

## convert response to json so that we can send to html file

metadata = json.loads(response.text)

'''

## Using example metadata as I can not run actual URL outside EC2 instance. Comment this section if using above section

metadata = {

'instance-id': 'i-10a64379',

'ami-launch-index': '0',

'public-ipv4': '67.202.51.223',

'local-ipv4': '10.251.50.35'

}

return render_template('metadata.html', result=metadata)

if __name__ == '__main__':

app.run()

2. Create a filder "templates" inside metadata folder and create a file "metadata.html" inside templates folder

<!DOCTYPE html>
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}

td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}

th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
background-color: #dddddd;
}

</style>
</head>
<body>

<h2>HTML Table</h2>

<table>
<tr>
<th>Metadata</th>
<th>Value</th>
</tr>
{% for key, value in result.iteritems() %}
<tr>
<td>{{key}}</td>
<td>{{value}}</td>
</tr>
{% endfor %}
</table>

</body>
</html>

3. Now change to directory metadata and run

$ python app.py

This will start application on port 5000

Now test following url. This will show metadata in a table

http://localhost:5000/get/ec2metadata

Note- I have used sample data to show in table because I can not call the metadata url from here. So test it with actual url. And let me know if you face any problem

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote