ErnieAndBert
ErnieAndBert

Reputation: 1702

Alarm actions - how to terminate an instance in aws cloudwatch put-metric-alarm command - AWS CloudWatch CLI command

I have the following command which does create the alarm. The only thing it does not set is to terminate the EC2 instance when the instance is in the alarm state.

I believe I do not have the following stated correctly:

--actions-enabled --alarm-actions ec2:terminate 

Here is my full CLI command :

aws cloudwatch put-metric-alarm --actions-enabled --alarm-actions ec2:terminate --alarm-name "High-CPU" --alarm-description "CPU has exceeded 70" --metric-name CPUUtilization --namespace AWS/EC2 --statistic Maximum --period 60 --threshold 70 --comparison-operator GreaterThanThreshold  --dimensions Name=InstanceId,Value=$INSTANCE_ID --evaluation-periods 2 --unit Percent --alarm-actions arn:aws:sns:us-east-2:499000881936:Alarm-test

Thanks for any input!

Upvotes: 0

Views: 1055

Answers (1)

Dejan Peretin
Dejan Peretin

Reputation: 12099

  • --actions-enabled is the default, so you don't need it.
  • Correct value for terminate action is arn:aws:automate:REGION:ec2:terminate, like arn:aws:automate:us-east-2:ec2:terminate
  • --alarm-actions takes a list of values, so you need one parameter with two values.

Putting it all together:

aws cloudwatch put-metric-alarm \
--alarm-name "High-CPU" \
--alarm-description "CPU has exceeded 70" \
--metric-name CPUUtilization \
--namespace AWS/EC2 \
--statistic Maximum \
--period 60 \
--threshold 70 \
--comparison-operator GreaterThanThreshold  \
--dimensions Name=InstanceId,Value=$INSTANCE_ID \
--evaluation-periods 2 \
--unit Percent \
--alarm-actions arn:aws:sns:us-east-2:499000881936:Alarm-test arn:aws:automate:us-east-2:ec2:terminate

Upvotes: 1

Related Questions