generated from appvia/terraform-aws-module-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
118 lines (107 loc) · 2.8 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
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
resource "aws_flow_log" "vpc" {
log_destination = aws_cloudwatch_log_group.default.arn
iam_role_arn = aws_iam_role.log.arn
vpc_id = var.vpc_id
traffic_type = var.traffic_type
}
resource "aws_cloudwatch_log_group" "default" {
name = var.name
retention_in_days = var.retention_in_days
kms_key_id = aws_kms_key.log.arn
}
data "aws_iam_policy_document" "log_assume" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["vpc-flow-logs.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "log" {
statement {
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogGroups",
"logs:DescribeLogStreams",
]
resources = [
aws_cloudwatch_log_group.default.arn,
"${aws_cloudwatch_log_group.default.arn}:*"
]
condition {
test = "StringEqualsIfExists"
variable = "aws:ResourceAccount"
values = [data.aws_caller_identity.current.account_id]
}
}
}
resource "aws_iam_role_policy" "log" {
name = var.name
role = aws_iam_role.log.id
policy = data.aws_iam_policy_document.log.json
}
resource "aws_iam_role" "log" {
name = var.name
assume_role_policy = data.aws_iam_policy_document.log_assume.json
}
resource "aws_kms_key" "log" {
description = "An KMS key for encrypting cloudwatch logs"
enable_key_rotation = true
deletion_window_in_days = 20
policy = jsonencode({
Version = "2012-10-17"
Id = "key-default-1"
Statement = [
{
Sid = "Enable IAM User Permissions"
Effect = "Allow"
Principal = {
AWS = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
},
Action = "kms:*"
Resource = "*"
},
{
Sid = "Allow administration of the key"
Effect = "Allow"
Principal = {
AWS = data.aws_caller_identity.current.arn
},
Action = [
"kms:ReplicateKey",
"kms:Create*",
"kms:Describe*",
"kms:Enable*",
"kms:List*",
"kms:Put*",
"kms:Update*",
"kms:Revoke*",
"kms:Disable*",
"kms:Get*",
"kms:Delete*",
"kms:ScheduleKeyDeletion",
"kms:CancelKeyDeletion"
],
Resource = "*"
},
{
Effect = "Allow"
Principal = {
Service = "logs.${data.aws_region.current.name}.amazonaws.com"
}
Action = [
"kms:Encrypt*",
"kms:Decrypt*",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Describe*"
]
Resource = "*"
}
]
})
}