This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Report batch job failures to Rollbar via a Lambda function
We want to be alerted when there is a job failure. To accomplish this we set up a CloudWatch event to detect failures and connect that event to a Lambda function that posts the details of the failure to Rollbar. This strategy has been used successfully on another project.
- Loading branch information
Showing
8 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
11 changes: 11 additions & 0 deletions
11
deployment/terraform/lambda-functions/alert_batch_failures/Makefile
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
all: requirements | ||
rm -rf alert_batch_failures.zip | ||
(cd src && zip -qXr ../alert_batch_failures.zip .) | ||
|
||
requirements: requirements.txt.out | ||
|
||
requirements.txt.out: requirements.txt | ||
git clean -qfdx src | ||
pip install -q -t src -r requirements.txt | tee requirements.txt.out | ||
|
||
.PHONY: all requirements |
1 change: 1 addition & 0 deletions
1
deployment/terraform/lambda-functions/alert_batch_failures/requirements.txt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rollbar==0.14.6 |
37 changes: 37 additions & 0 deletions
37
deployment/terraform/lambda-functions/alert_batch_failures/src/alert_batch_failures.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import os | ||
import rollbar | ||
|
||
environment = os.getenv('ENVIRONMENT', 'Staging') | ||
rollbar_token = os.getenv('ROLLBAR_SERVER_SIDE_ACCESS_TOKEN') | ||
rollbar.init(rollbar_token, environment) | ||
|
||
CLOUDWATCH_LOGS_URL = "https://console.aws.amazon.com/cloudwatch/home?region=eu-west-1#logEventViewer:group=/aws/batch/job;stream={logstream}" # NOQA | ||
|
||
|
||
@rollbar.lambda_function | ||
def handler(event, context): | ||
cloudwatch_urls = \ | ||
list(map(lambda attempt: | ||
CLOUDWATCH_LOGS_URL.format( | ||
logstream=attempt["container"]["logStreamName"]), | ||
event["detail"]["attempts"])) | ||
|
||
msg = """ | ||
{jobname} (JobID {jid}) entered state {state}. Reason: {reason}. | ||
CloudWatch URLs: | ||
- {cloudwatchurls} | ||
""".format(jobname=event["detail"]["jobName"].split("-")[0], | ||
jid=event["detail"]["jobId"], | ||
state=event["detail"]["status"], | ||
reason=event["detail"]["statusReason"], | ||
cloudwatchurls="\n\n-".join(cloudwatch_urls)) | ||
|
||
rollbar.report_message( | ||
msg, | ||
level='error', | ||
# Give each CloudWatch URL its own column in the occurrences tab | ||
extra_data=dict(map(lambda x: ("cloudwatch_url_{}".format(x[0]), x[1]), | ||
enumerate(cloudwatch_urls)))) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
resource "aws_lambda_function" "alert_batch_failures" { | ||
filename = "${path.module}/lambda-functions/alert_batch_failures/alert_batch_failures.zip" | ||
source_code_hash = "${base64sha256(file("${path.module}/lambda-functions/alert_batch_failures/alert_batch_failures.zip"))}" | ||
function_name = "func${var.environment}AlertBatchFailures" | ||
description = "Function to alert on AWS Batch Job Failures." | ||
role = "${aws_iam_role.alert_batch_failures.arn}" | ||
handler = "alert_batch_failures.handler" | ||
runtime = "python3.6" | ||
timeout = 10 | ||
memory_size = 128 | ||
|
||
environment { | ||
variables = { | ||
ENVIRONMENT = "${var.environment}" | ||
ROLLBAR_SERVER_SIDE_ACCESS_TOKEN = "${var.rollbar_server_side_access_token}" | ||
} | ||
} | ||
|
||
tags { | ||
Project = "${var.project}" | ||
Environment = "${var.environment}" | ||
} | ||
} | ||
|
||
resource "aws_cloudwatch_event_rule" "alert_batch_failures" { | ||
name = "rule${var.environment}AlertBatchFailures" | ||
description = "Rule to send alerts when batch jobs fail." | ||
|
||
event_pattern = <<PATTERN | ||
{ | ||
"source": ["aws.batch"], | ||
"detail-type": ["Batch Job State Change"], | ||
"detail": { | ||
"status": ["FAILED"], | ||
"jobQueue": [ | ||
"${aws_batch_job_queue.default.arn}" | ||
] | ||
} | ||
} | ||
PATTERN | ||
} | ||
|
||
resource "aws_cloudwatch_event_target" "alert_batch_failures" { | ||
rule = "${aws_cloudwatch_event_rule.alert_batch_failures.name}" | ||
target_id = "target${var.environment}AlertBatchFailures" | ||
arn = "${aws_lambda_function.alert_batch_failures.arn}" | ||
} | ||
|
||
resource "aws_lambda_permission" "alert_batch_failures" { | ||
statement_id = "perm${var.environment}AlertBatchFailures" | ||
action = "lambda:InvokeFunction" | ||
function_name = "${aws_lambda_function.alert_batch_failures.function_name}" | ||
principal = "events.amazonaws.com" | ||
source_arn = "${aws_cloudwatch_event_rule.alert_batch_failures.arn}" | ||
} |
This file contains 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
This file contains 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