AWS: Get email when EC2 instance state changes

To setup email notification when your EC2 instances change state, you should configure the following:

  • Amazon SNS topic – where you define recepients
  • Create an EventBridge event – where you define rule on instances that will trigger the notification

1. Create an Amazon Simple Notification Service (Amazon SNS) topic:

Go to Amazon SNS -> Create topic ->

Screen 1

-> Click Create topic

Go to Subscriptions -> Create subscription

Screen 2

A subscription confirmation email is sent to the address you have entered. Choose Confirm subscription in the email (Screen 3):

Screen 3

2. Create an EventBridge event

Open the EventBridge console -> Create rule

Screen 4

 To monitor all instances, in the Event pattern enter the following (Screen 4)

{
"source": ["aws.ec2"],
"detail-type": ["EC2 Instance State-change Notification"]
}

If you are planing to monitor particular instances then you need to indicate their instance IDs using the following way:

{
"source": ["aws.ec2"],
"detail-type": ["EC2 Instance State-change Notification"],
"detail": {
"instance-id": ["i-0897b9d936bda3bda", "i-0c76e28b0ec1a099c"]
}
}

Please note, this pattern can also be generated by choosing “Pre-defined pattern by service” radio button and choosing appropriate services. But for simplicity the above pattern can be entered.

In the Select targets section, choose previously created SNS topic.


Screen 5

In the Input transformer (Screen 5) paste the following strings accordingly:

Input Path

{"instance-id":"$.detail.instance-id", "state":"$.detail.state", "time":"$.time", "region":"$.region", "account":"$.account"}

Input Template

"At <time>, the status of your EC2 instance <instance-id> on account <account> in the AWS Region <region> has changed to <state>."

Click Create.

3. Test the configuration

Start/stop target instanes and check your email: