如何实现AWS账户内EC2实例创建/终止时的邮件通知?
Got it, let's walk through exactly how to set up email notifications whenever an EC2 instance is created or terminated in your AWS account. It's a straightforward workflow using EventBridge (formerly CloudWatch Events) and SNS—here's the step-by-step breakdown that'll get you up and running in minutes:
First, we need a central topic where EventBridge will send state-change events, and which will forward those events to your email.
- Open the AWS Console, navigate to the SNS (Simple Notification Service) dashboard.
- From the left sidebar, select Topics, then click Create topic.
- Choose the Standard topic type (it's perfect for this use case—simpler than FIFO and supports email subscriptions).
- Give your topic a descriptive name like
EC2-Instance-State-Alerts, and add an optional description (e.g., "Triggers emails when EC2 instances start or stop"). - Click Create topic, and save the topic's ARN (Amazon Resource Name) somewhere—you'll need it later.
Now we'll link your email address to the SNS topic so it receives notifications:
- On your newly created topic's detail page, switch to the Subscriptions tab and click Create subscription.
- For Protocol, select Email, then enter your full email address in the Endpoint field.
- Click Create subscription.
- Check your email inbox immediately—AWS will send a confirmation link. Click that link to verify your subscription; you won't get any notifications until you do this.
This is the "brain" of the setup—it watches for EC2 instance state changes and sends those events to your SNS topic.
- Open the AWS Console, navigate to EventBridge.
- From the left sidebar, select Rules, then click Create rule.
- Name your rule something like
EC2-Create-Terminate-Notifications, add an optional description, then click Next. - Under Define pattern, select Event pattern, then click Custom pattern (JSON).
- Paste this JSON pattern—it will only trigger when instances switch to
running(created/started) orterminatedstates:
{ "source": ["aws.ec2"], "detail-type": ["EC2 Instance State-change Notification"], "detail": { "state": ["running", "terminated"] } }
- Click Next, then under Select targets, choose SNS topic from the target dropdown.
- Select the SNS topic you created earlier from the Topic menu.
- For Configure input, leave it set to Matched event—this ensures your email includes full details like instance ID, region, and timestamp.
- Click Next, skip adding tags (unless you need them for organization), then click Create rule.
To make sure everything works as expected:
- Head to the EC2 Console, launch a small test instance (use a
t2.microif you're on the free tier). - Once the instance reaches the
runningstate, check your email—you should get a notification with all the instance details. - Terminate the test instance, and you'll get another notification when it moves to the
terminatedstate.
- Permissions: Don't worry about manual IAM setup—when you create the EventBridge rule via the console, AWS automatically creates a role with permission to send events to your SNS topic.
- Multi-Region Support: EventBridge rules are region-specific. If you run EC2 instances in multiple regions, you'll need to repeat this setup in each region (or use EventBridge Global Endpoints for cross-region monitoring, but that's overkill for most use cases).
- Filtering Specific Instances: If you only want alerts for instances with certain tags (e.g.,
Environment:Production), you can add a tag filter to the EventBridge pattern. For example, add this inside thedetailobject:"tags": { "Environment": ["Production"] }
内容的提问来源于stack exchange,提问作者LogicIO

