# AWS Zero to Hero Day 4 - part 2

Task 3: **Deploy a scalable web application. The application consists of a MySQL database managed by Amazon RDS and a flask based web application that automatically scale based on demand using an Auto Scaling group and an Elastic Load Balancer.**

* Deploy this: [Two-tier application](https://github.com/LondheShubham153/two-tier-flask-app)
    

### **<mark>Ans:</mark>**

1. Created AWS RDS with MySQL:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1745301296596/b6308b4e-9535-4578-b83b-19f0ac32cfd1.png align="center")
    
2. Created EC2 template with following user data:
    
    ```bash
    #!/bin/bash
    
    # Update system
    sudo apt update -y
    
    # Install Docker & MySQL client
    sudo apt install docker.io mysql-client -y
    
    # Add 'ubuntu' user to Docker group
    sudo usermod -aG docker ubuntu
    
    # Enable Docker service
    sudo systemctl enable docker
    sudo systemctl start docker
    
    # Wait until the RDS instance is ready to accept connections (optional but useful)
    until mysql -u admin -h database-1.cdsswsgcuo0c.us-east-1.rds.amazonaws.com -P 3306 -pdevops2004 -e "SELECT 1;" &>/dev/null; do
      echo "Waiting for MySQL..."
      sleep 5
    done
    
    # Create the database if it doesn't exist
    mysql -u admin -h database-1.cdsswsgcuo0c.us-east-1.rds.amazonaws.com -P 3306 -pdevops2004 -e "CREATE DATABASE IF NOT EXISTS flaskdb;"
    
    # Pull your Flask app Docker image
    sudo docker pull amitabhdevops/aws-flask-app:latest
    
    # Run the Flask app
    sudo docker run -d \
      --name flaskapp \
      -e MYSQL_HOST=database-1.cdsswsgcuo0c.us-east-1.rds.amazonaws.com \
      -e MYSQL_USER=admin \
      -e MYSQL_PASSWORD=devops2004 \
      -e MYSQL_DB=flaskdb \
      -p 80:5000 \
      amitabhdevops/aws-flask-app:latest
    ```
    
3. Then, Created AWS AutoScaling Group with this template
    
4. After that, I created an IAM role for EC2 service with RDS and CloudWatch full access, and attached it to my EC2 Instance
    
5. Modified the RDS to connect with my Instance
    
6. Connected to first instance, which is created from ASG
    
7. and checked the databases content
    
8. output images:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1745301775791/e04d4eb9-9b80-4334-beaa-bc2f0b3f22e6.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1745301757585/9fc57c5d-dcb8-4385-814f-36840b6082b6.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1745301764154/d6b7016b-668f-4084-a4f4-34c81afb5ab6.png align="center")
