Skip to content

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

4 minute read
2

I want to receive email notifications when my Amazon Elastic Compute Cloud (Amazon EC2) instance changes states.

Short description

To receive email notifications when your EC2 instance changes states, create an Amazon Simple Notification Service (Amazon SNS) topic to send messages to subscribed endpoints or clients. Then, use the EC2 Instance State-change Notification event type to create an Amazon EventBridge rule.

Note: You're charged separately for Amazon SNS and for EventBridge.

Resolution

Create a customize email notification

Complete the following steps:

  1. Create an Amazon SNS topic.
    Note: For Type, choose Standard.

  2. Create a subscription to the SNS topic.
    Note: For Protocol, choose Email. You receive a subscription confirmation email at the email address that you entered. Choose Confirm subscription in the email.

  3. Open the EventBridge console.

  4. Choose Create rule. Or, in the Events navigation pane, choose Rules, and then choose Create rule.

  5. Enter a Name and Description for your rule.

  6. Keep the default Event bus and Rule type settings, and then choose Next.

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

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

  9. Keep the default settings for Any state and Any instance, and then choose Next.

  10. For Select a target, select SNS topic.

  11. For Topic, select the topic that you created, and then choose Next.

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

  13. Select Configure input transformer, and then enter the following text for Input path and Template.
    Input path:

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

    Template:

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

    Note: For more information about input transformation fields, see Amazon EventBridge input transformation. The input transformer can only use fields that are present in the data event. For example, the following event pattern for an event that the preceding configuration captures shows an instance that entered the Pending state:

    {
        "id": "7bf73129-1428-4cd3-a780-95db273d1602",
        "detail-type": "EC2 Instance State-change Notification",
        "source": "aws.ec2",
        "account": "123456789012",
        "time": "2021-11-11T21:29:54Z",
        "region": "us-east-1",
        "resources": ["arn:aws:ec2:us-east-1:123456789012:instance/i-1234567890abcdef0"],
        "detail": {
            "instance-id": "i-1234567890abcdef0",
            "state": "pending"
        }
    }
  14. Choose Next.

  15. Leave the optional Tags empty, and then choose Next.

  16. Choose Create rule.
    Note: The rule that you created applies only to the AWS Region where you created it. The rule generates an email notification each time an instance changes state.

Test your custom email notification

To test the rule, start and stop an instance.

Note: When you stop and start an instance, the instance's public IP address changes. It's a best practice to use an Elastic IP address to route external traffic to your instance instead of a public IP address. If you use Amazon Route 53, then you might need to update the Route 53 DNS records when the public IP address changes.

Before you stop and start your instance, take the following actions:

AWS OFFICIALUpdated a month ago
4 Comments

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 2 years ago

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

AWS
EXPERT
replied 2 years ago

Hi AWS TEAM , any update on how to add instance name in the input path and template

replied a year ago

Will I be charged for using the event bus in addition to SNS?

replied a year ago