generated from geekcell/terraform-aws-module-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
147 lines (116 loc) · 4.01 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
/**
* # Terraform AWS CloudTrail Alerts Module
*
* A module that create CloudWatch metric filters and alarms required for most modern compliance reports. This
* module includes the necessary metric filters and alarms for the following compliance reports:
*
* | Compliance Report | Sections |
* |---|---|
* | CIS AWS Foundations Benchmark v1.5.0 | Section 4.1 - 4.15 |
* | NIST 800-171 v2 | Section 3.12.3 |
* | ISO/IEC 27001 v2 | Section A.12.4.1 |
* | PCI DSS v3.2.1 | Section 10.1 |
* | SOC 2 v2 | Section 5.2 |
*
* This module can also create an SNS topic with a Slack channel configuration for AWS Chatbot (must be configured)
* manually in the AWS Console.
*/
data "aws_caller_identity" "current" {}
data "aws_cloudwatch_log_group" "cloudtrail" {
name = var.cloudtrail_log_group_name
}
resource "aws_cloudwatch_log_metric_filter" "main" {
for_each = { for rule in local.alerts : rule.name => rule }
name = each.value.name
pattern = each.value.pattern
log_group_name = data.aws_cloudwatch_log_group.cloudtrail.name
metric_transformation {
name = "${each.value.name}Count"
namespace = var.cloudwatch_namespace
value = 1
}
}
resource "aws_cloudwatch_metric_alarm" "main" {
for_each = { for rule in local.alerts : rule.name => rule }
alarm_name = "${each.value.name}Alarm"
metric_name = "${each.value.name}Count"
namespace = var.cloudwatch_namespace
evaluation_periods = each.value.evaluation_periods
threshold = each.value.threshold
period = each.value.period
comparison_operator = each.value.comparison_operator
statistic = each.value.statistic
alarm_description = each.value.description
alarm_actions = [coalesce(var.sns_topic_arn, try(aws_sns_topic.main[0].arn, null))]
treat_missing_data = "notBreaching"
tags = var.tags
}
## KMS
resource "aws_kms_key" "main" {
count = var.sns_kms_master_key_id == null ? 1 : 0
description = "KMS key for CloudTrail alerts SNS topic."
policy = data.aws_iam_policy_document.kms[0].json
deletion_window_in_days = 7
enable_key_rotation = true
tags = var.tags
}
resource "aws_kms_alias" "main" {
count = var.sns_kms_master_key_id == null ? 1 : 0
target_key_id = aws_kms_key.main[0].id
name = var.sns_kms_master_key_alias
}
data "aws_iam_policy_document" "kms" {
count = var.sns_kms_master_key_id == null ? 1 : 0
statement {
sid = "IAMUserAdministration"
resources = ["*"]
actions = ["kms:*"]
principals {
type = "AWS"
identifiers = [
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:root",
]
}
}
statement {
sid = "CloudwatchUsage"
resources = ["*"]
actions = [
"kms:Decrypt",
"kms:GenerateDataKey*"
]
principals {
type = "Service"
identifiers = ["cloudwatch.amazonaws.com"]
}
}
}
## SNS
resource "aws_sns_topic" "main" {
count = var.sns_topic_arn == null ? 1 : 0
name = "${var.prefix}-cloudtrail-alerts"
kms_master_key_id = coalesce(var.sns_kms_master_key_id, aws_kms_key.main[0].arn)
tags = var.tags
}
## CHATBOT
resource "awscc_chatbot_slack_channel_configuration" "main" {
count = var.slack_channel_id != null && var.slack_workspace_id != null ? 1 : 0
configuration_name = "${var.prefix}-cloudtrail-alerts"
slack_channel_id = var.slack_channel_id
slack_workspace_id = var.slack_workspace_id
sns_topic_arns = [coalesce(var.sns_topic_arn, aws_sns_topic.main[0].arn)]
iam_role_arn = module.chatbot_role[0].arn
}
module "chatbot_role" {
count = var.slack_channel_id != null && var.slack_workspace_id != null ? 1 : 0
source = "github.com/geekcell/terraform-aws-iam-role?ref=v1"
name = "${var.prefix}-chatbot-cloudtrail-alerts"
description = "Role for AWS Chatbot to read CloudWatch alerts."
policy_arns = ["arn:aws:iam::aws:policy/CloudWatchReadOnlyAccess"]
assume_roles = {
"Service" : {
identifiers = ["chatbot.amazonaws.com"]
}
}
tags = var.tags
}