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

咨询Spot Instance创建与请求对应的CloudWatch EventName及邮件通知配置

关于Spot Instance通知的CloudWatch Event配置问题

1. RequestSpotInstances作为eventName是否正确?

是正确的。RequestSpotInstances对应AWS EC2的发起Spot实例请求的API操作,当用户提交Spot实例请求时,CloudTrail会记录这个事件,你的CloudWatch Event规则可以捕获到这个动作,满足“Spot Instance请求发起时收到通知”的需求。

2. 其他合适的eventName

如果需要捕获Spot实例实际创建完成的事件(即Spot请求被AWS资源池满足,实例成功启动),还需要添加RunInstances这个eventName。因为当Spot请求被调度成功后,EC2底层会调用RunInstances来创建实例,这个事件能准确反映Spot实例已经被创建出来。

如果需要覆盖Spot请求的其他关键状态,还可以考虑以下eventName:

  • CancelSpotInstanceRequests:Spot请求被取消时
  • TerminateInstances:Spot实例被终止时(若需关注终止通知)

修正后的Terraform配置

你的现有配置包含了不必要的rds.amazonaws.com和s3.amazonaws.com作为eventSource,应只保留ec2.amazonaws.com,同时补充需要的eventName:

resource "aws_sns_topic" "spot_instance_notification" {
  name = "SpotInstanceNotificationTopic"
}

resource "aws_cloudwatch_event_rule" "spot_instance_creation_rule" {
  name = "SpotInstanceCreationRule"

  event_pattern = <<EOF
  {
    "source": ["aws.ec2"],
    "detail-type": ["AWS API Call via CloudTrail"],
    "detail": {
      "eventSource": ["ec2.amazonaws.com"],
      "eventName": ["RequestSpotInstances", "RunInstances"]
    }
  }
  EOF
}

resource "aws_cloudwatch_event_target" "sns_target" {
  rule        = aws_cloudwatch_event_rule.spot_instance_creation_rule.name
  target_id   = "spot-instance-sns-target"
  arn         = aws_sns_topic.spot_instance_notification.arn
}

resource "aws_sns_topic_subscription" "email_subscription" {
  topic_arn = aws_sns_topic.spot_instance_notification.arn
  protocol  = "email"
  endpoint  = "myemail@gmail.com" 
}

额外说明

  • 确保你的AWS账户已开启CloudTrail,因为CloudWatch Event的AWS API Call via CloudTrail类型事件依赖CloudTrail的日志记录。
  • 邮件订阅需在收到确认邮件后点击链接完成验证,否则无法接收通知。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 13:52:18