Sending Amazon Simple Notification Service alerts for Amazon ECS task stopped events - Amazon Elastic Container Service
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Sending Amazon Simple Notification Service alerts for Amazon ECS task stopped events

Configure an Amazon EventBridge 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.

Prerequisite: Set up a test cluster

If you do not have a running cluster to capture events from, follow the steps in Getting started with the console using Linux containers on Amazon Fargate 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 EventBridge rule correctly.

Prerequisite: Configure permissions for Amazon SNS

To allow EventBridge to publish to an Amazon SNS topic, use the aws sns get-topic-attributes and the aws sns set-topic-attributes commands.

For information about how to add the permission, see Amazon SNS permissions in the Amazon Simple Notification Service Developer Guide

Add the following permissions:

{ "Sid": "PublishEventsToMyTopic", "Effect": "Allow", "Principal": { "Service": "events.amazonaws.com" }, "Action": "sns: Publish", "Resource": "arn:aws:sns:region:account-id:TaskStoppedAlert", }

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.

For information about how to create and subscribe to an Amazon SNS topic , see Getting started with Amazon SNS in the Amazon Simple Notification Service Developer Guide and use the following table to determine what options to select.

Option Value

Type

Standard
Name

TaskStoppedAlert

Protocol Email
Endpoint

An email address to which you currently have access

Step 2: Register an event rule

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

For information about how to create and subscribe to an Amazon SNS topic , see Create a rule in Amazon EventBridge in the Amazon EventBridge User Guide and use the following table to determine what options to select.

Option Value

Rule type

Rule with an event pattern

Event source Amazon events or EventBridge partner events
Event pattern

Custom pattern (JSON editor)

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

Amazon service

Target SNS topic
Topic

TaskStoppedAlert (The topic you created in Step 1)

Step 3: Test your rule

Verify that the rule is working by running 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. If you have an existing task definition that can satisfy the rule requirements, run a task using it. If you do not, the following steps will walk you through registering a Fargate task definition and running it that will.

  1. Open the console at https://console.amazonaws.cn/ecs/v2.

  2. In the navigation pane, choose Task definitions.

  3. Choose Create new task definition, Create new task definition with JSON.

  4. In the JSON editor box, edit your JSON file, copy the following into the editor.

    { "containerDefinitions":[ { "command":[ "sh", "-c", "sleep 5" ], "essential":true, "image":"amazonlinux:2", "name":"test-sleep" } ], "cpu":"256", "executionRoleArn":"arn:aws:iam::012345678910:role/ecsTaskExecutionRole", "family":"fargate-task-definition", "memory":"512", "networkMode":"awsvpc", "requiresCompatibilities":[ "FARGATE" ] }
  5. Choose Create.

To run a task from the console
  1. Open the console at https://console.amazonaws.cn/ecs/v2.

  2. On the Clusters page, choose the cluster you created in the prerequisites.

  3. From the Tasks tab, choose Run new task.

  4. For Application type, choose Task.

  5. For Task definition, choose fargate-task-definition.

  6. For Desired tasks, enter the number of tasks to launch.

  7. Choose Create.