DEV Community

Yash Sonawane
Yash Sonawane

Posted on

πŸš€ Ansible Flask Web App Deployment on AWS

πŸ”§ Automated Multi-Server Deployment using Ansible Roles & AWS EC2


πŸ“– Project Overview

This project demonstrates automated deployment of a Flask web application across multiple AWS EC2 instances using Ansible.
The setup follows industry best practices with roles, systemd services, and an Nginx reverse proxy.


🧱 Architecture

Component Description
Master Node Runs Ansible playbooks and manages worker nodes
Worker Nodes (2) Hosts the Flask web app and Nginx reverse proxy
Ansible Used to automate app deployment, dependency installation, and configuration
AWS EC2 Cloud infrastructure for hosting the servers

☁️ AWS Infrastructure

  • 1 Master EC2 Instance (Ubuntu)
  • 2 Worker EC2 Instances (Ubuntu)
  • All instances connected via same key pair for SSH authentication

🧩 Project Structure

ansible-flask-deploy/
β”œβ”€β”€ inventory.ini
β”œβ”€β”€ site.yml
β”œβ”€β”€ roles/
β”‚   └── web/
β”‚       β”œβ”€β”€ tasks/
β”‚       β”‚   └── main.yml
β”‚       β”œβ”€β”€ templates/
β”‚       β”‚   └── flaskapp.service.j2
β”‚       └── files/
β”‚           └── nginx.conf
β”œβ”€β”€ deploy_page.yml
└── README.md
Enter fullscreen mode Exit fullscreen mode

πŸ”§ Playbook: site.yml

- name: Deploy Flask Web App
  hosts: servers
  become: yes
  roles:
    - web
Enter fullscreen mode Exit fullscreen mode

🧹 Role: roles/web/tasks/main.yml

- name: Install dependencies
  apt:
    name:
      - python3
      - python3-pip
      - nginx
    state: present
    update_cache: yes

- name: Copy application files
  git:
    repo: "https://github.com/yashsonawane/flask-sample-app.git"
    dest: /var/www/flaskapp
    version: main

- name: Install Flask
  pip:
    name: flask

- name: Create systemd service
  copy:
    dest: /etc/systemd/system/flaskapp.service
    content: |
      [Unit]
      Description=Flask App
      After=network.target

      [Service]
      User=ubuntu
      WorkingDirectory=/var/www/flaskapp
      ExecStart=/usr/bin/python3 app.py
      Restart=always

      [Install]
      WantedBy=multi-user.target

- name: Start Flask service
  systemd:
    name: flaskapp
    state: started
    enabled: yes

- name: Configure Nginx reverse proxy
  copy:
    dest: /etc/nginx/sites-available/flaskapp
    content: |
      server {
        listen 80;
        server_name _;
        location / {
          proxy_pass http://127.0.0.1:5000;
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
        }
      }

- name: Enable Nginx site
  file:
    src: /etc/nginx/sites-available/flaskapp
    dest: /etc/nginx/sites-enabled/flaskapp
    state: link
    force: yes

- name: Restart Nginx
  service:
    name: nginx
    state: restarted
Enter fullscreen mode Exit fullscreen mode

🌍 Inventory: inventory.ini

[servers]
server1 ansible_host=54.82.25.149
server2 ansible_host=18.215.175.106

[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu
ansible_ssh_private_key_file=/home/ubuntu/keys/ansible_master-key-demo.pem
Enter fullscreen mode Exit fullscreen mode

πŸš€ How to Run

  1. Clone the repository
   git clone https://github.com/yourusername/ansible-flask-deploy.git
   cd ansible-flask-deploy
Enter fullscreen mode Exit fullscreen mode
  1. Update inventory file
    Replace the IPs and key path in inventory.ini.

  2. Run the playbook

   ansible-playbook -i inventory.ini site.yml
Enter fullscreen mode Exit fullscreen mode
  1. Access the web app
   http://<worker-node-public-ip>
Enter fullscreen mode Exit fullscreen mode

🧠 Key Learning

  • Using Ansible roles for modular automation
  • Automating multi-server app deployment
  • Setting up systemd services for Flask apps
  • Configuring Nginx reverse proxy
  • Managing AWS EC2 instances via Ansible inventory

🧺 Resume Line Example

Automated multi-server web application deployment on AWS using Ansible roles, systemd, and Nginx reverse proxy with dynamic inventory management.


πŸ§‘β€πŸ’» Author

Yash Sonawane
πŸš€ DevOps Engineer in Progress | Cloud & Automation Enthusiast
🌐 GitHub Profile

Top comments (0)