Sending Amazon Simple Notification Service Alerts for Failed Job Events

In this tutorial, we will configure a Cloudwatch event rule that only captures job events when the job has moved to a FAILED status.

Prerequisites:

  1. You have working Lambda function
  2. Optional: You have Cloudwatch rules to launch those lambda function.

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

Create Amazon SNC Topic:

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

Choose TopicsCreate new topic.

For Topic name, enter JobFailedAlert and choose Create topic.

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

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

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

Go back to the SNS console and refresh your browser. You should be able to see the subscription ID with your E-mail address as an endpoint.

Create Alarm and Send Notification ( Option 1 )

This will create Alarm, in case the error triggers. That means, if any instances fail to start, they will send the error code other than “0”. When this condition is matched, Cloudwatch will launch this alarm and change the state to “ALARM”. This will eventually call the Simple Notification Service (SNS) and E-mail will be sent to all the valid subscribers within that SNC topic.

  1. Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/.
  2. In the navigation pane, choose AlarmsCreate Alarm.
  3. Choose Lambda --> By Function Nameselect the FunctionName with “Error” as Metric Name.

For example, we will monitor the instances when they are being started. That means, we need to select the Lambda function which will start the instances. Don’t forget to select the “Error” as a metric name in your lambda function.

Click Next.

Give any Name like “Automatic Instance Start”.

The condition should look like this:

 

In the Actions panel, choose whenever this alarm to State is Alarm

And send the notification to SNS topic.

For monitoring shutting down instances, choose another Lambda function responsible for shutting down jobs.

 

Register Event Rule ( Option 2)

This rule defines a CloudWatch Events rule that matches any event where the job status is FAILED and call SNS to send E-Mails. I have not tested this section.

To create an event rule

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

In the navigation pane, choose Events PatternCreate rule.

Choose Show advanced optionsedit.

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 Targets, choose Add target. For Target type, choose SNS topicJobFailedAlert.

Choose Configure details.

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

Note: Personally I haven't checked this second method. 

 

Good Luck!

mm

Anup Chhetri

IT system administrator

You may also like...

error: Content is protected !!