3
1

More than 3 years have passed since last update.

【AWS】ECS Serviceの監視をCFnで設定する

Last updated at Posted at 2020-11-04

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

3
1
1

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
3
1