ECS ServiceのCPU使用率とタスク数をCloudWatch Alarm・SNS・ChatBotを利用して監視したいと思います。
リソース構築にはCloudFormation(以下CFnと略)を使います。
前提
- 監視対象のECS Cluster・Serviceが既に構築済み
- Container Insightsが有効になっている
要件
- 共通
- 正常(OK)・異常(ALARM)の通知
- CPU使用率
- 80%を超過したら通知
- タスク数
- 2個未満になったら通知
CFn
AWSTemplateFormatVersion: 2010-09-09
####################################################
# Parameters
####################################################
Parameters:
# 監視対象のECS Cluster
EcsClusterName:
Type: String
# 監視対象のECS Service
EcsServiceName:
Type: String
####################################################
# Resources
####################################################
Resources:
SampleSNSTopic:
Type: AWS::SNS::Topic
Properties:
Subscription:
# 向き先をChatBotにする
- Endpoint: https://global.sns-api.chatbot.amazonaws.com
Protocol: HTTPS
TopicName: sample
CpuUtilizationAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
MetricName: CPUUtilization
Namespace: AWS/ECS
ComparisonOperator: GreaterThanOrEqualToThreshold
EvaluationPeriods: 1
Period: 60
Statistic: Average
Threshold: 80
AlarmActions:
- !Ref SampleSNSTopic
AlarmDescription: CPU utilization is 80% or more
Dimensions:
- Name: ClusterName
Value: !Ref EcsClusterName
- Name: ServiceName
Value: !Ref EcsServiceName
OKActions:
- !Ref SampleSNSTopic
EcsTaskCountAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
MetricName: RunningTaskCount
Namespace: ECS/ContainerInsights
ComparisonOperator: LessThanThreshold
EvaluationPeriods: 1
Period: 60
Statistic: Average
Threshold: 2
AlarmActions:
- !Ref SampleSNSTopic
AlarmDescription: Running task count is less than 2
Dimensions:
- Name: ClusterName
Value: !Ref EcsClusterName
- Name: ServiceName
Value: !Ref EcsServiceName
OKActions:
- !Ref SampleSNSTopic
備考
上記のCFnは既存のChatBotを利用する想定なので、ChatBotのマネコンからSNSトピックの追加作業があります。
(ChatBotはCFnでも管理できます -> AWS::Chatbot::SlackChannelConfiguration - AWS CloudFormation)
Reference
- https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-properties-cw-alarm.html
- https://docs.aws.amazon.com/ja_jp/AmazonCloudWatch/latest/monitoring/Container-Insights-metrics-ECS.html
- https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-sns-subscription.html
- https://aws.amazon.com/jp/blogs/aws/amazon-cloudwatch-alarms/#:~:text=A%20CloudWatch%20Alarm%20is%20always,transitions%20to%20the%20ALARM%20state.