forked from OtavioBernardes/terraform-aws-docdb-autoscaling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.tf
91 lines (78 loc) · 3.11 KB
/
lambda.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
# Scaleup Lambda function
resource "aws_lambda_function" "scaleup" {
filename = local.output_path
function_name = "${var.name}-scaleup"
role = aws_iam_role.lambda.arn
handler = "index.scaleup"
source_code_hash = data.archive_file.source_code.output_base64sha256
runtime = "python3.9"
environment {
variables = {
min_capacity = tostring(var.min_capacity)
max_capacity = tostring(var.max_capacity)
cluster_identifier = var.cluster_identifier
scaleup_alarm_name = aws_cloudwatch_metric_alarm.scaleup.alarm_name
metric_name = var.scaling_policy.metric_name
target = tostring(var.scaling_policy.target)
scaledown_target = tostring(var.scaling_policy.scaledown_target)
statistic = var.scaling_policy.statistic
period = tostring(var.scaling_policy.period)
cooldown = tostring(var.scaling_policy.cooldown)
}
}
depends_on = [
aws_cloudwatch_log_group.scaleup,
data.archive_file.source_code
]
}
resource "aws_lambda_permission" "sns" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.scaleup.arn
principal = "sns.amazonaws.com"
statement_id = "AllowSubscriptionToSNS"
source_arn = aws_sns_topic.scaleup.arn
}
# Scaledown Lambda function
resource "aws_lambda_function" "scaledown" {
filename = local.output_path
function_name = "${var.name}-scaledown"
role = aws_iam_role.lambda.arn
handler = "index.scaledown"
source_code_hash = data.archive_file.source_code.output_base64sha256
runtime = "python3.9"
environment {
variables = {
min_capacity = tostring(var.min_capacity)
max_capacity = tostring(var.max_capacity)
cluster_identifier = var.cluster_identifier
scaleup_alarm_name = aws_cloudwatch_metric_alarm.scaleup.alarm_name
metric_name = var.scaling_policy.metric_name
target = tostring(var.scaling_policy.target)
scaledown_target = tostring(var.scaling_policy.scaledown_target)
statistic = var.scaling_policy.statistic
period = tostring(var.scaling_policy.period)
cooldown = tostring(var.scaling_policy.cooldown)
}
}
depends_on = [
aws_cloudwatch_log_group.scaledown,
data.archive_file.source_code
]
}
resource "aws_cloudwatch_event_rule" "scaledown" {
name = "DocDB-${var.cluster_identifier}-Scaledown"
description = "Schedule for DocDB ${var.cluster_identifier} scaledown function"
schedule_expression = var.scaledown_schedule
}
resource "aws_cloudwatch_event_target" "scaledown" {
rule = aws_cloudwatch_event_rule.scaledown.name
target_id = "Scaledown-DocDB-${var.cluster_identifier}"
arn = aws_lambda_function.scaledown.arn
}
resource "aws_lambda_permission" "scaledown" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.scaledown.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.scaledown.arn
}