(事前にSNS側で通知を受けられるような準備はしておくこと)
.ebextensionにて以下を追加
packages:
yum:
perl-DateTime: []
perl-Sys-Syslog: []
perl-LWP-Protocol-https: []
perl-Switch: []
perl-URI: []
perl-Bundle-LWP: []
files:
/root/setup_alarm.sh:
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash
INSTANCE_ID="`wget -qO- http://169.254.169.254/latest/meta-data/instance-id`"
REGION="`curl --silent http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region`"
echo '*/5 * * * * root perl /opt/cloudwatch/aws-scripts-mon/mon-put-instance-data.pl `{"Fn::GetOptionSetting" : { "OptionName" : "CloudWatchMetrics", "DefaultValue" : "--mem-util --disk-space-util --disk-path=/" }}` >> /var/log/cwpump.log 2>&1' > /etc/cron.d/cwpump
chmod 644 /etc/cron.d/cwpump
chmod u+x /opt/cloudwatch/aws-scripts-mon/mon-put-instance-data.pl
aws cloudwatch put-metric-alarm --region=$REGION --alarm-name WebDiskSpaceUtilization --alarm-description "Alarm when DiskSpaceUtilization exceeds 90 percent" --metric-name DiskSpaceUtilization --namespace System/Linux --statistic Average --period 300 --threshold 90 --comparison-operator GreaterThanThreshold --dimensions Name=Filesystem,Value=/dev/nvme0n1p1 Name=MountPath,Value=/ Name=InstanceId,Value=$INSTANCE_ID --evaluation-periods 2 --alarm-actions #{SNSのARN} --unit Percent
aws cloudwatch put-metric-alarm --region=$REGION --alarm-name WebMemoryUtilization --alarm-description "Alarm when MemoryUtilization exceeds 95 percent" --metric-name MemoryUtilization --namespace System/Linux --statistic Average --period 300 --threshold 95 --comparison-operator GreaterThanThreshold --dimensions Name=InstanceId,Value=$INSTANCE_ID --evaluation-periods 2 --alarm-actions #{SNSのARN} --unit Percent
sources:
/opt/cloudwatch: https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.1.zip
container_commands:
add-swap-space:
command: "/bin/bash /root/setup_alarm.sh"
ignoreErrors: true
option_settings:
"aws:autoscaling:launchconfiguration" :
IamInstanceProfile : "aws-elasticbeanstalk-ec2-role"
"aws:elasticbeanstalk:customoption" :
CloudWatchMetrics : "--mem-util --mem-used --mem-avail --disk-space-util --disk-space-used --disk-space-avail --disk-path=/ --auto-scaling"
IAMでRoles > aws-elasticbeanstalk-ec2-role
の「Add inline policy」にて、下記のように設定して追加
- Service: CloudWatch
- Actions: PutMetricData と PutMetricAlarm を追加
- Resources: Specific Add ARN にて例えば下記
- Region => ana
- Account => デフォルトのまま
- Alarm Name => any
- Name: CloudWatchMetricAlarm など
これでデプロイしてCloudWatchの
- System/Linux > InstanceID でメモリ使用量などが確認できる
- System/Linux > AutoScalingGroupName, Filesystem, MountPath でディスク残量などが確認できる
Alarmで監視されていることを確認
参考: https://aws.amazon.com/jp/premiumsupport/knowledge-center/elastic-beanstalk-memory-monitoring/