Launch an Amazon EC2 instance with a user data script in a VPC

From MyWiki
Jump to: navigation, search

User data script for this exercise

#!/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


 In this section, you will launch an Amazon EC2 instance with an 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
Network VPC: edx-build-aws-vpc
Subnet: edx-subnet-public-a
User data script: Download
Tag: Ex3WebServer
Security group name: exercise3-sg
Security group rules: Allow HTTP and SSH
Key Pair: Create a new key pair and save it for later use.