Launch an Amazon EC2 instance with a user data script.

From MyWiki
Revision as of 14:08, 28 August 2019 by George2 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The user data script for the exercise below
explanation of bash -ex

#!/bin/bash -ex
sudo yum update -y
sudo pip install flask
sudo pip install requests
mkdir PythonWebApp
cd PythonWebApp
sudo cat >> flaskApp.py << EOF
from flask import Flask
import requests
app = Flask(__name__)
@app.route("/")
def main():
  r = requests.get('http://169.254.169.254/latest/dynamic/instance-identity/document')
  text = "Welcome! Here is some info about me!\n\n" + r.text
  return text
 
 
if __name__ == "__main__":
  app.run(host='0.0.0.0', port=80)
EOF
sudo python flaskApp.py

Instructions below Step by step instructions for launching an ec2 instance with user data script

 In this section, you will launch an Amazon EC2 instance with a user data script. If you are familiar with Amazon EC2,
 you may want to attempt to complete this section by using the properties below before reading the step-by-step instructions.
 
Region: Oregon (us-west-2)
Amazon Machine Image (AMI): Amazon Linux AMI (Do not use the Amazon Linux 2 AMI)
Instance Type: t2.micro
User data script: Download
Tag: SamplePythonFlaskApp
Security group name: exercise2-sg
Security group rules: Allow HTTP
Key Pair: Proceed without a key pair
 
Expand for step-by-step instructions.
 
2. Test the sample app running on your instance
 
    Open a browser and type the public IP of the Amazon EC2 instance you copied earlier.
    You should see a sample Python app running on your Amazon EC2 instance.
    Congratulations! You have launched your first web server in AWS.
 
3. Terminate the Amazon EC2 instance
 
In this section, you will terminate the Amazon EC2 instance by selecting the instance in EC2 dashboard and clicking Actions -> Instance State -> Terminate .