Trigger Email Notification On EC2 Instance State Changes

Trigger Email Notification On EC2 Instance State Changes

Table of Contents

  • Prerequisites
  • Create an SNS topic
  • Create a CloudWatch event rule
  • Testing rule

Prerequisites

An AWS Account An IAM User with:

  • AWS Management Console access
  • The IAM permissions required to perform IAM, EC2, and CloudWatch activities.
  • IAM policy creation and AWS Application Programming Interface (API) permissions are outside this article’s scope. Always adhere to the principle of least privilege when authorizing accounts to perform actions. Administrative access to an EC2 Instance.

Create an SNS topic

  1. Open the Amazon SNS console, and then choose Topics from the navigation pane. image.png

  2. Choose Create topic. image.png

  3. For Name, enter a name for your topic[EC2-State-Change-Notify]. image.png

  4. For Display name, enter a display name for your topic and choose create topic image.png

  5. After topic creation click on the Subscriptions tab, choose Create subscription. image.png

  6. For Protocol, choose Email -> For Endpoint, enter the email address where you want to receive the notifications -> Choose Create subscription. image.png image.png

  7. A subscription confirmation email is sent to the address you entered. Choose Confirm subscription in the email. image.png

  8. When you click on confirm you will get below message which confirms your subscription image.png

  9. Now go back to Topics->EC2-State-Change-Notify and you can see its status has changed from pending to confirmed. image.png

Note the SNS topic you created. You use this topic when creating the CloudWatch event rule.

Create a CloudWatch event rule

  1. Go to services → CloudWatch → Rules → click on Create rule.

image.png image.png

  1. For Event Source, choose Event Pattern -> For Service Name, choose EC2 -> For Event Type, choose EC2 Instance State-change Notification -> Choose Any state -> Choose Any instance. image.png

  2. Choose Add target -> For Targets, choose SNS topic -> For Topic, choose the topic name "EC2-State-Change-Notify" that you created earlier image.png image.png

  3. For Configure input, choose Input Transformer -> For Input Path, enter the following: {"instance-id":"$.detail.instance-id", "state":"$.detail.state", "time":"$.time", "region":"$.region", "account":"$.account"}image.png

  4. For Input Template, enter the following: "At , the status of your EC2 instance on account in the AWS Region has changed to ." Note: The Input Template also allows custom inputs. image.png

  5. Choose Configure details -> For Name, enter a rule name "EC2-State-Change-Notify" -> For Description, enter a rule description -> Choose Create rule. image.png

Lets test our implementation by changing the state of our running instance.

  • Let goto EC2 console and check the current status of our instance. Below you can see our instance is in running state image.png

  • We will change our instance state from running to stopped by clicking on Instance State -> Stop image.png

  • Ec2 instance is stopped image.png

  • As soon as the EC2 was stopped our cloudwatch event rule got triggered and we received email notification of this state change as you can see below. image.png

Conclusion

We have seen how to trigger email notification while EC2 instances state changes using AWS cloud native services without any third party integrations and user friendly interface. This kind of monitoring can be done in environment where EC2 autoscaling policy is not enabled as part of their implementation.

Stay tuned, for my next blogs..

So, did you find my content helpful? If you did or like my other content, feel free to buy me a coffee. Thanks.

Did you find this article valuable?

Support Dheeraj Choudhary by becoming a sponsor. Any amount is appreciated!