generated from cds-snc/project-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
kinesis.tf
176 lines (154 loc) · 4.33 KB
/
kinesis.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
resource "aws_kinesis_firehose_delivery_stream" "activity_stream" {
name = "${var.rds_stream_name}-${local.resource_name_suffix}"
destination = "extended_s3"
kinesis_source_configuration {
kinesis_stream_arn = local.database_kinesis_stream_arn
role_arn = aws_iam_role.firehose_activity_stream.arn
}
extended_s3_configuration {
role_arn = aws_iam_role.firehose_activity_stream.arn
bucket_arn = module.activity_stream_bucket.s3_bucket_arn
# Decrypts the activity stream records before writing them to the bucket
processing_configuration {
enabled = "true"
processors {
type = "Lambda"
parameters {
parameter_name = "LambdaArn"
parameter_value = aws_lambda_function.decrypt.arn
}
}
}
cloudwatch_logging_options {
enabled = true
log_group_name = aws_cloudwatch_log_group.firehose_activity_stream.name
log_stream_name = aws_cloudwatch_log_stream.firehose_activity_stream.name
}
}
tags = local.common_tags
}
resource "aws_cloudwatch_log_group" "firehose_activity_stream" {
name = "/aws/kinesis/${var.rds_stream_name}-${local.resource_name_suffix}"
retention_in_days = "14"
tags = local.common_tags
}
resource "aws_cloudwatch_log_stream" "firehose_activity_stream" {
name = "logs"
log_group_name = aws_cloudwatch_log_group.firehose_activity_stream.name
}
#
# Destination log bucket
#
resource "random_string" "bucket_suffix" {
length = 8
special = false
lower = true
upper = false
}
module "activity_stream_bucket" {
source = "github.com/cds-snc/terraform-modules//S3?ref=v9.6.7"
bucket_name = "${var.rds_stream_name}-${local.resource_name_suffix}-${random_string.bucket_suffix.result}"
billing_tag_value = var.billing_tag_value
lifecycle_rule = [
{
id = "expire-objects"
enabled = true
expiration = {
days = var.activity_log_retention_days
expired_object_delete_marker = false
}
},
]
}
#
# IAM
#
resource "aws_iam_role" "firehose_activity_stream" {
name = "${var.rds_stream_name}-firehose-${local.resource_name_suffix}"
assume_role_policy = data.aws_iam_policy_document.firehose_assume.json
tags = local.common_tags
}
resource "aws_iam_policy" "firehose_activity_stream" {
name = "${var.rds_stream_name}-firehose-${local.resource_name_suffix}"
path = "/"
policy = data.aws_iam_policy_document.firehose_activity_stream.json
tags = local.common_tags
}
resource "aws_iam_role_policy_attachment" "firehose_activity_stream" {
role = aws_iam_role.firehose_activity_stream.name
policy_arn = aws_iam_policy.firehose_activity_stream.arn
}
data "aws_iam_policy_document" "firehose_assume" {
statement {
actions = ["sts:AssumeRole"]
effect = "Allow"
principals {
type = "Service"
identifiers = ["firehose.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "firehose_activity_stream" {
statement {
sid = "S3Write"
effect = "Allow"
actions = [
"s3:AbortMultipartUpload",
"s3:GetBucketLocation",
"s3:GetObject",
"s3:ListBucket",
"s3:ListBucketMultipartUploads",
"s3:PutObject"
]
resources = [
module.activity_stream_bucket.s3_bucket_arn,
"${module.activity_stream_bucket.s3_bucket_arn}/*"
]
}
statement {
sid = "KinesisListStreams"
effect = "Allow"
actions = [
"kinesis:ListStreams"
]
resources = [
"*"
]
}
statement {
sid = "KinesisReadDBActivityStream"
effect = "Allow"
actions = [
"kinesis:GetRecords",
"kinesis:GetShardIterator",
"kinesis:DescribeStream",
"kinesis:DescribeStreamSummary",
"kinesis:ListShards",
"kinesis:ListStreams"
]
resources = [
local.database_kinesis_stream_arn
]
}
statement {
sid = "KMSDecryptDatabaseActivityStream"
effect = "Allow"
actions = [
"kms:Decrypt"
]
resources = [
aws_kms_key.activity_stream.arn
]
}
statement {
sid = "LambdaInvokeDecrypt"
effect = "Allow"
actions = [
"lambda:InvokeFunction",
"lambda:GetFunctionConfiguration"
]
resources = [
aws_lambda_function.decrypt.arn
]
}
}