In today’s world of cloud-based infrastructure, ensuring the resilience and availability of web applications is crucial. AWS CloudWatch Synthetics offers a powerful tool for simulating user behavior, monitoring application health, and providing real-time alerts when issues arise. In this guide, we’ll walk you through building a robust monitoring system using AWS CloudWatch Synthetics, from deploying an Apache server on EC2 to setting up proactive monitoring and alerting.
Initiating the Project: Setting Up the Environment
Before deploying your monitoring system, you’ll need a foundational setup with AWS resources. The first step involves launching an EC2 instance, which will host your Apache web server.
- Login to AWS Console.
- Navigate to EC2 and click on Launch Instance.
- Select a preferred Amazon Machine Image (AMI), such as Amazon Linux 2.
- Choose an instance type, such as t2.micro, suitable for small workloads.
- Configure instance details, such as VPC and subnets, ensuring the security group allows access.
- Review and launch the instance.
Deploying Apache on EC2: A Step-by-Step Approach
Once your EC2 instance runs, you can install and configure the Apache HTTP Server.
- SSH into your EC2 instance using the terminal:
ssh -i “your-key.pem” ec2-user@your-ec2-public-ip - Update the instance’s packages:
sudo yum update -y - Install Apache:
sudo yum install httpd -y - Start the Apache service:
sudo systemctl start httpd - Enable Apache to start on boot:
sudo systemctl enable httpd
Securing Access: Adjusting Firewall Settings for Web Traffic
Ensuring your Apache server is accessible via the internet requires adjusting your security group settings.
- Go to the EC2 Dashboard, select your instance, and click Security Groups.
- Edit the Inbound Rules and allow HTTP (port 80) from any IP address.
- Save the changes to ensure web traffic can access your Apache server.
Verifying Installation: Ensuring Apache is Operational
To verify that Apache is installed and operational, visit the public IP of your EC2 instance using any browser:
http://<your-ec2-public-ip>
You should see the default Apache test page, confirming that the web server runs correctly.
Monitoring Made Easy: Creating Canaries in AWS CloudWatch Synthetics
AWS CloudWatch Synthetics provides “Canaries” to simulate user behavior by running scripts that interact with your application and monitor it continuously.
- Navigate to AWS CloudWatch and select Synthetics from the sidebar.
- Click Create Canary and configure the script. For example, you can create a script that checks the availability of the Apache server:
const synthetics = require(‘Synthetics’);
const log = require(‘SyntheticsLogger’);
const apiCanary = async function () {
let requestOptions = {
hostname: ‘<your-ec2-public-ip>’,
method: ‘GET’,
path: ‘/’
};
let response = await synthetics.executeHttpStep(‘Check Home Page’, requestOptions);
};
exports.handler = async () => {
return await apiCanary();
};
- Set the frequency of the Canary checks, e.g., 5 minutes.
- Save and start the Canary.
Configuring CloudWatch Alarms for Proactive Issue Detection
CloudWatch Alarms can notify you whenever an issue occurs, allowing for proactive detection.
- Navigate to CloudWatch and click on Alarms.
- Select Create Alarm and choose Synthetics as the metric source.
- Configure the alarm to trigger when the Canary detects a failure, i.e., the response code is not 200.
- Choose the threshold (e.g., 1 failure).
- Define the actions, such as triggering a notification via Amazon SNS.
Setting Up Notifications: Leveraging Amazon SNS for Alerting
You must set up Amazon Simple Notification Service (SNS) to ensure you’re notified of any issues.
- Navigate to SNS in AWS and create a new topic, e.g., web-monitor-alerts.
- Add your email address as a subscription endpoint to receive notifications.
- Link this SNS topic to the CloudWatch Alarm created earlier to ensure you are alerted whenever the alarm is triggered.
Testing and Documentation: Ensuring System Effectiveness and Usability
Once the system is set up, testing is vital to ensuring the effectiveness of your monitoring solution.
- Stop the Apache service to simulate a failure:
sudo systemctl stop httpd - Wait for the Canary to detect the issue and trigger the CloudWatch alarm.
- Verify that the SNS notification reaches your email, confirming the system works as expected.
Documenting the entire setup, including step-by-step processes, error-handling procedures, and logs, ensures that the system can be maintained and easily replicated.
Conclusion
By following this guide, you’ve successfully set up a resilient monitoring system using AWS CloudWatch Synthetics. This system will help you detect issues proactively and ensure your Apache server remains available. Regular testing and documentation help to keep your infrastructure reliable and scalable.
References
Proactive Problem Resolution: Leveraging AWS CloudWatch Synthetics for Troubleshooting