How can I get customized email notifications when my EC2 instance changes states?

3 minute read

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

Short description

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 EventBridge using the EC2 Instance State-change Notification event type.


Create an SNS topic

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

2.    Select Create topic.

3.    For Type, choose Standard.

4.    For Name, enter a name for your topic.

5.    For Display name, enter a display name for your topic.

6.    Select Create topic.

7.    On the Subscriptions tab, choose Create subscription.

8.    For Protocol, choose Email.

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

10.  Select Create subscription.

A subscription confirmation email is sent to the address that you entered. Choose Confirm subscription in the email. Note the SNS topic that you created. You use this topic when creating the EventBridge rule.

Create an EventBridge event

1.    Open the EventBridge console.

2.    Select Create rule from the homepage. Or, choose Rules under Events in the sidebar, and then select Create rule.

3.    Enter a Name for your rule. You can optionally enter a Description.

4.    Keep the default Event bus and Rule type settings, and then select Next.

5.    In Event pattern, keep the Event source as AWS services. For the AWS service, choose EC2.

6.    For Event type, choose EC2 Instance State-change Notification.

7.    Keep Any state and Any instance as the default settings, and then select Next.

8.    For Select a target, choose SNS topic.

9.    For Topic, choose the topic name that you created earlier, and then select Next.

10.  Expand the Additional settings section. For Configure target input, choose Input transformer.

11.  Select Configure input transformer, and then enter the following text:

        For Input path, enter the following:

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

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

12.  Select Next.

13.  Leave the optional Tags empty, and select Next. Then, select Create rule.

        Note: The rule that you created applies to a single AWS Region.

You can test the rule by starting or stopping an instance. This rule generates an email notification every time an instance changes to any state, including stopped.

AWS OFFICIALUpdated 2 years ago

How to add instance name in the inputh path and template so that we need not expose the Instance ID in the email notification.

replied 6 months ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
replied 6 months ago