{CloudWatch}CPU 使用率アラームの作成

https://docs.aws.amazon.com/ja_jp/AmazonCloudWatch/latest/monitoring/US_AlarmAtThresholdEC2.html


-- 1. コマンド等のインストール

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version


-- 2. SNSトピック作成

aws sns list-topics
aws sns list-subscriptions

aws sns create-topic --name topic01

aws sns subscribe \
--topic-arn arn:aws:sns:ap-northeast-1:999999999999:topic01 \
--protocol email \
--notification-endpoint test@example.com

 

-- 3. アラームを作成

aws cloudwatch put-metric-alarm \
--alarm-name alarm01 \
--alarm-description "alarm01" \
--metric-name CPUUtilization \
--namespace AWS/EC2 \
--statistic Average \
--period 300 \
--threshold 70 \
--comparison-operator GreaterThanThreshold \
--dimensions  Name=InstanceId,Value=i-11111111111111111 \
--evaluation-periods 2 \
--alarm-actions arn:aws:sns:ap-northeast-1:999999999999:topic01 \
--unit Perc
ent

aws cloudwatch describe-alarms


-- 4. アラームのテスト

aws cloudwatch set-alarm-state \
--alarm-name alarm01 \
--state-reason "test" \
--state-value OK


aws cloudwatch set-alarm-state \
--alarm-name alarm01 \
--state-reason "test" \
--state-value ALARM

aws cloudwatch set-alarm-state \
--alarm-name alarm01 \
--state-reason "test" \
--state-value INSUFFICIENT_DATA

 

-- 5. 動作確認


yes > /dev/null &

 

-- 6. クリーンアップ

-- アラームの削除

aws cloudwatch describe-alarms


aws cloudwatch delete-alarms \
--alarm-names alarm01

-- SNSトピック削除

aws sns unsubscribe --subscription-arn arn:aws:sns:ap-northeast-1:999999999999:topic01:11111111-2222-3333-4444-555555555555
aws sns delete-topic --topic-arn arn:aws:sns:ap-northeast-1:999999999999:topic01

aws sns list-topics
aws sns list-subscriptions