配置AWS Health Dashboard邮件告警

作者:ezmsp易云服科技公有云架构师 陈博文

序言

在当今云计算时代,AWS作为全球领先的云服务提供商,为企业提供了众多强大的服务和工具。其中,AWS Health Dashboard(AWS健康仪表板)是一个重要的工具,它可以帮助我们了解AWS服务的可用性和运行状况。然而,仅仅查看健康仪表板可能无法满足我们对于实时监控和告警的需求。为了让我们能够更快地了解到潜在的问题和变更,配置Health Dashboard邮件告警显得尤为重要。

本操作说明将为您详细介绍如何配置AWS Health Dashboard邮件告警。通过配置邮件告警,您将能够实时接收到AWS服务状态变化的通知,从而更好地监控和管理您的云资源。这将有助于您在出现问题时迅速采取行动,确保业务的稳定运行。

在接下来的章节中,我们将一步步引导您完成配置过程,帮助您更好地利用AWS Health Dashboard,提高您的云服务管理效率。

一、创建lambda函数

1、起名并按照自己实际情况选择–创建

配置AWS Health Dashboard邮件告警

2、创建sns

  • 创建主题
配置AWS Health Dashboard邮件告警
  • 选择标准,输入名字
配置AWS Health Dashboard邮件告警
  • 获取arn,保存下来放到后续代码中
配置AWS Health Dashboard邮件告警

3、写入代码

import json
import boto3
from datetime import datetime
#时区转换
def timeconvert(otime):
    # 原始时间字符串
    original_time_str = otime
    # 解析原始时间字符串为datetime对象
    original_time = datetime.strptime(original_time_str, "%a, %d %b %Y %H:%M:%S %Z")
    # 转换为Unix时间戳
    timestamp = original_time.timestamp()
    # 转换为utc+8
    timestamp_shanghai = timestamp + 28800
    # 转换为datetime对象
    dt = datetime.fromtimestamp(timestamp_shanghai)
    # 格式化为指定格式的字符串
    formatted_time = dt.strftime("%a, %d %b %Y %H:%M:%S UTC+8")
    # 输出结果
    return(formatted_time)

#使用sns发送邮件
def publish_to_sns(sub, msg):
    #替换为上一步中创建的sns arn
    topic_arn = "arn:aws:sns:ap-northeast-1:$<accountid>:cbw-test-mail"
    sns = boto3.client("sns")
    try:
        response = sns.publish(
            TopicArn=topic_arn,
            Message=msg,
            Subject=sub
        )
    except smtplib.SMTPException as e:
        print("Error: 无法发送邮件: {}".format(e))
#收集告警信息
def lambda_handler(event, context):
    # TODO implement
    service = event['detail']['service']
    type = event['detail']['eventTypeCode']
    description = event['detail']['eventDescription'][0]['latestDescription']
    start_time = event['detail']['startTime']
    start_time_shanghai = timeconvert(start_time)
    influnce_resources = event['resources']
    res ='Health dashboard 告警nAWS 服务:%s n事件类型:%s n事件描述:%s n开始日期:%s n事件影响资源:%s' %(service,type,description,start_time_shanghai,influnce_resources)
    print(res)
    publish_to_sns('ec2异常告警',res)
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

        
配置AWS Health Dashboard邮件告警

二、配置Health Dashboard的eventbridge

1、点击右上角配置,跳转到创建eventbridge界面

配置AWS Health Dashboard邮件告警

2、配置eventbridge名字

配置AWS Health Dashboard邮件告警

3、构建事件–事件源选择其它–示例选择aws事件

配置AWS Health Dashboard邮件告警

4、创建方法选择 自定义json,事件模式输入,点击下一步


{
  "source": ["aws.health"],
  "detail-type": ["AWS Health Event"],
  "detail": {
    "service": ["*"]
  }
}
配置AWS Health Dashboard邮件告警

5、目标类型选择aws服务、目标选择lambda函数、选择一中创建好的lambda函数

配置AWS Health Dashboard邮件告警

6、继续下一步标签(可选)

7、检查并创建eventbridge

三、测试

1、创建测试

配置AWS Health Dashboard邮件告警

2、写入测试模板–保存

配置AWS Health Dashboard邮件告警
  • 测试模板
{
  "version": "0",
  "id": "fd9d4512-1eb0-50f6-0491-d016ae56aef0",
  "detail-type": "AWS Health Event",
  "source": "aws.health",
  "account": "123456789012",
  "time": "2023-02-15T10:07:10Z",
  "region": "us-east-1",
  "resources": [
    "i-abcd1111"
  ],
  "detail": {
    "eventArn": "arn:aws:health:us-east-1::event/EC2/AWS_EC2_OPERATIONAL_ISSUE",
    "service": "EC2",
    "eventTypeCode": "AWS_EC2_OPERATIONAL_ISSUE",
    "eventTypeCategory": "issue",
    "eventScopeCode": "PUBLIC",
    "communicationID": "01b0993207d81a09dcd552ebd1e633e36cf1f09a",
    "startTime": "Wed, 15 Feb 2023 22:07:07 GMT",
    "lastUpdatedTime": "Wed, 15 Feb 2023 22:07:07 GMT",
    "statusCode": "open",
    "eventRegion": "us-east-1",
    "eventDescription": [
      {
        "latestDescription": "We are investigating increased API Error rates and Latencies for Amazon Elastic Compute Cloud in the US-EAST-1 Region.",
        "language": "en_US",
        "eventStatus": 1
      }
    ]
  }
}

3、验证

  • 点击test–会生成响应–符合预期告警邮件格式
配置AWS Health Dashboard邮件告警
  • 邮箱验证–可正常收到告警邮件
配置AWS Health Dashboard邮件告警

发表评论

您的电子邮箱地址不会被公开。