-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
112 lines (88 loc) · 2.33 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
data "aws_caller_identity" "default" {
count = module.this.enabled ? 1 : 0
}
module "topic_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["rds", var.db_instance_id, "threshold", "alerts"]
context = module.this.context
}
resource "aws_sns_topic" "default" {
count = module.this.enabled ? 1 : 0
name = module.topic_label.id
}
module "subscription_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["rds", var.db_instance_id, "event", "sub"]
context = module.this.context
}
resource "aws_db_event_subscription" "default" {
count = module.this.enabled ? 1 : 0
name = module.subscription_label.id
sns_topic = join("", aws_sns_topic.default.*.arn)
source_type = "db-instance"
source_ids = [var.db_instance_id]
event_categories = [
"failover",
"failure",
"low storage",
"maintenance",
"notification",
"recovery",
]
depends_on = [
aws_sns_topic_policy.default
]
}
resource "aws_sns_topic_policy" "default" {
count = module.this.enabled ? 1 : 0
arn = join("", aws_sns_topic.default.*.arn)
policy = join("", data.aws_iam_policy_document.sns_topic_policy.*.json)
}
data "aws_iam_policy_document" "sns_topic_policy" {
count = module.this.enabled ? 1 : 0
statement {
sid = "AllowManageSNS"
actions = [
"SNS:Subscribe",
"SNS:SetTopicAttributes",
"SNS:RemovePermission",
"SNS:Receive",
"SNS:Publish",
"SNS:ListSubscriptionsByTopic",
"SNS:GetTopicAttributes",
"SNS:DeleteTopic",
"SNS:AddPermission",
]
effect = "Allow"
resources = aws_sns_topic.default.*.arn
principals {
type = "AWS"
identifiers = ["*"]
}
condition {
test = "StringEquals"
variable = "AWS:SourceOwner"
values = data.aws_caller_identity.default.*.account_id
}
}
statement {
sid = "Allow CloudwatchEvents"
actions = ["sns:Publish"]
resources = aws_sns_topic.default.*.arn
principals {
type = "Service"
identifiers = ["events.amazonaws.com"]
}
}
statement {
sid = "Allow RDS Event Notification"
actions = ["sns:Publish"]
resources = aws_sns_topic.default.*.arn
principals {
type = "Service"
identifiers = ["rds.amazonaws.com"]
}
}
}