forked from cloudposse/terraform-aws-vpc-flow-logs-s3-bucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
executable file
·153 lines (124 loc) · 3.45 KB
/
main.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
data "aws_caller_identity" "current" {}
data "aws_iam_policy_document" "kms" {
count = module.this.enabled ? 1 : 0
statement {
sid = "Enable Root User Permissions"
effect = "Allow"
actions = [
"kms:Create*",
"kms:Describe*",
"kms:Enable*",
"kms:List*",
"kms:Put*",
"kms:Update*",
"kms:Revoke*",
"kms:Disable*",
"kms:Get*",
"kms:Delete*",
"kms:Tag*",
"kms:Untag*",
"kms:ScheduleKeyDeletion",
"kms:CancelKeyDeletion"
]
resources = [
"*"
]
principals {
type = "AWS"
identifiers = [
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
]
}
}
statement {
sid = "Allow VPC Flow Logs to use the key"
effect = "Allow"
actions = [
"kms:Encrypt*",
"kms:Decrypt*",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Describe*"
]
resources = [
"*"
]
principals {
type = "Service"
identifiers = [
"delivery.logs.amazonaws.com"
]
}
}
}
# https://docs.aws.amazon.com/vpc/latest/userguide/flow-logs-s3.html
data "aws_iam_policy_document" "bucket" {
count = module.this.enabled ? 1 : 0
statement {
sid = "AWSLogDeliveryWrite"
principals {
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}
actions = [
"s3:PutObject"
]
resources = [
"${var.arn_format}:s3:::${module.this.id}/*"
]
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = [
"bucket-owner-full-control"
]
}
}
statement {
sid = "AWSLogDeliveryAclCheck"
principals {
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}
actions = [
"s3:GetBucketAcl"
]
resources = [
"${var.arn_format}:s3:::${module.this.id}"
]
}
}
module "kms_key" {
source = "cloudposse/kms-key/aws"
version = "0.7.0"
description = "KMS key for VPC Flow Logs"
deletion_window_in_days = 10
enable_key_rotation = true
policy = join("", data.aws_iam_policy_document.kms.*.json)
context = module.this.context
}
module "s3_log_storage_bucket" {
source = "cloudposse/s3-log-storage/aws"
version = "0.15.0"
kms_master_key_arn = module.kms_key.alias_arn
sse_algorithm = "aws:kms"
versioning_enabled = false
expiration_days = var.expiration_days
glacier_transition_days = var.glacier_transition_days
lifecycle_prefix = var.lifecycle_prefix
lifecycle_rule_enabled = var.lifecycle_rule_enabled
lifecycle_tags = var.lifecycle_tags
noncurrent_version_expiration_days = var.noncurrent_version_expiration_days
noncurrent_version_transition_days = var.noncurrent_version_transition_days
standard_transition_days = var.standard_transition_days
force_destroy = var.force_destroy
policy = join("", data.aws_iam_policy_document.bucket.*.json)
context = module.this.context
}
resource "aws_flow_log" "default" {
count = module.this.enabled && var.flow_log_enabled ? 1 : 0
log_destination = module.s3_log_storage_bucket.bucket_arn
log_destination_type = "s3"
traffic_type = var.traffic_type
vpc_id = var.vpc_id
}