How to customize email notifications when an EC2 instance changes states?

We want to receive email notifications when an Amazon Elastic Compute Cloud (Amazon EC2) instance changes states. How can we do this?

Overview:

Below is the overview of steps that we are going to perform in order to achieve the desired output.

To receive email notifications when your EC2 instance changes states:

  1. Create an Amazon Simple Notification Service (Amazon SNS) topic. The SNS topic sends messages to subscribing endpoints or clients.
  2. Create an Amazon CloudWatch event using the EC2 Instance State-change Notification event type.

Creating an SNS topic:

  1. Open the Amazon SNS Console,

and then select Topics from the navigation pane.

  1. Click on Create topic.

For Type, Select the Standard.
For Name, enter a name for your topic.
For Display name, enter a display name for your topic.

  1. On the Subscriptions tab, click Create subscription.

You select the ARN of the topic that you have just created in Type ARN

            For Protocol, Select Email.

For Endpoint, enter the email address where you want to receive the notifications.

Click on the Create subscription tab and it is created.

  1. A subscription confirmation email is sent to the address you entered. 

Click on the confirm link to confirm the subscription.

Below is the subscription confirmation page.

Creating a CloudWatch event:

  1. Open the CloudWatch Console, and then go to Events > Rules > Create rule
  1. Click on Create Rule.
    For Event Source, Click on Event Pattern.
    For Service Name, Select EC2.
    For Event Type, Select  EC2 Instance State-change Notification.
    Tick on  Any state.
    Tick on Any instance.
  1. In the Targets section.
    For Targets, select SNS topic.
    For Topic, select the topic name that you created earlier.


For Configure input, select Input Transformer.
For Input Path, enter the following:
{“instance-id”:”$.detail.instance-id”, “state”:”$.detail.state”, “time”:”$.time”, “region”:”$.region”, “account”:”$.account”}
For Input Template, enter the following:
“At <time>, the status of your EC2 instance <instance-id> on account <account> in the AWS Region <region> has changed to <state>.”


Note: The Input Template also allows custom inputs.

  1. Click Configure details.
    For Name, enter a rule name.
    For Description, enter a rule description.
  1. Then click on Create rule to complete rule creation.

We can verify by changing the states of the instance in the EC2 console. Open the EC2 Console

Select any instance and change the instance state manually. In this case we are Stopping an Instance.

When the state of the instance is changed into “Stopping” from “running”. An Email alert is sent to our email address given in the SNS topic.

And When the instance state changes to “Stopped” from “Stopping”. 

Then again we get an Email alert confirming the same. 

Siam Tunglut

Cloud Engineer. Software engineer turned bureaucrat turned Cloud Engineer.

Scroll to top