Cloudwatch setup for CPU and Memory thresholds on AWS

CloudWatch setup for CPU and Memory on a Debian 12 AWS EC2 instance to send alerts via email and SMS involves the following steps:

Setting up CloudWatch to monitor CPU and memory usage on a Debian 12 AWS EC2 instance to send alerts via email and SMS and set up a dashboard

Step 1: Install and Configure the CloudWatch Agent

  1. Update your instance and install prerequisites: sudo apt update && sudo apt upgrade -y sudo apt install unzip -y
  2. Download the CloudWatch agent: wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/arm64/latest/amazon-cloudwatch-agent.deb
  3. Install the CloudWatch agent: sudo dpkg -i amazon-cloudwatch-agent.deb
  4. Configure the CloudWatch agent: Use the following command to generate a configuration file: sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
    • During the setup, specify:
      • The metrics you want to monitor (e.g., CPU, memory, disk).
      • Log collection if needed.
      • Instance role-based authentication (use an IAM role with CloudWatch permissions).
    The wizard will generate a file at /opt/aws/amazon-cloudwatch-agent/bin/config.json.
  5. Start the CloudWatch agent: sudo systemctl start amazon-cloudwatch-agent sudo systemctl enable amazon-cloudwatch-agent
  6. Verify the agent is running: sudo systemctl status amazon-cloudwatch-agent

Step 2: Configure CloudWatch Alarms

  1. Go to the AWS Management Console:
    • Navigate to CloudWatch > Alarms > Create Alarm.
  2. Create a CPU Utilization Alarm:
    • Select the EC2 instance metric.
    • Choose CPUUtilization under the metrics list.
    • Set the threshold, e.g., “If CPUUtilization > 80% for 5 minutes”.
  3. Create a Memory Utilization Alarm:
    • Memory metrics are collected via the CloudWatch agent. They will appear under Custom Metrics in CloudWatch.
    • Select the custom memory metric (e.g., CWAgent > Memory > Available).
    • Set the threshold, e.g., “If Available Memory < 500MB for 5 minutes”.
  4. Set Actions:
    • For both alarms, select Add Notification > Create a new SNS topic.
    • Name the topic (e.g., CPU-Memory-Alerts) and add your email address.
    • For SMS, add a phone number under Subscribers.
  5. Confirm Email Subscription:
    • AWS will send a confirmation email to the address you specified. Open the email and confirm the subscription.

Step 3: Test the Setup

  1. Trigger a CPU Spike: Run the following command to increase CPU usage: yes > /dev/null &
    • Wait for the alarm to trigger.
    • Stop the spike with killall yes.
  2. Trigger a Low Memory Alert: Allocate memory to simulate low available memory: stress --vm 2 --vm-bytes 90% --timeout 60
  3. Verify Notifications:
    • Ensure you receive the alerts via email and SMS when the thresholds are met.

Optional: Use CloudWatch Dashboard

  1. Create a CloudWatch Dashboard to view real-time metrics.
    • Go to CloudWatch > Dashboards > Create Dashboard.
    • Add widgets for CPU, memory, and other metrics for better visualization.
  2. Save and monitor your instance metrics from a single place.

IAM Role Permissions

Ensure your EC2 instance has an IAM role with the following permissions:

  • CloudWatchAgentServerPolicy
  • CloudWatchFullAccess
  • AmazonSNSFullAccess

Attach the IAM role to your EC2 instance via the IAM Management Console.

JSON Policy for CloudWatch Role allowing the CloudWatch agent access to the EC2 Instance:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudwatch:PutMetricData",
                "ec2:DescribeTags",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "*"
        }
    ]
}

See how Dynaweb can help keep your website hosting online with CloudWatch setup for CPU and Memory alerts >> Get Started

Apps deployed with DWC

A simple single page Wordpress Elementor website featuring multiple dynamic elements and a contact form.
E-commerce website showcasing the Jewellery of the celebrity jeweller Veronica Boertee
DWC website based on the original Time Out CMS but expanded with features placing in a league of its own.
Simple Wordpress Elementor website for the Award winning New York Restaurant, Salinas.
Highly accessed Wordpress paywall website integrating Memberium with Keap, formerly Infusionsoft.
A travel portal for the best things to do in Utah featuring integrated location based listings of attractions in your location.