forked from cloudandthings/terraform-aws-clickops-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
162 lines (128 loc) · 5.1 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
data "aws_iam_policy_document" "lambda_permissions" {
statement {
sid = "SSMAccess"
actions = ["ssm:GetParameter"]
resources = ["arn:aws:ssm:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:parameter/${var.naming_prefix}/webhooks-*"]
}
# Organizational deployment
dynamic "statement" {
for_each = !var.standalone ? toset(["organizational_deployment"]) : toset([])
content {
sid = "S3AccessBucket"
actions = ["s3:ListBucket"]
resources = ["arn:aws:s3:::${var.cloudtrail_bucket_name}"]
}
}
dynamic "statement" {
for_each = !var.standalone ? toset(["organizational_deployment"]) : toset([])
content {
sid = "S3AccessBucketObject"
actions = ["s3:GetObject"]
resources = ["arn:aws:s3:::${var.cloudtrail_bucket_name}/*"]
}
}
dynamic "statement" {
for_each = !var.standalone ? toset(["organizational_deployment"]) : toset([])
content {
sid = "SQSAccess"
actions = [
"sqs:DeleteMessage",
"sqs:GetQueueAttributes",
"sqs:ReceiveMessage"
]
resources = [aws_sqs_queue.bucket_notifications[0].arn]
}
}
}
locals {
deployment_filename = "deployment-clickopsnotifier-${var.lambda_runtime}.zip"
deployment_path = "${path.module}/${local.deployment_filename}"
deployment_upload_to_s3 = var.lambda_deployment_upload_to_s3_enabled && (var.lambda_deployment_s3_bucket != null)
deployment_s3_key = coalesce(
var.lambda_deployment_s3_key,
join("/", [var.naming_prefix, local.deployment_filename])
)
}
resource "aws_s3_object" "deployment" {
count = local.deployment_upload_to_s3 ? 1 : 0
bucket = var.lambda_deployment_s3_bucket
key = local.deployment_s3_key
source = local.deployment_path
etag = filemd5(local.deployment_path)
}
module "clickops_notifier_lambda" {
source = "terraform-aws-modules/lambda/aws"
version = "4.9.0"
function_name = var.naming_prefix
description = "ClickOps Notifier Lambda"
handler = var.standalone ? "app.handler_standalone" : "app.handler_organizational"
runtime = var.lambda_runtime
timeout = var.event_processing_timeout
memory_size = var.lambda_memory_size
# Where should we get the package from?
create_package = false
local_existing_package = var.lambda_deployment_s3_bucket == null ? local.deployment_path : null
s3_existing_package = (
var.lambda_deployment_s3_bucket == null
? null
: {
bucket = var.lambda_deployment_s3_bucket
key = local.deployment_s3_key
}
)
# Publish creation/changes as a new Lambda Function Version
publish = true
create_lambda_function_url = false
# Logs
cloudwatch_logs_retention_in_days = var.log_retention_in_days
# IAM
create_role = var.create_iam_role
lambda_role = var.iam_role_arn
attach_policy_json = true
policy_json = data.aws_iam_policy_document.lambda_permissions.json
attach_policy_statements = length(var.additional_iam_policy_statements) > 0
policy_statements = var.additional_iam_policy_statements
environment_variables = {
PARAMETER_NAMES_FOR_SLACK_WEBHOOKS = jsonencode([for p in aws_ssm_parameter.webhooks_for_slack : p.name])
PARAMETER_NAMES_FOR_MSTEAMS_WEBHOOKS = jsonencode([for p in aws_ssm_parameter.webhooks_for_msteams : p.name])
EXCLUDED_ACCOUNTS = jsonencode(var.excluded_accounts)
INCLUDED_ACCOUNTS = jsonencode(var.included_accounts)
EXCLUDED_USERS = jsonencode(var.excluded_users)
INCLUDED_USERS = jsonencode(var.included_users)
EXCLUDED_SCOPED_ACTIONS = jsonencode(local.ignored_scoped_events)
LOG_LEVEL = var.lambda_log_level
FIREHOSE_DELIVERY_STREAM_NAME = coalesce(var.firehose_delivery_stream_name, "__NONE__")
}
allowed_triggers = var.standalone ? {
permission = {
statement_id = "AllowExecutionFromCloudWatch"
principal = "logs.amazonaws.com"
source_arn = "${data.aws_cloudwatch_log_group.this[0].arn}:*"
}
} : {}
event_source_mapping = var.standalone ? {} : {
src = {
event_source_arn = aws_sqs_queue.bucket_notifications[0].arn
batch_size = var.event_batch_size
maximum_batching_window_in_seconds = var.event_maximum_batching_window
}
}
tags = var.tags
depends_on = [aws_s3_object.deployment]
}
resource "aws_ssm_parameter" "webhooks_for_slack" {
for_each = nonsensitive(toset(keys(var.webhooks_for_slack_notifications)))
name = "/${var.naming_prefix}/webhooks-for-slack/${each.key}"
description = "Webhook \"${each.key}\" for clickops notifications via Slack."
type = "SecureString"
value = var.webhooks_for_slack_notifications[each.key]
tags = var.tags
}
resource "aws_ssm_parameter" "webhooks_for_msteams" {
for_each = nonsensitive(toset(keys(var.webhooks_for_msteams_notifications)))
name = "/${var.naming_prefix}/webhooks-for-msteams/${each.key}"
description = "Webhook \"${each.key}\" for clickops notifications via MS Teams."
type = "SecureString"
value = var.webhooks_for_msteams_notifications[each.key]
tags = var.tags
}