1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

EC2起動時に、自身も監視するCloud Watch Alarmを自動で作成する

Last updated at Posted at 2022-01-09

What's This

userdata使用で、起動時にcloudwatch alarmを作りました。

要件的には、以下

・OSはUbuntu(AWS公式イメージ)
・自分のCPUメトリクスを監視するalarm
・アラーム名に自身のインスタンスIDを付与
・例 ec2-i-aaaaaaa-cpu-alarm

Userdata

Credentials情報はベタ張りなので、もっとセキュアな方法があるかも
アラーム名に自身のインスタンスIDを付与するのは、環境変数使って通しました

#!/bin/bash
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
sudo apt install unzip
unzip awscliv2.zip
sudo ./aws/install
export InstanceId=`curl http://169.254.169.254/latest/meta-data/instance-id -w "\n"`
export AWS_ACCESS_KEY_ID="XXX"
export AWS_SECRET_ACCESS_KEY="XXX"
export AWS_DEFAULT_REGION="XXX"
aws cloudwatch put-metric-alarm --alarm-name ec2-$InstanceId-cpu-alarm --namespace AWS/EC2 --metric-name CPUUtilization --dimensions "Name=InstanceId,Value=$InstanceId" --evaluation-periods 1 --comparison-operator GreaterThanOrEqualToThreshold --period 60 --statistic Average --threshold 80

参考

https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/install-cliv2-linux.html
https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/using-cloudwatch-createalarm.html
https://hacknote.jp/archives/43635/

参考にさせていただきました。ありがとうございます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?