-
Notifications
You must be signed in to change notification settings - Fork 373
/
main.tf
41 lines (35 loc) · 1.25 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
resource "aws_sns_topic" "ecs_events" {
name = "ecs_events_${var.environment}_${var.cluster}"
}
data "aws_caller_identity" "current" {}
data "aws_region" "current" {
current = true
}
data "template_file" "ecs_task_stopped" {
template = <<EOF
{
"source": ["aws.ecs"],
"detail-type": ["ECS Task State Change"],
"detail": {
"clusterArn": ["arn:aws:ecs:$${aws_region}:$${account_id}:cluster/$${cluster}"],
"lastStatus": ["STOPPED"],
"stoppedReason": ["Essential container in task exited"]
}
}
EOF
vars {
account_id = data.aws_caller_identity.current.account_id
cluster = var.cluster
aws_region = data.aws_region.current.name
}
}
resource "aws_cloudwatch_event_rule" "ecs_task_stopped" {
name = "${var.environment}_${var.cluster}_task_stopped"
description = "${var.environment}_${var.cluster} Essential container in task exited"
event_pattern = data.template_file.ecs_task_stopped.rendered
}
resource "aws_cloudwatch_event_target" "event_fired" {
rule = aws_cloudwatch_event_rule.ecs_task_stopped.name
arn = aws_sns_topic.ecs_events.arn
input = "{ \"message\": \"Essential container in task exited\", \"account_id\": \"${data.aws_caller_identity.current.account_id}\", \"cluster\": \"${var.cluster}\"}"
}