Menu
Amazon EC2 Container Service
Developer Guide (API Version 2014-11-13)

Tutorial: Sending Amazon Simple Notification Service Alerts for Task Stopped Events

In this tutorial, you configure a CloudWatch Events event rule that only captures task events where the task has stopped running because one of its essential containers has terminated. The event sends only task events with a specific stoppedReason property to the designated Amazon SNS topic.

Step 1: Set Up a Test Cluster

If you do not have a running cluster to capture events from, follow the steps in Getting Started with Amazon ECS to create one. At the end of this tutorial, you run a task on this cluster to test that you have configured your Amazon SNS topic and CloudWatch Events event rule correctly.

Step 2: 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 a 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. On the Create new topic window, for Topic name, enter TaskStoppedAlert and choose Create topic.

  4. On the Topics window, select the topic that you just created. On the Topic details: TaskStoppedAlert screen, choose Create subscription.

  5. On the Create Subscription window, 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 3: Register Event Rule

Next, you register an event rule that captures only task-stopped events for tasks with stopped containers.

To create an event rule

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

  2. On 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:

    Copy
    { "source": [ "aws.ecs" ], "detail-type": [ "ECS Task State Change" ], "detail": { "lastStatus": [ "STOPPED" ], "stoppedReason" : [ "Essential container in task exited" ] } }

    This code defines a CloudWatch Events event rule that matches any event where the lastStatus and stoppedReason fields match the indicated values. 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, and then choose TaskStoppedAlert.

  6. Choose Configure details.

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

Step 4: Test Your Rule

To test your rule, you attempt to run a task that exits shortly after it starts. 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 Amazon ECS console at https://console.aws.amazon.com/ecs/.

  2. Choose Task Definitions, Create new Task Definition.

  3. For Task Definition Name, type WordPressFailure and choose Add Container.

  4. For Container name, type Wordpress, for Image, type wordpress, and for Maximum memory (MB), type 128.

  5. Choose Add, Create.

  6. On the Task Definition screen, choose Actions, Run Task.

  7. For Cluster, choose default and then Run Task.

  8. On the Tasks tab for your cluster, periodically choose the refresh icon until you no longer see your task running. For Desired task status, choose Stopped to verify that your task has stopped.

  9. Check your email to confirm that you have received an email alert for the stopped notification.