-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
97 lines (82 loc) · 2.4 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
locals {
parameter_name = "/${var.namespace}/${var.stage}/${var.name}/Terraform-SNS-Trigger-Hash"
}
data "aws_caller_identity" "default" {}
resource "aws_ssm_parameter" "default" {
name = "${local.parameter_name}"
description = "Force an SNS event via cloudwatch and Parameter Store for ${module.label.id}"
type = "String"
value = "${var.trigger_hash}"
tags = "${var.tags}"
depends_on = ["aws_cloudwatch_event_rule.default", "aws_sns_topic_policy.default"]
}
resource "aws_cloudwatch_event_rule" "default" {
name = "${module.label.id}-sns-notify"
description = "Force an SNS event via cloudwatch and Parameter Store for ${module.label.id}"
event_pattern = <<PATTERN
{
"source": [
"aws.ssm"
],
"detail-type": [
"Parameter Store Change"
],
"detail": {
"operation": ["Update", "Create"],
"name": ["${local.parameter_name}"]
}
}
PATTERN
}
resource "aws_cloudwatch_event_target" "sns" {
rule = "${aws_cloudwatch_event_rule.default.name}"
target_id = "SendToSNS"
arn = "${var.sns_topic_arn}"
depends_on = ["aws_cloudwatch_event_rule.default"]
input = "${var.sns_message_override}"
}
resource "aws_sns_topic_policy" "default" {
count = "${var.add_events_to_sns_policy == "true" ? 1 : 0}"
arn = "${var.sns_topic_arn}"
policy = "${data.aws_iam_policy_document.sns_topic_policy.0.json}"
}
data "aws_iam_policy_document" "sns_topic_policy" {
count = "${var.add_events_to_sns_policy == "true" ? 1 : 0}"
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 = ["${var.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 CloudwatchEvents"
actions = ["sns:Publish"]
resources = ["${var.sns_topic_arn}"]
principals {
type = "Service"
identifiers = ["events.amazonaws.com"]
}
}
}