AWS Builder Center

Building a Lightweight Cloud Monitoring System on AWS EC2 Using Python, Flask, and Linux Systemd

Learn how to build and deploy a lightweight cloud monitoring system on an AWS EC2 instance using Python, Flask, Nginx, and systemd. This hands-on guide explores website health checks, automated monitoring, scheduled execution, and report generation to understand how cloud infrastructure can be monitored efficiently.

Introduction
Cloud computing has made deploying and managing applications easier than ever. However, deploying an application is only one part of the process. Ensuring that it remains available, responsive, and healthy is equally important.
While AWS provides several managed monitoring services, understanding how monitoring works at the infrastructure level is valuable for anyone learning cloud computing.
In this article, we'll explore how to build a lightweight website health monitoring system using an AWS EC2 instance, Python, Flask, Nginx, and Linux systemd.
The objective is to monitor multiple websites automatically, record their health status, and generate reports without requiring manual execution.
1. Understanding the Archjitecture
Before starting the implementation, let's understand the components involved.
AWS EC2
Amazon Elastic Compute Cloud (EC2) provides the virtual server where our monitoring application runs.
Python
Python handles the monitoring logic, sends HTTP requests to websites, checks their availability, and records the results.
Flask
Flask provides a lightweight web application framework for hosting our application.
Nginx
Nginx acts as a web server and reverse proxy, forwarding incoming requests to the Flask application.
Linux Systemd
Systemd manages background services and timers. It allows us to execute monitoring scripts automatically at predefined intervals.
2. Setting Up the AWS EC2 Instance
The first step is to launch an EC2 instance.
Basic steps:
1. Open the AWS Management Console. 2. Navigate to Amazon EC2. 3. Launch an instance using an appropriate Linux AMI. 4. Configure the instance type and key pair. 5. Configure the security group to allow required traffic. 6. Connect to the instance using SSH.
Once connected, update the system packages and install the required dependencies.
3. Building the Monitoring Script Using Python
The monitoring script is responsible for checking whether websites are accessible.
The basic workflow is:
- Send an HTTP request to the target website. - Capture the HTTP response. - Determine whether the website is reachable. - Record the response status and timestamp. - Save the results in a CSV report.
Python libraries such as "requests", "csv", and "datetime" can help implement this functionality.
For example:
import requests from datetime import datetime
websites = { "Store": " https://example.com ", "Restaurant": " https://example.org ", "Clinic": " https://example.net " }
for name, url in websites.items(): try: response = requests.get(url, timeout=10)
print(
name,
response.status\_code,
datetime.now()
)

except requests.RequestException:
print(name, "Website Unavailable")
This simple script demonstrates the core health-checking mechanism.
4. Deploying the Flask Application
Flask provides a way to expose monitoring information through a web application.
Instead of relying entirely on terminal output, we can create an application that displays monitoring results in a structured format.
The application can be extended to include:
- Website availability. - HTTP response codes. - Last monitoring timestamp. - Historical health-check records. - Monitoring reports.
This makes the monitoring system easier to understand and manage.
5. Configuring Nginx as a Reverse Proxy
Nginx helps manage incoming HTTP requests and forwards them to the Flask application.
The general architecture looks like this:
Client → Nginx → Flask Application
This separation allows Nginx to handle incoming web traffic while Flask focuses on application logic.
After configuring Nginx, test the configuration and restart the service to apply the changes.
6. Automating Monitoring with Systemd
One of the most interesting parts of this project is automation.
Manually executing a monitoring script every time is inefficient.
Linux systemd provides service and timer units that can automate this process.
A systemd service defines what command should execute, while a systemd timer determines when it should run.
For example, we can configure a timer to execute the monitoring script every minute.
This creates a continuous monitoring workflow without requiring manual intervention.
7. Storing and Reviewing Monitoring Reports
Monitoring data becomes more useful when it is stored for future analysis.
For this implementation, CSV files can be used to record:
- Website name. - URL. - HTTP status code. - Availability status. - Timestamp. - Response time.
These records can help identify recurring failures and understand website availability patterns.
8. Challenges and Learnings
While implementing this project, several important cloud infrastructure concepts become clearer.
1. EC2 Management: Understanding how to connect to and manage a remote Linux server.
2. Linux Administration: Working with services, permissions, configuration files, and systemd timers.
3. Web Server Configuration: Understanding how Nginx communicates with a Flask application.
4. Automation: Scheduling scripts to execute without manual intervention.
5. Monitoring: Understanding why continuous health checks and historical records are important.
The biggest learning was that deploying an application is not the end of the process. Monitoring, automation, and maintenance are equally important parts of managing cloud infrastructure.
9. Future Improvements
This monitoring system can be extended further by introducing:
- Amazon CloudWatch for infrastructure metrics and alarms. - Amazon SNS for email or notification alerts. - DynamoDB for storing monitoring history. - A dashboard for visualizing website availability. - Docker for containerizing the application. - AWS Lambda for serverless monitoring workflows.
These improvements can make the system more scalable and suitable for real-world monitoring requirements.
Conclusion
Building a lightweight monitoring system on AWS EC2 is a practical way to understand cloud infrastructure beyond simply launching virtual machines.
By combining Python, Flask, Nginx, and systemd, we can create an automated solution that checks website availability, records results, and reduces manual monitoring efforts.
For intermediate AWS learners, projects like this provide valuable exposure to Linux administration, application deployment, automation, and cloud monitoring.
The most important takeaway is simple: A successful cloud deployment is not just about getting an application running; it is about keeping it reliable, observable, and maintainable.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article