Menu
AWS Batch
User Guide

Tutorial: Sending Amazon Simple Notification Service Alerts for Failed Job Events

In this tutorial, you configure a CloudWatch Events event rule that only captures job events where the job has moved to a FAILED status. At the end of this tutorial, you can submit a job to this job queue to test that you have configured your Amazon SNS alerts correctly.

Prerequisites

This tutorial assumes that you have a working compute environment and job queue that are ready to accept jobs. If you do not have a running compute environment and job queue to capture events from, follow the steps in Getting Started with AWS Batch to create one.

Step 1: Create and Subscribe to an Amazon SNS Topic

For this tutorial, you configure an Amazon SNS topic to serve as an event target for your new event rule.

To create an Amazon SNS topic

  1. Open the Amazon SNS console at https://console.aws.amazon.com/sns/v2/home.

  2. Choose Topics, Create new topic.

  3. For Topic name, enter JobFailedAlert and choose Create topic.

  4. Select the topic that you just created. On the Topic details: JobFailedAlert screen, choose Create subscription.

  5. For Protocol, choose Email. For Endpoint, enter an email address to which you currently have access and choose Create subscription.

  6. Check your email account, and wait to receive a subscription confirmation email message. When you receive it, choose Confirm subscription.

Step 2: Register Event Rule

Next, register an event rule that captures only job-failed events.

To create an event rule

  1. Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/.

  2. In the navigation pane, choose Events, Create rule.

  3. Choose Show advanced options, edit.

  4. For Build a pattern that selects events for processing by your targets, replace the existing text with the following text:

    { "detail-type": [ "Batch Job State Change" ], "source": [ "aws.batch" ], "detail": { "status": [ "FAILED" ] } }

    This code defines a CloudWatch Events rule that matches any event where the job status is FAILED. For more information about event patterns, see Events and Event Patterns in the Amazon CloudWatch User Guide.

  5. For Targets, choose Add target. For Target type, choose SNS topic, JobFailedAlert.

  6. Choose Configure details.

  7. For Rule definition, type a name and description for your rule and then choose Create rule.

Step 3: Test Your Rule

To test your rule, submit a job that exits shortly after it starts with a non-zero exit code. If your event rule is configured correctly, you receive an email message within a few minutes with the event text.

To test a rule

  1. Open the AWS Batch console at https://console.aws.amazon.com/batch/.

  2. Submit a new AWS Batch job. For more information, see Submitting a Job. For the job's command, substitute this command to exit the container with an exit code of 1.

    /bin/sh, -c, 'exit 1'
  3. Check your email to confirm that you have received an email alert for the failed job notification.