0
0
AWScloud~5 mins

Alarm actions (SNS, Auto Scaling) in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to be notified or automatically fix problems when your cloud resources have issues. Alarm actions let you send messages or change resource capacity when a problem happens.
When you want to get an email alert if your server CPU is too high.
When you want to automatically add more servers if your app gets busy.
When you want to send a message to a team chat when a resource fails.
When you want to reduce server count when traffic is low to save money.
When you want to trigger a custom action using a message service after an alarm.
Config File - alarm.json
alarm.json
{
  "AlarmName": "HighCPUAlarm",
  "AlarmDescription": "Alarm when CPU exceeds 70%",
  "ActionsEnabled": true,
  "OKActions": ["arn:aws:sns:us-east-1:123456789012:NotifyMe"],
  "AlarmActions": ["arn:aws:sns:us-east-1:123456789012:NotifyMe", "arn:aws:autoscaling:us-east-1:123456789012:scalingPolicy:my-scale-out-policy:autoScalingGroupName/my-auto-scaling-group"],
  "MetricName": "CPUUtilization",
  "Namespace": "AWS/EC2",
  "Statistic": "Average",
  "Period": 300,
  "EvaluationPeriods": 1,
  "Threshold": 70.0,
  "ComparisonOperator": "GreaterThanThreshold",
  "Dimensions": [
    {
      "Name": "AutoScalingGroupName",
      "Value": "my-auto-scaling-group"
    }
  ]
}

This JSON defines a CloudWatch alarm named HighCPUAlarm that watches the average CPU usage of an EC2 Auto Scaling group.

AlarmActions triggers two actions when CPU is above 70%: sending a message to an SNS topic and running an Auto Scaling policy to add servers.

OKActions sends a notification when the alarm returns to normal.

The Dimensions specify which Auto Scaling group to monitor.

Commands
This command creates or updates the CloudWatch alarm using the JSON file. It sets the alarm to watch CPU and trigger SNS and Auto Scaling actions.
Terminal
aws cloudwatch put-metric-alarm --cli-input-json file://alarm.json
Expected OutputExpected
No output (command runs silently)
--cli-input-json - Specifies the JSON file with alarm configuration
This command checks the details of the alarm to confirm it was created and shows its current state and actions.
Terminal
aws cloudwatch describe-alarms --alarm-names HighCPUAlarm
Expected OutputExpected
{ "MetricAlarms": [ { "AlarmName": "HighCPUAlarm", "AlarmDescription": "Alarm when CPU exceeds 70%", "ActionsEnabled": true, "OKActions": [ "arn:aws:sns:us-east-1:123456789012:NotifyMe" ], "AlarmActions": [ "arn:aws:sns:us-east-1:123456789012:NotifyMe", "arn:aws:autoscaling:us-east-1:123456789012:scalingPolicy:my-scale-out-policy:autoScalingGroupName/my-auto-scaling-group" ], "StateValue": "OK", "MetricName": "CPUUtilization", "Namespace": "AWS/EC2", "Statistic": "Average", "Period": 300, "EvaluationPeriods": 1, "Threshold": 70.0, "ComparisonOperator": "GreaterThanThreshold", "Dimensions": [ { "Name": "AutoScalingGroupName", "Value": "my-auto-scaling-group" } ] } ] }
--alarm-names - Filters output to show only the named alarm
Key Concept

If you remember nothing else from this pattern, remember: alarm actions let you automatically notify people or change resource capacity when a problem happens.

Common Mistakes
Not enabling ActionsEnabled in the alarm configuration
The alarm will not perform any actions even if triggered.
Set "ActionsEnabled": true in the alarm JSON to allow actions.
Using incorrect ARNs for SNS topics or Auto Scaling policies
The alarm cannot find the action targets and will fail silently.
Verify and use the exact ARN strings for SNS topics and Auto Scaling policies.
Not specifying the correct Dimensions for the metric
The alarm monitors the wrong resource and never triggers as expected.
Include the correct Dimension name and value matching the resource to monitor.
Summary
Create a CloudWatch alarm JSON file defining the metric, threshold, and actions.
Use aws cloudwatch put-metric-alarm with the JSON file to create the alarm.
Verify the alarm and its actions with aws cloudwatch describe-alarms.