-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon-webhook.tf
83 lines (71 loc) · 1.91 KB
/
common-webhook.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
resource "aws_sns_topic" "webhook" {
name = "parkingspace-sns-cicd"
}
data "aws_iam_policy_document" "webhook_webhook" {
statement {
actions = ["sns:Publish"]
principals {
type = "Service"
identifiers = ["codestar-notifications.amazonaws.com"]
}
resources = [aws_sns_topic.webhook.arn]
}
}
resource "aws_sns_topic_policy" "webhook" {
arn = aws_sns_topic.webhook.arn
policy = data.aws_iam_policy_document.webhook_webhook.json
}
data "archive_file" "webhook_notification" {
type = "zip"
source_dir = "${path.module}/lambda/"
output_path = "${path.module}/output/lambda.zip"
}
resource "aws_lambda_function" "webhook_notification" {
filename = data.archive_file.webhook_notification.output_path
function_name = "parkingspace-lambda-cicd-notification"
handler = "notification.handler"
source_code_hash = data.archive_file.webhook_notification.output_base64sha256
role = aws_iam_role.webhook_notification.arn
runtime = "nodejs16.x"
environment {
variables = {
WEBHOOK_ID = ""
WEBHOOK_TOKEN = ""
}
}
lifecycle {
ignore_changes = [
environment
]
}
}
resource "aws_iam_role" "webhook_notification" {
name = "parkingspace-role-cicd-notification"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_sns_topic_subscription" "webhook_notification" {
topic_arn = aws_sns_topic.webhook.arn
protocol = "lambda"
endpoint = aws_lambda_function.webhook_notification.arn
}
resource "aws_lambda_permission" "webhook_notification" {
statement_id = "AllowExecutionFromSNS"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.webhook_notification.arn
principal = "sns.amazonaws.com"
source_arn = aws_sns_topic.webhook.arn
}