-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_ec2trigger.tf
35 lines (32 loc) · 946 Bytes
/
lambda_ec2trigger.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
resource "aws_cloudwatch_event_rule" "lambda_rule" {
name = "${var.short_name}-JoinDirectoryServiceDomain"
description = "Captures notifications of EC2 being started"
event_pattern = <<EOF
{
"source": [
"aws.ec2"
],
"detail-type": [
"EC2 Instance State-change Notification"
],
"detail": {
"state": [
"terminated",
"pending"
]
}
}
EOF
}
resource "aws_cloudwatch_event_target" "lambda_joiner" {
arn = "${aws_lambda_function.lambda_joiner.arn}"
rule = "${aws_cloudwatch_event_rule.lambda_rule.id}"
target_id = "Ec2StateChange"
}
resource "aws_lambda_permission" "allow_cloudwatch_to_call_handle_function" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.lambda_joiner.function_name}"
principal = "events.amazonaws.com"
source_arn = "${aws_cloudwatch_event_rule.lambda_rule.arn}"
}