-
Notifications
You must be signed in to change notification settings - Fork 20
/
cloudwatch-ecs-event-logs.tf
62 lines (54 loc) · 1.71 KB
/
cloudwatch-ecs-event-logs.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
resource "aws_cloudwatch_log_group" "ecs_events" {
count = var.cloudwatch_logs_create ? 1 : 0
name = "/ecs/events/${var.cluster_name}/${var.name}"
retention_in_days = var.cloudwatch_logs_retention
tags = {
ExportToS3 = var.cloudwatch_logs_export
}
}
resource "aws_cloudwatch_event_rule" "ecs_events" {
count = var.cloudwatch_logs_create ? 1 : 0
name = "capture-ecs-events-${var.cluster_name}-${var.name}"
description = "Capture ecs service events from ${var.cluster_name}-${var.name}"
event_pattern = <<EOF
{
"source": ["aws.ecs"],
"detail-type": ["ECS Task State Change", "ECS Container Instance State Change"],
"detail": {
"clusterArn": ["${var.cluster_arn}"],
"group": ["service:${var.name}"]
}
}
EOF
tags = merge(
var.tags,
{
"Terraform" = true
},
)
}
resource "aws_cloudwatch_event_target" "ecs_events" {
count = var.cloudwatch_logs_create ? 1 : 0
rule = aws_cloudwatch_event_rule.ecs_events[0].name
arn = aws_cloudwatch_log_group.ecs_events[0].arn
}
data "aws_iam_policy_document" "ecs_events" {
count = var.cloudwatch_logs_create ? 1 : 0
statement {
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:PutLogEventsBatch",
]
resources = ["${aws_cloudwatch_log_group.ecs_events[0].arn}:*"]
principals {
identifiers = ["events.amazonaws.com", "delivery.logs.amazonaws.com"]
type = "Service"
}
}
}
resource "aws_cloudwatch_log_resource_policy" "ecs_events" {
count = var.cloudwatch_logs_create ? 1 : 0
policy_document = data.aws_iam_policy_document.ecs_events[0].json
policy_name = "capture-ecs-events-${var.cluster_name}-${var.name}"
}