forked from terraform-aws-modules/terraform-aws-notify-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
111 lines (87 loc) · 2.86 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
data "aws_sns_topic" "this" {
count = false == var.create_sns_topic && var.create ? 1 : 0
name = var.sns_topic_name
}
resource "aws_sns_topic" "this" {
count = var.create_sns_topic && var.create ? 1 : 0
name = var.sns_topic_name
tags = merge(var.tags, var.sns_topic_tags)
}
locals {
sns_topic_arn = element(
concat(
aws_sns_topic.this.*.arn,
data.aws_sns_topic.this.*.arn,
[""],
),
0,
)
}
resource "aws_cloudwatch_log_group" "lambda" {
count = var.create ? 1 : 0
name = "/aws/lambda/${var.lambda_function_name}"
retention_in_days = var.cloudwatch_log_group_retention_in_days
kms_key_id = var.cloudwatch_log_group_kms_key_id
tags = merge(var.tags, var.cloudwatch_log_group_tags)
}
resource "aws_sns_topic_subscription" "sns_notify_slack" {
count = var.create ? 1 : 0
topic_arn = local.sns_topic_arn
protocol = "lambda"
endpoint = aws_lambda_function.notify_slack[0].arn
filter_policy = var.subsription_filter_policy
}
resource "aws_lambda_permission" "sns_notify_slack" {
count = var.create ? 1 : 0
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 = "${path.module}/functions/notify_slack.py"
}
}
data "null_data_source" "lambda_archive" {
inputs = {
filename = "${path.module}/functions/notify_slack.zip"
}
}
data "archive_file" "notify_slack" {
count = var.create ? 1 : 0
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 ? 1 : 0
filename = data.archive_file.notify_slack[0].output_path
function_name = var.lambda_function_name
description = var.lambda_description
role = aws_iam_role.lambda[0].arn
handler = "notify_slack.lambda_handler"
source_code_hash = data.archive_file.notify_slack[0].output_base64sha256
runtime = "python3.7"
timeout = 30
kms_key_arn = var.kms_key_arn
reserved_concurrent_executions = var.reserved_concurrent_executions
environment {
variables = {
SLACK_WEBHOOK_URL = var.slack_webhook_url
SLACK_CHANNEL = var.slack_channel
SLACK_USERNAME = var.slack_username
SLACK_EMOJI = var.slack_emoji
LOG_EVENTS = var.log_events ? "True" : "False"
}
}
tags = merge(var.tags, var.lambda_function_tags)
lifecycle {
ignore_changes = [
filename,
last_modified,
]
}
depends_on = [aws_cloudwatch_log_group.lambda]
}