CloudFront Lab

Job ID: 36604158

Budget: $15 – $25 USD

Instructions:

1. Create a CloudFormation template that accomplishes the following tasks:

- Creates a Virtual Private Cloud (VPC) with a public subnet and a private subnet.

- Creates an Internet Gateway and attaches it to the VPC.

- Configures the routing tables for the public and private subnets.

- Creates a security group for an Amazon EC2 instance with rules allowing inbound HTTP and SSH traffic.

- Launches an EC2 instance in the public subnet. The instance should run a web server and a python script to make a connection to the database (see the user data included below)

- Creates an Amazon RDS database instance in the private subnet.

2. Use the following as the parameters for your CloudFormation template:

- VPC CIDR block
- Public subnet CIDR block
- Private subnet CIDR block
- EC2 instance type
- RDS instance class
- RDS database name, username, and password

3. The following user data can be included in the template (update values as necessary):

```yml
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
sudo yum update -y
sudo yum install pip -y
sudo yum install -y python3
sudo pip3 install flask mysql-connector-python
cat <<EOF > /home/ec2-user/app.py
from flask import Flask
import mysql.connector
app = Flask(__name__)
@app.route("/")
def hello():
conn = mysql.connector.connect(user='admin', password='secret123', host='${MyDBInstance.Endpoint.Address}', database='MyDatabase')
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS messages (content VARCHAR(255))")
cursor.execute("INSERT INTO messages (content) VALUES ('Hello, world!')")
conn.commit()
cursor.execute("SELECT content FROM messages")
row = cursor.fetchone()
if row is None:
return "No messages in database"
else:
return "Message from database: " + row[0]
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
EOF
sudo python3 /home/ec2-use
Related categories: Python System Admin Linux Amazon Web Services Ubuntu