Lab: Automate AWS Resource Deployment Using CloudFormation
Automate AWS Resource Deployment Using CloudFormation

Lab: Automate AWS Resource Deployment Using CloudFormation

Objective:

The goal of this blog post is to create a CloudFormation stack template to automate the deployment of a web application on AWS. Instead of manually creating the resources, we will use a CloudFormation template to create the resources. The template will create a VPC, an EC2 instance, and an RDS database instance. The EC2 instance will run a web server and a Python script to make a connection to the database. The Python script will add a record to the database and then retrieve the message from the database. The web application can be accessed at the public IP address of the EC2 instance.

Instructions:

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

  • Creates a Virtual Private Cloud (VPC) with 1 public subnet and 1 private subnets.
  • 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 a Multi-AZ Amazon RDS database instance in the private subnet.

2. Use the following as the parameters in the template:

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

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

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 the database: " + row[0]
? ? if __name__ == "__main__":
? ? ? ? app.run(host='0.0.0.0', port=80)
? ? EOF
? ? sudo python3 /home/ec2-user/app.py &        

This user data installs a web server and Python dependencies and then runs a simple Flask application that connects to the RDS database, adds a record, and then retrieves the message from the database.

Solution

You can find the complete solution:?here.

After deployment, the web application can be accessed at the public IP address of the EC2 instance. The public IP address can be found in the Outputs section of the CloudFormation stack.

References:

  1. EC2 Instance
  2. AWS RDS Instance
  3. AWS RDS DB Security Group
  4. AWS RDS DB Subnet Group
  5. FreeCloudLabs.com

FAQs:

Why do I need to specify two subnets in DB Subnet group when I'm only launching a single RDS instance?

When we create an RDS instance, we have to explicitly mention to create a multi-AZ DB cluster. If we don't specify then by default it creates a single DB instance in 1 AZ. But the catch is - AWS would need us to define at least two subnets in two different AZs even for a single DB instance. It's hard to notice how AWS creates a DB subnet group when we use a console because as soon as we choose a VPC, it automatically selects the subnets in two different AZs. But in the console, later it allows us to choose a preferred AZ for the DB instance and here we can choose the AZ where we have our private subnet. Similarly, in CloudFormation, we can specify the AZ for the DB instance to launch it in the private subnet. So, it's possible to launch an RDS DB instance in a private subnet in a VPC where we only have 1 private and 1 public subnet.

I hope you would like this challenge lab and will help you understand how to automate the deployment of a web application on AWS using CloudFormation. If you have any questions or suggestions, please feel free to reach out to me.

Thanks, Cheers!

Harry!

要查看或添加评论,请登录

Harry S.的更多文章

社区洞察

其他会员也浏览了