-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
main.tf
137 lines (112 loc) · 3.23 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
data "aws_caller_identity" "default" {}
module "sns_kms_key_label" {
source = "cloudposse/label/null"
version = "0.25.0"
count = local.create_kms_key ? 1 : 0
attributes = ["sns"]
context = module.this.context
}
module "sns_kms_key" {
source = "cloudposse/kms-key/aws"
version = "0.10.0"
count = local.create_kms_key ? 1 : 0
name = local.create_kms_key ? module.sns_kms_key_label[0].id : ""
description = "KMS key for the ${local.alert_for} SNS topic"
enable_key_rotation = true
alias = "alias/${local.alert_for}-sns"
policy = local.create_kms_key ? data.aws_iam_policy_document.sns_kms_key_policy[0].json : ""
context = module.this.context
}
data "aws_iam_policy_document" "sns_kms_key_policy" {
count = local.create_kms_key ? 1 : 0
policy_id = "CloudWatchEncryptUsingKey"
statement {
effect = "Allow"
actions = [
"kms:*"
]
resources = ["*"]
principals {
type = "AWS"
identifiers = ["arn:aws:iam::${data.aws_caller_identity.default.account_id}:root"]
}
}
statement {
effect = "Allow"
actions = [
"kms:Decrypt",
"kms:GenerateDataKey"
]
resources = ["*"]
principals {
type = "Service"
identifiers = ["cloudwatch.amazonaws.com"]
}
}
}
module "aws_sns_topic_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["cloudtrail-breach"]
context = module.this.context
}
resource "aws_sns_topic" "default" {
count = local.enabled ? 1 : 0
name = module.aws_sns_topic_label.id
tags = module.this.tags
kms_master_key_id = local.create_kms_key ? module.sns_kms_key[0].key_id : var.kms_master_key_id
}
resource "aws_sns_topic_policy" "default" {
count = local.enabled == true && var.sns_policy_enabled == true ? 1 : 0
arn = local.sns_topic_arn
policy = data.aws_iam_policy_document.sns_topic_policy.json
}
data "aws_iam_policy_document" "sns_topic_policy" {
policy_id = "__default_policy_ID"
statement {
sid = "__default_statement_ID"
actions = [
"SNS:Subscribe",
"SNS:SetTopicAttributes",
"SNS:RemovePermission",
"SNS:Receive",
"SNS:Publish",
"SNS:ListSubscriptionsByTopic",
"SNS:GetTopicAttributes",
"SNS:DeleteTopic",
"SNS:AddPermission",
]
effect = "Allow"
resources = [local.sns_topic_arn]
principals {
type = "AWS"
identifiers = ["*"]
}
condition {
test = "StringEquals"
variable = "AWS:SourceOwner"
values = [
"arn:aws:iam::${data.aws_caller_identity.default.account_id}:root",
]
}
}
statement {
sid = "Allow ${local.alert_for} CloudwatchEvents"
actions = ["sns:Publish"]
resources = [local.sns_topic_arn]
principals {
type = "AWS"
identifiers = ["*"]
}
condition {
test = "StringEquals"
variable = "AWS:SourceArn"
values = local.metric_alarms_arns
}
}
}
locals {
enabled = module.this.enabled
create_kms_key = local.enabled && var.kms_master_key_id == null
metric_alarms_arns = [for i in aws_cloudwatch_metric_alarm.default : i.arn]
}