You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现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:

Step 1: Create an SNS Topic for Notifications

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.
Step 2: Subscribe Your Email to the SNS Topic

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.
Step 3: Create an EventBridge Rule to Trigger on EC2 State Changes

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) or terminated states:
{
  "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.micro if you're on the free tier).
  • Once the instance reaches the running state, 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 terminated state.
Quick Notes to Keep in Mind
  • 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 the detail object:
    "tags": {
      "Environment": ["Production"]
    }
    

内容的提问来源于stack exchange,提问作者LogicIO

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:57:01