-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
225 lines (197 loc) · 6.33 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
provider "aws" {
version = "~> 2.0"
region = "${var.aws_region}"
}
locals {
package_json = jsondecode(file("./package.json"))
build_folder = "./deploy"
target_pkg_name = "${local.package_json.name}-${local.package_json.version}"
# Namespace for the param path
ssm_param_ns = "dev/application"
}
data "aws_caller_identity" "current" {}
resource "aws_s3_bucket" "lambda_bucket" {
bucket = var.s3_bucket_name
acl = "private"
tags = {
Name = "Dev Bucket"
Environment = "Dev"
}
}
resource "aws_kms_key" "default" {
description = "Default encryption key (symmetric)"
deletion_window_in_days = 10
}
resource "aws_ssm_parameter" "slack_webhook_url" {
name = "/${local.ssm_param_ns}/slack_webhook/url"
description = "Datbase password"
type = "SecureString"
key_id = aws_kms_key.default.key_id
value = var.slack_webhook_endpoint_url
tags = {
environment = "dev"
}
}
resource "aws_s3_bucket_object" "default" {
bucket = "${aws_s3_bucket.lambda_bucket.id}"
key = local.target_pkg_name
source = "./${local.build_folder}/${local.target_pkg_name}.zip"
etag = "${filemd5("./${local.build_folder}/${local.target_pkg_name}.zip")}"
}
resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_lambda_function" "log_lambda" {
function_name = "${var.lambda_func_ns}-print-log-lambda"
s3_bucket = "${aws_s3_bucket.lambda_bucket.id}"
s3_key = "${aws_s3_bucket_object.default.id}"
handler = "dist/index.logLambda"
role = "${aws_iam_role.iam_for_lambda.arn}"
timeout = 300
source_code_hash = "${filebase64sha256("${local.build_folder}/${local.target_pkg_name}.zip")}"
runtime = "nodejs12.x"
depends_on = [
"aws_iam_role_policy_attachment.attach_lambda_role_logs",
"aws_cloudwatch_log_group.log_lambda"
]
environment {
variables = {
name = "${var.lambda_func_ns}-print-log-lambda"
}
}
}
resource "aws_lambda_function" "error_processing_lambda" {
function_name = "${var.lambda_func_ns}-error-processing-lambda"
s3_bucket = "${aws_s3_bucket.lambda_bucket.id}"
s3_key = "${aws_s3_bucket_object.default.id}"
handler = "dist/index.processLambdaErrorLog"
role = "${aws_iam_role.iam_for_lambda.arn}"
timeout = 300
source_code_hash = "${filebase64sha256("${local.build_folder}/${local.target_pkg_name}.zip")}"
runtime = "nodejs12.x"
depends_on = [
"aws_iam_role_policy_attachment.attach_lambda_role_logs",
"aws_cloudwatch_log_group.log_lambda"
]
environment {
variables = {
name = "${var.lambda_func_ns}-error-processing-lambda"
#SLACK_MS_DEBUG = true
SLACK_MS_CHANNEL = "general"
}
}
}
resource "aws_cloudwatch_log_group" "log_lambda" {
name = "/aws/lambda/${var.lambda_func_ns}-print-log-lambda"
retention_in_days = 1
}
resource "aws_cloudwatch_log_group" "error_processing_lambda" {
name = "/aws/lambda/${var.lambda_func_ns}-error-processing-lambda"
retention_in_days = 1
}
data "aws_iam_policy_document" "lambda_cw_log_policy" {
version = "2012-10-17"
statement {
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
effect = "Allow"
resources = ["arn:aws:logs:*:*:*"]
}
statement {
actions = [
"ssm:GetParameters",
"ssm:GetParametersByPath"
]
resources = [
"arn:aws:ssm:${var.aws_region}:${data.aws_caller_identity.current.account_id}:parameter/${local.ssm_param_ns}*"
]
effect = "Allow"
}
statement {
actions = ["kms:Decrypt"]
resources = ["${aws_kms_key.default.arn}"]
effect = "Allow"
}
}
resource "aws_lambda_permission" "allow_cloudwatch_logs" {
statement_id = "AllowExecutionFromCloudWatchLogs"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.error_processing_lambda.arn
principal = "logs.${var.aws_region}.amazonaws.com"
source_arn = aws_cloudwatch_log_group.log_lambda.arn
}
resource "aws_iam_policy" "lambda_logging" {
name = "lambda_logging"
path = "/"
description = "IAM Policy for logging from a lambda"
policy = data.aws_iam_policy_document.lambda_cw_log_policy.json
}
resource "aws_iam_role_policy_attachment" "attach_lambda_role_logs" {
role = "${aws_iam_role.iam_for_lambda.name}"
policy_arn = "${aws_iam_policy.lambda_logging.arn}"
}
resource "aws_cloudwatch_log_subscription_filter" "error_lambda_logfilter" {
name = "lambda_error_log_filter"
log_group_name = aws_cloudwatch_log_group.log_lambda.name
filter_pattern = "ERROR"
destination_arn = aws_lambda_function.error_processing_lambda.arn
distribution = "Random"
depends_on = [
"aws_lambda_permission.allow_cloudwatch_logs"
]
}
data "aws_iam_policy_document" "queue_sns_topic_policy" {
version = "2012-10-17"
policy_id = "sqs_sns_topic_policy"
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["sns.amazonaws.com"]
}
actions = [
"sqs:sendMessage"
]
resources = [aws_sqs_queue.log_error_queue.arn]
condition {
test = "ArnEquals"
variable = "aws:SourceArn"
values = [
aws_sns_topic.log_error_topic.arn
]
}
}
}
resource "aws_sqs_queue_policy" "default" {
queue_url = aws_sqs_queue.log_error_queue.id
policy = data.aws_iam_policy_document.queue_sns_topic_policy.json
}
resource "aws_sns_topic" "log_error_topic" {
name = "log-error-notification-topic"
}
resource "aws_sqs_queue" "log_error_queue" {
name = "log-error-notification-queue"
}
resource "aws_sns_topic_subscription" "log_error_notify_sub" {
topic_arn = aws_sns_topic.log_error_topic.arn
protocol = "sqs"
endpoint = aws_sqs_queue.log_error_queue.arn
}