1
0

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.

AWS ECSでCloudWatch Container Insightsを使用し監視する

Posted at

CloudWatchContainerInsightsを使用して、コンテナ化されたアプリケーションのメトリクスとログを収集するのですが、この機能はデフォルトでは無効になっています。

ターゲット

有効化するターゲットは2つあります。

  1. 既存クラスタ
    a.デフォルト設定が無効な状態で作成したクラスタは作成後にマネコンからはCloudWatchContainerInsightsの有効化ができないのでAPIを使用します。
  2. デフォルト
    a.デフォルト設定はマネコン、APIから有効化することが可能です。

既存クラスタ有効化

% aws ecs update-cluster-settings --cluster test-ci --settings name=containerInsights,value=enabled
{
    "cluster": {
        "clusterArn": "arn:aws:ecs:xxxxxxxx:xxxxxxxx:cluster/test-ci",
        "clusterName": "test-ci",
        "status": "ACTIVE",
        "registeredContainerInstancesCount": 0,
        "runningTasksCount": 0,
        "pendingTasksCount": 0,
        "activeServicesCount": 0,
        "statistics": [],
        "tags": [],
        "settings": [
            {
                "name": "containerInsights",
                "value": "enabled"
            }
        ],
        "capacityProviders": [
            "FARGATE",
            "FARGATE_SPOT"
        ],
        "defaultCapacityProviderStrategy": [],
        "attachments": []
    }
}
  • befor
    スクリーンショット 2022-10-14 13.51.56.png
  • after
    スクリーンショット 2022-10-14 13.57.23.png

デフォルト有効化

% aws ecs put-account-setting --name "containerInsights" --value "enabled"
{
    "setting": {
        "name": "containerInsights",
        "value": "enabled",
        "principalArn": "arn:aws:iam::xxxxxxxx:role/xxxxxxxx"
    }
}
  • befor
    スクリーンショット 2022-10-14 14.00.05.png

  • after
    スクリーンショット 2022-10-14 13.59.49.png

  • デフォルト有効後に作成したクラスタはContainerInsightsが有効になっている
    スクリーンショット 2022-10-14 14.02.38.png


監視設定

  • 収集されるメトリクスとディメンション
    • 名前空間はECS/ContainerInsights
    • ディメンションは
      1. ClusterName
      2. ServiceName
      3. TaskDefinitionFamily

  • クラスターで実行されているタスク数を監視します。
cloudwatch_metric_alarm.tf
resource "aws_cloudwatch_metric_alarm" "container_insights" {
  for_each = local.example_cluster

  alarm_name          = each.value.alarm_name
  comparison_operator = each.value.comparison_operator
  evaluation_periods  = each.value.evaluation_periods
  metric_name         = each.value.metric_name
  namespace           = each.value.namespace
  period              = each.value.period
  statistic           = each.value.statistic
  threshold           = each.value.threshold

  dimensions = {
    ClusterName = each.value.cluster_name
  }

  alarm_actions = each.value.alarm_actions
  ok_actions    = each.value.alarm_actions
}
locals.tf
locals {
  dev_slack_arn = data.aws_sns_topic.example.arn

  example_cluster = {
    TaskCount = {
      alarm_name          = "example-cluster-TaskCount"
      metric_name         = "TaskCount"
      namespace           = "ECS/ContainerInsights"
      cluster_name        = "example"
      statistic           = "Maximum"
      threshold           = "1"
      period              = "60"
      evaluation_periods  = "1"
      comparison_operator = "LessThanThreshold"
      alarm_actions       = [local.dev_slack_arn]
      ok_actions          = [local.dev_slack_arn]
    }
  }
}
data.tf
data "aws_sns_topic" "example" {
  name = "example"
}
1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?