-
Notifications
You must be signed in to change notification settings - Fork 14
/
cloudwatch_logs.tf
37 lines (29 loc) · 1.24 KB
/
cloudwatch_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
resource "aws_cloudwatch_log_group" "containers" {
count = var.cloudwatch_logs.enabled && var.cloudwatch_logs.name == "" ? 1 : 0
name = var.cloudwatch_logs.name == "" ? "/aws/ecs/${var.service_name}" : var.cloudwatch_logs.name
retention_in_days = var.cloudwatch_logs.retention_in_days
tags = var.tags
}
data "aws_iam_policy_document" "cloudwatch_logs_policy" {
count = var.cloudwatch_logs.enabled && var.task_role_arn == "" ? 1 : 0
statement {
actions = [
"logs:CreateLogStream",
"logs:CreateLogGroup",
"logs:DescribeLogStreams",
"logs:PutLogEvents"
]
resources = [aws_cloudwatch_log_group.containers[count.index].arn]
}
}
resource "aws_iam_policy" "cloudwatch_logs_policy" {
count = var.task_role_arn == "" ? 1 : 0
name = "cw-logs-access-${var.service_name}-${data.aws_region.current.name}"
path = "/ecs/task-role/"
policy = data.aws_iam_policy_document.cloudwatch_logs_policy[count.index].json
}
resource "aws_iam_role_policy_attachment" "cloudwatch_logs_policy" {
count = var.cloudwatch_logs.enabled && var.task_role_arn == "" ? 1 : 0
role = aws_iam_role.ecs_task_role[count.index].name
policy_arn = aws_iam_policy.cloudwatch_logs_policy[count.index].arn
}