• Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
test
Code4Developers

Code4Developers

Code4Developers
  • Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
  • AWS

Email alerts when a docker container stopped in AWS ECS CLUSTER

  • Balakrishna Cherukuri
  • July 24, 2020
  • 3 minute read
AWS
Total
0
Shares
0
0
0

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“

AWS

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 https://console.aws.amazon.com/cloudwatch/.
  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.

AWS

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

It will have 3 states:

  • RUNNING
  • PENDING
  • STOPPED

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”: [

“aws.ecs”

],

“detail-type”: [

“ECS Task State Change”

],

“detail”: {

“clusterArn”: [

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

],

“lastStatus”: [

“STOPPED”

]

}

}

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:

{“stoppedReason”:”$.detail.stoppedReason”,”lastStatus”:”$.detail.lastStatus”,”group”:”$.detail.group”}

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 :

AWS

NEXT ON STEP 2 PAGE:

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.)

HURRAY!!!!! THATS ALL YOU NEED TO DO.

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

$.detail.group 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.

Balakrishna Cherukuri
Balakrishna Cherukuri

Working as a DevOps Engineer with 2+ years of experience. Certified from AWS with AWS CERTIFIED SOLUTIONS ARCHITECT- ASSOCIATE certificate.

Views: 19,157

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on X (Opens in new window) X
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to email a link to a friend (Opens in new window) Email
  • Click to print (Opens in new window) Print

Like this:

Like Loading...

Related Posts

Total
0
Shares
Share 0
Tweet 0
Pin it 0
1 comment
  1. Anonymous says:
    October 12, 2020 at 4:12 pm

    Can you explain with environment variables

    Reply

Leave a ReplyCancel reply

Subscribe to Website via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Posts
  • Getting Started with AWS Step Functions: Orchestration Made Simple

    Getting Started with AWS Step Functions: Orchestration Made Simple

    2 months ago
  • React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    2 years ago
  • Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    2 years ago
  • Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    2 years ago
  • Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    2 years ago
  • Angular Custom Elements: Creating Reusable Components with Angular

    Angular Custom Elements: Creating Reusable Components with Angular

    3 years ago
  • Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    5 years ago
  • How to Build an Inclusive Esports Community

    How to Build an Inclusive Esports Community

    5 years ago
  • Best Digital Icebreakers

    Best Digital Icebreakers

    5 years ago
  • New Learning Models for Fall 2020

    New Learning Models for Fall 2020

    5 years ago
Subscribe to Website via Email

Enter your email address to subscribe to this website and receive notifications of new posts by email.

Featured Posts
  • javascript 1
    Spread syntax (three dots) in JavaScript
    • March 21, 2018
  • Angular 2
    Angular 6 CRUD – Part 1: Project Setup, Routing, Service
    • May 9, 2018
  • javascript 3
    Local Storage and Session Storage
    • May 22, 2017
  • Angular 4
    Angular 4 Project Structure
    • June 18, 2017
  • AWS 5
    Email alerts when a docker container stopped in AWS ECS CLUSTER
    • July 24, 2020
Code4Developers
Learning is never ending process

Input your search keywords and press Enter.

 

Loading Comments...
 

    %d