Created
December 23, 2021 15:53
-
-
Save danmaas/74be2d6270deeeb653ba5978919512e4 to your computer and use it in GitHub Desktop.
How to send an SNS notification for ECS Fargate container failures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CloudWatch Event Rule to catch non-normal container exit codes | |
resource "aws_cloudwatch_event_rule" "fargate_container_error" { | |
name = "${var.sitename}-fargate-container-error" | |
description = "Container stopped with a non-zero exit code" | |
event_pattern = <<PATTERN | |
{ | |
"source": [ | |
"aws.ecs" | |
], | |
"detail-type": [ | |
"ECS Task State Change" | |
], | |
"detail": { | |
"lastStatus": [ | |
"STOPPED" | |
], | |
"stoppedReason": [ | |
"Essential container in task exited" | |
], | |
"containers": { | |
"exitCode": [ | |
{ "anything-but": 0 } | |
] | |
} | |
} | |
} | |
PATTERN | |
tags = { | |
Terraform = "true" | |
} | |
} | |
# Report non-normal container exits to the SNS alert topic | |
# note: that topic must have a policy that allows SNS:Publish from Service events.amazonaws.com | |
resource "aws_cloudwatch_event_target" "fargate_container_error_to_sns" { | |
rule = aws_cloudwatch_event_rule.fargate_container_error.name | |
target_id = "${var.sitename}-fargate-container-error-to-sns" | |
arn = var.tech_alerts_sns_topic_arn | |
input_transformer { | |
input_paths = { | |
"containerName"="$.detail.containers[0].name", | |
"exitCode"="$.detail.containers[0].exitCode" | |
} | |
input_template = "\"ECS Container error: <containerName> exited with code <exitCode>. See the CloudWatch log group for this task for details.\"" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment