-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevent-bridge.tf
111 lines (97 loc) · 2.59 KB
/
event-bridge.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
module "eventbridge" {
providers = {
aws = aws.us-east-1
}
role_name = "${local.name}-eventbridge"
source = "terraform-aws-modules/eventbridge/aws"
create_bus = false
rules = {
"${local.name}-add-remove-user" = {
description = "Trigger wg-manage lambda"
event_pattern = jsonencode(
{
"source": ["aws.iam"],
"detail-type": ["AWS API Call via CloudTrail"],
"detail": {
"eventSource": ["iam.amazonaws.com"],
"eventName" : ["AddUserToGroup", "RemoveUserFromGroup"],
"requestParameters": {
"groupName": [var.wg_group_name]
}
}
}
)
enabled = true
}
}
targets = {
"${local.name}-add-remove-user" = [
{
name = "Trigger wireguard manage lambda"
arn = aws_sns_topic.wireguard_group_change_notification.arn
}
]
}
}
resource "aws_sns_topic_policy" "default" {
provider = aws.us-east-1
arn = aws_sns_topic.wireguard_group_change_notification.arn
policy = data.aws_iam_policy_document.wg_group_change_sns_topic_policy.json
}
data "aws_iam_policy_document" "wg_group_change_sns_topic_policy" {
policy_id = "__default_policy_ID"
statement {
actions = [
"SNS:Subscribe",
"SNS:SetTopicAttributes",
"SNS:RemovePermission",
"SNS:Receive",
"SNS:Publish",
"SNS:ListSubscriptionsByTopic",
"SNS:GetTopicAttributes",
"SNS:DeleteTopic",
"SNS:AddPermission",
]
condition {
test = "StringEquals"
variable = "AWS:SourceOwner"
values = [
local.account
]
}
effect = "Allow"
principals {
type = "AWS"
identifiers = ["*"]
}
resources = [
aws_sns_topic.wireguard_group_change_notification.arn
]
sid = "__default_statement_ID"
}
statement {
sid = "__publish_from_eventbridge"
actions = [
"sns:Publish"
]
effect = "Allow"
principals {
type = "Service"
identifiers = ["events.amazonaws.com"]
}
resources = [
aws_sns_topic.wireguard_group_change_notification.arn
]
}
}
resource "aws_sns_topic" "wireguard_group_change_notification" {
provider = aws.us-east-1
name = "${local.name}-group-change-notification"
display_name = "${local.name}-group-change-notification"
}
resource "aws_sns_topic_subscription" "trigger_wg_manage_lambda" {
provider = aws.us-east-1
topic_arn = aws_sns_topic.wireguard_group_change_notification.arn
protocol = "lambda"
endpoint = module.wg_manage.lambda_function_arn
}