forked from terraform-aws-modules/terraform-aws-notify-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
84 lines (66 loc) · 2.19 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
data "aws_sns_topic" "this" {
count = "${(1 - var.create_sns_topic) * var.create}"
name = "${var.sns_topic_name}"
}
resource "aws_sns_topic" "this" {
count = "${var.create_sns_topic * var.create}"
name = "${var.sns_topic_name}"
}
locals {
sns_topic_arn = "${element(concat(aws_sns_topic.this.*.arn, data.aws_sns_topic.this.*.arn, list("")), 0)}"
}
resource "aws_sns_topic_subscription" "sns_notify_slack" {
count = "${var.create}"
topic_arn = "${local.sns_topic_arn}"
protocol = "lambda"
endpoint = "${aws_lambda_function.notify_slack.0.arn}"
}
resource "aws_lambda_permission" "sns_notify_slack" {
count = "${var.create}"
statement_id = "AllowExecutionFromSNS"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.notify_slack.0.function_name}"
principal = "sns.amazonaws.com"
source_arn = "${local.sns_topic_arn}"
}
data "null_data_source" "lambda_file" {
inputs {
filename = "${substr("${path.module}/functions/notify_slack.py", length(path.cwd) + 1, -1)}"
}
}
data "null_data_source" "lambda_archive" {
inputs {
filename = "${substr("${path.module}/functions/notify_slack.zip", length(path.cwd) + 1, -1)}"
}
}
data "archive_file" "notify_slack" {
count = "${var.create}"
type = "zip"
source_file = "${data.null_data_source.lambda_file.outputs.filename}"
output_path = "${data.null_data_source.lambda_archive.outputs.filename}"
}
resource "aws_lambda_function" "notify_slack" {
count = "${var.create}"
filename = "${data.archive_file.notify_slack.0.output_path}"
function_name = "${var.lambda_function_name}"
role = "${aws_iam_role.lambda.arn}"
handler = "notify_slack.lambda_handler"
source_code_hash = "${data.archive_file.notify_slack.0.output_base64sha256}"
runtime = "python3.6"
timeout = 30
kms_key_arn = "${var.kms_key_arn}"
environment {
variables = {
SLACK_WEBHOOK_URL = "${var.slack_webhook_url}"
SLACK_CHANNEL = "${var.slack_channel}"
SLACK_USERNAME = "${var.slack_username}"
SLACK_EMOJI = "${var.slack_emoji}"
}
}
lifecycle {
ignore_changes = [
"filename",
"last_modified",
]
}
}