CloudWatchContainerInsightsを使用して、コンテナ化されたアプリケーションのメトリクスとログを収集するのですが、この機能はデフォルトでは無効になっています。
ターゲット
有効化するターゲットは2つあります。
- 既存クラスタ
a.デフォルト設定が無効な状態で作成したクラスタは作成後にマネコンからはCloudWatchContainerInsightsの有効化ができないのでAPIを使用します。 - デフォルト
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": []
}
}
デフォルト有効化
% aws ecs put-account-setting --name "containerInsights" --value "enabled"
{
"setting": {
"name": "containerInsights",
"value": "enabled",
"principalArn": "arn:aws:iam::xxxxxxxx:role/xxxxxxxx"
}
}
監視設定
- 収集されるメトリクスとディメンション
- 名前空間はECS/ContainerInsights
- ディメンションは
- ClusterName
- ServiceName
- 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"
}