How-to centralized integration of eventbridge event notifications sent to feishu

时间:2023-01-11 16:20:57

简介

在使用亚马逊云的过程中,各种服务的通知事件在日常运维里常常发挥着关键作用。但在实际使用过程中,这些通知常常因为各种原因被忽略而导致意外的损失。如:亚马逊云的维护事件会发送通知邮件到账号的注册邮箱,但这些邮箱通常无人值守,使得在维护事件发生时,客户会遭遇“意外”停机。另外,客户部署的工作负载也有各种自定义的通知希望集成到一个统一的客户端进行提醒;而现在各种流行的即时通信软件都有移动客户端,同时基本都支持webhook机制,外部通过API调用,即可传入相应的信息;因此即时通信软件是接收这些通知的理想终端。

但如果您管理着多个AWS账号,而这些账号可能又属于多个不同的AWS Organization,那您需要在每个账号里分别做配置;另外,日后新增账号时,也得配置一次。那这样看起来工作量也不小。

为此,我们在原有告警通知方案的基础上,增加了对AWS Organization和多子账号, 以便提高您的运维效率。

说明:此方案仅目前仅支持AWS Global Regions,在AWS 北京和宁夏区暂时不支持事件的跨区传输。

架构设计

架构图

How-to centralized integration of eventbridge event notifications sent to feishu

相关服务介绍

  • Event Bridge – 用于事件的监听及转发;
  • SNS – 用于服务间的解耦合;
  • Lambda – 发送事件到飞书的的代码;
  • IAM – 用于给予账号发送事件信息的角色。

架构介绍

  • 本架构支持在多个AWS Organization的部署,支持跨区域。
  • 发送消息到飞书的代码只需要在任意一个账号中部署一次(此账号我们成为主账号),且全部采用无服务器架构,可以有效的节约企业成本。
  • 在主账号中创建专门的事件总线(Target Event Bus),该Event Bus配置规则发送事件到SNS;Lambda用于接收SNS的事件,调用飞书的接口,发送信息。
  • 主账号的默认事件总线(Default Event Bus)配置规则,用于对事件进行筛选并发送事件到Target Event Bus。
  • Target Event Bus需要配置一个Resource Based Policy,通过Organization ID或者AWS Account ID对其他账号进行信任,严格控制安全。
  • 其他AWS Organization的账号通过Organization管理账号创建的StackSet来部署 IAM Rule 和规则。
  • 子账号使用规则筛选事件并把事件转发到主账号的Target Event Bus。

准备工作

创建飞书机器人并将webbook记录一下

How-to centralized integration of eventbridge event notifications sent to feishu

Organization Master 部署

IAM

创建执行 lambda function 需要的iam role

How-to centralized integration of eventbridge event notifications sent to feishu

Lambda

创建 lambda function

How-to centralized integration of eventbridge event notifications sent to feishu

创建 lambda layer

这里 Compatible architectures & runtimes 一定要和创建 function 的一致

How-to centralized integration of eventbridge event notifications sent to feishu

暂时无法在飞书文档外展示此内容

源码可以从这里找到 https://github.com/Chris-wa-He/AWS-Lambda-notifier/tree/Feishu-notifier

lambda function 添加 layer

How-to centralized integration of eventbridge event notifications sent to feishu

更新 lambda function 代码并部署

import os
import boto3
from feishu import Feishu
from alarm import Alarm
secretURL = "https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxxxxxxxxxxxxxxxx"
# Initial Feishu handler
feishu = Feishu(secretURL)
def lambda_handler(event, context):
print(event)
msg = msg_format(event)
print(msg)
fsAlarm = Alarm(
description=msg,
)
feishu.send_text_msg(fsAlarm)
response = {
"statusCode": 200,
"body": "Message Sent."
}
return response
def msg_format(event):
try:
# 消息来源是SNS,取 $.Records[0].Sns.Message,并对字符串进行一些处理,确保发送时可以正常显示
msg = event['Records'][0]['Sns']['Message']
# 进行字符串处理后返回,以确保IM客户端正确显示
msg = msg.replace("\\n", "\n")
if msg[0] == '\"' and msg[-1] == '\"':
msg = msg[1:-1]
return msg
except:
# 消息来源不是SNS,直接返回
return event

SNS

创建 topic

创建 Subscriptions

EventBridge

Create event bus

How-to centralized integration of eventbridge event notifications sent to feishu

Resource-base policy (PS: 可以在 Create event bus 时 Load template 加载默认模板 选择其中的 allow_all_accounts_from_organization_to_put_events polic )

Create rules

How-to centralized integration of eventbridge event notifications sent to feishu

How-to centralized integration of eventbridge event notifications sent to feishu

{ "source": [{ "prefix": "" }] }

How-to centralized integration of eventbridge event notifications sent to feishu

子账号配置

每个需要被整合事件的子账号都需要在EventBridge里面的Default Event Bus中部署规则和所需的IAM角色。

IAM

How-to centralized integration of eventbridge event notifications sent to feishu

Permission

<<Organization Account ID>> 需要替换成主账号ID

<<Event Bus Name>> 需要替换成主账号 event bus name

{ "Version": "2012-10-17", "Statement": [ { "Action": [ "events:PutEvents" ], "Resource": [ "arn:aws:events:us-east-1:<<Organization Account ID>>:event-bus/<<Event Bus Name>>" ], "Effect": "Allow" } ] }

Trust relationships

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "events.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }

EventBridge

敲黑板 这里一定要选择 default event bus

创建 ec2 status rules

How-to centralized integration of eventbridge event notifications sent to feishu

How-to centralized integration of eventbridge event notifications sent to feishu

创建 ec2 scheduled change rules 同理

event pattern

{ "source": ["aws.health"], "detail-type": ["AWS Health Event"], "detail": { "service": ["EC2"], "eventTypeCategory": ["scheduledChange"] } }

 

测试

将 ec2 stop & start

How-to centralized integration of eventbridge event notifications sent to feishu

卸载

将上述创建的资源逆向删除即可

参考连接

​https://aws.amazon.com/premiumsupport/knowledge-center/eventbridge-notification-scheduled-events/​

​https://aws.amazon.com/cn/blogs/china/centralized-integration-of-eventbridge-event-notifications-sent-to-ding-talk-and-enterprise-wechat/​

​https://aws.amazon.com/cn/blogs/china/enable-wechat-dingtalk-alarm-notification-with-one-click-based-on-aws-serverless/​

​https://github.com/Chris-wa-He/AWS-Lambda-notifier/tree/Feishu-notifier​


License: ​​Attribution-NonCommercial-ShareAlike 4.0 International​

本文出自 ​​Suzf Blog​​。 如未注明,均为 SUZF.NET 原创。

转载请注明:​​https://suzf.net/post/1458​