forked from DNXLabs/terraform-aws-sns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda-slack.tf
104 lines (87 loc) · 2.5 KB
/
lambda-slack.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
resource "aws_iam_role" "default" {
count = var.slack_endpoint == "" ? 0 : 1
name_prefix = "slack-lambda"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_policy" "default" {
count = var.slack_endpoint == "" ? 0 : 1
name_prefix = "cloudwatchfull-lambda"
path = "/"
description = "IAM policy for a lambda function"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:CreateLogGroup",
"cloudwatch:*"
],
"Resource": [
"arn:aws:logs:*:*:*",
"arn:aws:cloudwatch:*:*:*"
],
"Effect": "Allow"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "lambda_logs" {
count = var.slack_endpoint == "" ? 0 : 1
role = aws_iam_role.default[0].name
policy_arn = aws_iam_policy.default[0].arn
}
resource "random_string" "lambda_suffix" {
length = 8
special = false
lower = true
numeric = false
}
resource "aws_lambda_function" "default" {
count = var.slack_endpoint == "" ? 0 : 1
filename = "${path.module}/slack.zip"
function_name = "slack-cloudwatch-notification-${var.sns_topic_name != "" ? var.sns_topic_name : random_string.lambda_suffix.result}"
role = aws_iam_role.default[0].arn
handler = "index.handler"
source_code_hash = filebase64sha256("${path.module}/slack.zip")
runtime = "nodejs16.x"
environment {
variables = {
UNENCRYPTED_HOOK_URL = var.slack_endpoint
}
}
}
resource "aws_lambda_permission" "with_sns" {
count = var.slack_endpoint == "" ? 0 : 1
statement_id = "AllowExecutionFromSNS"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.default[0].function_name
principal = "sns.amazonaws.com"
source_arn = var.sns_topic_arn != "" ? var.sns_topic_arn : aws_sns_topic.default[0].arn
}
resource "aws_sns_topic_subscription" "lambda_subscription" {
count = var.slack_endpoint == "" ? 0 : 1
#topic_arn = data.aws_sns_topic.health_topic_client.arn
#endpoint = data.aws_lambda_function.slack-lambda-function.arn
topic_arn = var.sns_topic_arn != "" ? var.sns_topic_arn : aws_sns_topic.default[0].arn
protocol = "lambda"
endpoint = aws_lambda_function.default[0].arn
depends_on = [aws_lambda_function.default]
}