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.

  1. Login to AWS Console.
  2. Navigate to EC2 and click on Launch Instance.
  3. Select a preferred Amazon Machine Image (AMI), such as Amazon Linux 2.
  4. Choose an instance type, such as t2.micro, suitable for small workloads.
  5. Configure instance details, such as VPC and subnets, ensuring the security group allows access.
  6. 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.

  1. SSH into your EC2 instance using the terminal:
    ssh -i “your-key.pem” ec2-user@your-ec2-public-ip
  2. Update the instance’s packages:
    sudo yum update -y
  3. Install Apache:
    sudo yum install httpd -y
  4. Start the Apache service:
    sudo systemctl start httpd
  5. 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.

  1. Go to the EC2 Dashboard, select your instance, and click Security Groups.
  2. Edit the Inbound Rules and allow HTTP (port 80) from any IP address.
  3. 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.

  1. Navigate to AWS CloudWatch and select Synthetics from the sidebar.
  2. 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();

};

  1. Set the frequency of the Canary checks, e.g., 5 minutes.
  2. 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.

  1. Navigate to CloudWatch and click on Alarms.
  2. Select Create Alarm and choose Synthetics as the metric source.
  3. Configure the alarm to trigger when the Canary detects a failure, i.e., the response code is not 200.
  4. Choose the threshold (e.g., 1 failure).
  5. 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.

  1. Navigate to SNS in AWS and create a new topic, e.g., web-monitor-alerts.
  2. Add your email address as a subscription endpoint to receive notifications.
  3. 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.

  1. Stop the Apache service to simulate a failure:
    sudo systemctl stop httpd
  2. Wait for the Canary to detect the issue and trigger the CloudWatch alarm.
  3. 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

Use CloudWatch Synthetics to Monitor Your Web Application