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

Last updated: 2021-03-08

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

Resolution

Create an SNS topic

  1. Open the Amazon SNS console, and then choose Topics from the navigation pane.
  2. Choose Create topic.
    For Name, enter a name for your topic.
    For Display name, enter a display name for your topic.
  3. Choose Create topic.
  4. On the Subscriptions tab, choose Create subscription.
    For Protocol, choose Email.
    For Endpoint, enter the email address where you want to receive the notifications.
  5. Choose Create subscription.
  6. A subscription confirmation email is sent to the address you entered. Choose Confirm subscription in the email.
    Note the SNS topic you created. You use this topic when creating the CloudWatch event rule.

Create a CloudWatch event

  1. Open the CloudWatch console, and then choose Events from the navigation pane.
  2. Choose Get started.
    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.
  3. Choose Add target.
    For Targets, choose SNS topic.
    For Topic, choose the topic name that you created earlier.
    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"}
    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.
  4. Choose Configure details.
    For Name, enter a rule name.
    For Description, enter a rule description.
  5. Choose Create rule.

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.


Did this article help?


Do you need billing or technical support?