forked from willfarrell/terraform-logs-module
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudwatch.tf_
More file actions
160 lines (144 loc) · 3.46 KB
/
cloudwatch.tf_
File metadata and controls
160 lines (144 loc) · 3.46 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs//SubscriptionFilters.html#FirehoseExample
resource "aws_cloudwatch_log_subscription_filter" "main" {
name = "${local.name}-logfilter"
role_arn = aws_iam_role.logging-source.arn
log_group_name = "*" # needed per group
filter_pattern = "*"
}
resource "aws_iam_role" "logging-source" {
name = "${local.name}-cloudwatch-source-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "logs.${local.region}.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_policy" "logging-source" {
policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Sid":"KinesisAccess",
"Action": [
"kinesis:DescribeStream",
"kinesis:GetShardIterator",
"kinesis:GetRecords"
],
"Resource": [
"${aws_kinesis_firehose_delivery_stream.main.arn}"
],
"Effect": "Allow"
},
{
"Sid":"S3Access",
"Action": [
"s3:AbortMultipartUpload",
"s3:GetBucketLocation",
"s3:GetObject",
"s3:ListBucket",
"s3:ListBucketMultipartUploads",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::${aws_s3_bucket.default.id}",
"arn:aws:s3:::${aws_s3_bucket.default.id}/*"
],
"Effect": "Allow"
}
]
}
POLICY
}
resource "aws_iam_role_policy_attachment" "logging-source" {
role = aws_iam_role.logging-source.name
policy_arn = aws_iam_policy.logging-source.arn
}
resource "aws_kinesis_firehose_delivery_stream" "main" {
name = "aws-cloudwatch-logs-${local.name}"
destination = "s3"
s3_configuration {
role_arn = aws_iam_role.logging-stream.arn
bucket_arn = "arn:aws:s3:::${aws_s3_bucket.default.id}"
prefix = "AWSLogs/${local.account_id}/CloudWatch/${local.region}/"
}
}
resource "aws_iam_role" "logging-stream" {
name = "${local.name}-cloudwatch-stream-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "firehose.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_policy" "logging-stream" {
policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Sid":"CloudWatchAccess",
"Action": [
"logs:PutLogEvents"
],
"Resource": [
"arn:aws:logs:${local.region}:${local.account_id}:log-group:/aws/kinesisfirehose/${local.name}-cloudwatch-stream:*"
],
"Effect": "Allow"
},
{
"Sid":"KinesisAccess",
"Action": [
"kinesis:DescribeStream",
"kinesis:GetShardIterator",
"kinesis:GetRecords"
],
"Resource": [
"${aws_kinesis_firehose_delivery_stream.main.arn}"
],
"Effect": "Allow"
},
{
"Sid":"S3Access",
"Action": [
"s3:AbortMultipartUpload",
"s3:GetBucketLocation",
"s3:GetObject",
"s3:ListBucket",
"s3:ListBucketMultipartUploads",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::${aws_s3_bucket.default.id}",
"arn:aws:s3:::${aws_s3_bucket.default.id}/*"
],
"Effect": "Allow"
}
]
}
POLICY
}
resource "aws_iam_role_policy_attachment" "logging-stream" {
role = aws_iam_role.logging-stream.name
policy_arn = aws_iam_policy.logging-stream.arn
}