Email alerts when a docker container stopped in AWS ECS CLUSTER

Do you have docker containers running in an AWS ECS cluster and worried about how to get notified when any container in production got killed by any reason, Then you are on right place.

Just think, Your production container is down and you never know about it immediately, Because you are playing pubg in your mobile 😉

Haha its a serious problem right, “ OFCOURSE DUDE“


What if I had a alert mechanism to my AWS Infrastructure which could mail me whenever my containers got stopped , like “Hey Bala your service ABC container got stopped due to this reason “. Then it would be great to know about the situation and act on it accordingly.

How To achieve this??????

AWS provides you Cloudwatch Rules to monitor your ECS task state change in an ECS cluster.

To create a rule that triggers on an event:

  1. Open the CloudWatch console at
  2. In the navigation pane, choose Events, Create rule.
  3. Choose event pattern
  4. choose Service name as Elastic Container Service (ECS)
  5. Event type as State change.
  6. Choose Any cluster option to monitor all your ECS clusters or choose Specific cluster option to monitor your specific ECS cluster.

It will look like below image.


This will track the change in state of your docker containers .

It will have 3 states:


By default all three states will be tracked by cloudwatch rules, but if you wish to track only STOPPED state and get notified, then you can add the below code to Event Pattern Preview section. (You can actually add multiple states or another state other than STOPPED aswell)



“source”: [



“detail-type”: [

“ECS Task State Change”


“detail”: {

“clusterArn”: [

“arn:aws:ecs:<add your region here> :<account-id>:cluster/<cluster-name>”


“lastStatus”: [





Then on the right side ,  there will be Targets section, choose target as SNS topic(if you dont have SNS topic, create an SNS topic and subscribe your email address to that SNS topic and confirm the subscription) .

Here comes the interesting part, now if you choose matched event under configure input section(Which is default option) , you will receive a long json object which I feel will be difficult to read in a mail about all the details together. So to avoid this we can customise the email which we get.

Choose Input Transformer :

In the first text area, add the following lines:


In the second text area, add the following lines:

“The container <group> in production got <lastStatus> due to <stoppedReason> , Kindly check “

It will look like the below image :



Give an appropriate name and description to your Cloudwatch rule. and click on that tick mark to enable the rule.

You can actually enable and disable this rule using that tick mark. (like during code deployments you can disable and enable when deployment is done.)


Whenever any task(container) in ECS cluster gets killed:

$ will track the name of the container that is stopped.

$.detail.lastStatus will track that it is stopped.

$.detail.stoppedReason will track the reason why the container is stopped.

and will send an email like

The container service:web-container in production got STOPPED due to OutOfMemoryError, Kindly check

Thats it 🙂

Thanks for your Time.

One Reply to “Email alerts when a docker container stopped in AWS ECS CLUSTER”

Leave a Reply