-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcloudwatch.tf
More file actions
51 lines (45 loc) · 1.63 KB
/
cloudwatch.tf
File metadata and controls
51 lines (45 loc) · 1.63 KB
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
resource "aws_cloudwatch_log_group" "this" {
name = "/aws/lambda/${var.name}"
retention_in_days = 90
tags = {
Name = "/aws/lambda/${var.name}"
}
kms_key_id = var.logs_kms_key_arn
}
resource "aws_cloudwatch_metric_alarm" "this" {
count = length(var.alarm_actions) > 0 ? 1 : 0
alarm_name = var.name
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
period = "60"
statistic = "Sum"
threshold = "0"
alarm_description = coalesce(var.alarm_description, "${var.name} invocation error")
actions_enabled = true
alarm_actions = var.alarm_actions
namespace = "AWS/Lambda"
metric_name = "Errors"
dimensions = {
FunctionName = aws_lambda_function.this.function_name
}
}
resource "aws_cloudwatch_event_rule" "this" {
count = var.schedule_expression == null ? 0 : 1
name = "${var.name}-trigger"
description = "${var.name}-trigger"
schedule_expression = var.schedule_expression
}
resource "aws_cloudwatch_event_target" "this" {
count = var.schedule_expression == null ? 0 : 1
rule = aws_cloudwatch_event_rule.this[0].name
target_id = aws_cloudwatch_event_rule.this[0].name
arn = aws_lambda_function.this.arn
}
resource "aws_lambda_permission" "this" {
count = var.schedule_expression == null ? 0 : 1
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.this.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.this[0].arn
}