-
Notifications
You must be signed in to change notification settings - Fork 18
/
cloudwatch.tf
56 lines (48 loc) · 1.53 KB
/
cloudwatch.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
# Trigger the Lambda function with these instance events.
resource "aws_cloudwatch_event_rule" "events" {
name = var.name
event_pattern = <<PATTERN
{
"source": [
"aws.autoscaling",
"aws.ec2"
],
"detail-type": [
"EC2 Instance Launch Successful",
"EC2 Instance State-change Notification"
]
}
PATTERN
}
resource "aws_cloudwatch_event_target" "events" {
target_id = "${var.name}-events"
rule = aws_cloudwatch_event_rule.events.name
arn = module.lambda.function_arn
}
resource "aws_lambda_permission" "events" {
statement_id = "${var.name}-events"
action = "lambda:InvokeFunction"
function_name = module.lambda.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.events.arn
}
# Also check for changes regularly.
resource "aws_cloudwatch_event_rule" "schedule" {
count = var.schedule == "" ? 0 : 1
name = "${var.name}-schedule"
schedule_expression = var.schedule
}
resource "aws_cloudwatch_event_target" "schedule" {
count = var.schedule == "" ? 0 : 1
target_id = "${var.name}-schedule"
rule = aws_cloudwatch_event_rule.schedule[0].name
arn = module.lambda.function_arn
}
resource "aws_lambda_permission" "schedule" {
count = var.schedule == "" ? 0 : 1
statement_id = "${var.name}-schedule"
action = "lambda:InvokeFunction"
function_name = module.lambda.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.schedule[0].arn
}