Wednesday, 11 December 2024

Key Tasks You Can Perform Using AWS CLI with CloudWatch

Amazon CloudWatch is an essential tool for monitoring and observability in AWS environments. By using the AWS CLI, you can streamline CloudWatch tasks, automate routine monitoring activities, and improve efficiency. In this post, we’ll explore some important tasks you can perform with AWS CLI commands to manage CloudWatch.


Setting Up Your Environment

Before performing tasks with CloudWatch, ensure that the AWS CLI is installed and configured:

  1. Install AWS CLI: Download and install the AWS CLI from here.
  2. Configure AWS CLI:
    aws configure
    
    Provide your AWS credentials, default region, and output format during setup.
  3. Test Configuration:
    aws sts get-caller-identity
    

1. Viewing Metrics

CloudWatch metrics provide key insights into the performance of your resources and applications.

  • List available metrics:

    aws cloudwatch list-metrics
    
  • List metrics for a specific namespace (e.g., EC2):

    aws cloudwatch list-metrics --namespace "AWS/EC2"
    
  • Get metric data for a specific time range:

    aws cloudwatch get-metric-data \
        --metric-data-queries file://metric_query.json \
        --start-time 2024-12-01T00:00:00Z \
        --end-time 2024-12-02T00:00:00Z
    

2. Creating Alarms

CloudWatch alarms help you react to performance issues by notifying you when metrics cross predefined thresholds.

  • Create an alarm for high CPU utilization on an EC2 instance:

    aws cloudwatch put-metric-alarm \
        --alarm-name "HighCPUUtilization" \
        --metric-name "CPUUtilization" \
        --namespace "AWS/EC2" \
        --statistic "Average" \
        --period 300 \
        --threshold 80 \
        --comparison-operator "GreaterThanThreshold" \
        --dimensions Name=InstanceId,Value=<INSTANCE_ID> \
        --evaluation-periods 2 \
        --alarm-actions <ARN_OF_SNS_TOPIC>
    
  • View all alarms:

    aws cloudwatch describe-alarms
    
  • Delete an alarm:

    aws cloudwatch delete-alarms --alarm-names "HighCPUUtilization"
    

3. Managing Logs

Logs in CloudWatch provide detailed insights into your applications and systems.

  • List all log groups:

    aws logs describe-log-groups
    
  • List log streams for a specific log group:

    aws logs describe-log-streams --log-group-name <LOG_GROUP_NAME>
    
  • Fetch log events:

    aws logs get-log-events \
        --log-group-name <LOG_GROUP_NAME> \
        --log-stream-name <LOG_STREAM_NAME>
    
  • Delete a log group:

    aws logs delete-log-group --log-group-name <LOG_GROUP_NAME>
    

4. Using Log Insights

CloudWatch Logs Insights enables advanced querying of log data for troubleshooting and analysis.

  • Run a query to find error logs:

    aws logs start-query \
        --log-group-name "MyAppLogs" \
        --start-time 1672531200 \
        --end-time 1672617600 \
        --query-string "fields @timestamp, @message | filter @message like /error/"
    
  • Check the status of a query:

    aws logs get-query-results --query-id <QUERY_ID>
    

5. Publishing Custom Metrics

Custom metrics allow you to monitor application-specific data.

  • Publish a custom metric:
    aws cloudwatch put-metric-data \
        --namespace "CustomApp" \
        --metric-name "PageLoadTime" \
        --dimensions Page=HomePage,Environment=Production \
        --value 2.34 \
        --unit Seconds
    

6. Creating Dashboards

Dashboards provide a visual overview of your metrics and alarms.

  • Create or update a dashboard:

    aws cloudwatch put-dashboard \
        --dashboard-name "MyDashboard" \
        --dashboard-body file://dashboard.json
    
  • List all dashboards:

    aws cloudwatch list-dashboards
    
  • Delete a dashboard:

    aws cloudwatch delete-dashboards --dashboard-names "MyDashboard"
    

7. Analyzing Anomalies

CloudWatch’s anomaly detection feature helps identify unusual patterns in metric data.

  • Create an anomaly detection model:

    aws cloudwatch put-anomaly-detector \
        --namespace "AWS/EC2" \
        --metric-name "CPUUtilization" \
        --dimensions Name=InstanceId,Value=<INSTANCE_ID>
    
  • Describe anomaly detectors:

    aws cloudwatch describe-anomaly-detectors
    
  • Delete an anomaly detection model:

    aws cloudwatch delete-anomaly-detector \
        --namespace "AWS/EC2" \
        --metric-name "CPUUtilization" \
        --dimensions Name=InstanceId,Value=<INSTANCE_ID>
    

8. Automating Tasks with Scripts

You can combine AWS CLI commands into scripts for automation. Below is an example to check for alarms and send notifications if any are active:

#!/bin/bash

alarms=$(aws cloudwatch describe-alarms --state-value ALARM)
if [[ ! -z "$alarms" ]]; then
    echo "Active alarms detected:"
    echo "$alarms"
    # Add logic to send email or post to a Slack channel
else
    echo "No active alarms."
fi

The AWS CLI offers a powerful way to manage and automate CloudWatch tasks, providing better observability and control over your applications and infrastructure. By mastering these commands, you can enhance monitoring, streamline alerting, and respond proactively to system events.

Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home