使用企业微信和lambda函数接收SNS报警

1

首先开通企业微信:
https://work.weixin.qq.com/wework_admin/loginpage_wx#contacts
登录管理后台,使用二维码或微信邀请自己加入企业,然后就可以再联系人列表中看到企业。
应用管理中创建接收aws报警的应用
在这里插入图片描述
点击应用进入查看agentid和secret
在这里插入图片描述
在我的企业中查看企业id
在这里插入图片描述
可以向应用程序发个消息测试

2

创建lambda应用,使用python2.7,绑定sns,sns中设置接收aws events消息(cloudwatch发送到sns的消息处理于此类似)
在这里插入图片描述
编写lambda函数,需要用到request包,所以需要把函数脚本放在lambda-cloudwatch-wechat文件夹中然后

pip install requests -t lambda-cloudwatch-wechat

函数脚本

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function
from __future__ import unicode_literals
import json
import requests

corpid = 'ww1b7xxxxxxxxx0c8c'
agentid = '1000002'
appsecret = 'DkOW6xxxxxxxxxxxxxxxxxxxxxxxy9wg'
toparty = 1

token_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + \
    corpid + '&corpsecret=' + appsecret

def lambda_handler(event, context):
    info = ''
    message = str(event['Records'][0]['Sns']['Message'])
    print("From SNS: " + message)
    for k,w in dict.items(eval(message)):
        info = info + k + ': ' + str(w) + '\n'
    info = "Asia AWS Events:\n" + info
    print(info)
    req = requests.get(token_url)
    accesstoken = req.json()['access_token']
    print(accesstoken)
    msgsend_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken

    params = {
        # "touser": touser,
        "toparty": toparty,
        "msgtype": "text",
        "agentid": agentid,
        "text": {
            "content": info
        },
        "safe": 0
    }
    req = requests.post(msgsend_url, data=json.dumps(params))

然后将脚本和用到的模块打包成zip文件,不要直接将lambda-cloudwatch-wechat文件夹打包,然后上传到lambda,修改corpid为自己企业id,agentid和appsecret为自己应用的id和密钥。另外注意lambda的处理程序中的内容
在这里插入图片描述

3

测试
配置测试事件,或者触发sns中的事件(比如ec2开启关闭)

{
  "Records": [
    {
      "EventSource": "aws:sns",
      "EventVersion": "1.0",
      "EventSubscriptionArn": "arn:aws:sns:ap-northeast-2:{{{accountId}}}:ExampleTopic",
      "Sns": {
        "Type": "Notification",
        "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
        "TopicArn": "arn:aws:sns:ap-northeast-2:123456789012:ExampleTopic",
        "Subject": "example subject",
        "Message": {
          "version": "0",
          "id": "e428780b-2455-526e-d668-6a25f2ba8ef9",
          "detail-type": "EC2 Instance State-change Notification",
          "source": "aws.ec2",
          "account": "036379725830",
          "time": "2019-11-22T10:19:59Z",
          "region": "ap-northeast-1",
          "resources": [
            "arn:aws:ec2:ap-northeast-1:036379725830:instance/i-01810f9551a66b938"
          ],
          "detail": {
            "instance-id": "i-01810f9551a66b938",
            "state": "stopped"
          }
        },
        "Timestamp": "1970-01-01T00:00:00.000Z",
        "SignatureVersion": "1",
        "Signature": "EXAMPLE",
        "SigningCertUrl": "EXAMPLE",
        "UnsubscribeUrl": "EXAMPLE",
        "MessageAttributes": {
          "Test": {
            "Type": "String",
            "Value": "TestString"
          },
          "TestBinary": {
            "Type": "Binary",
            "Value": "TestBinary"
          }
        }
      }
    }
  ]
}

查看企业号中收到的消息
在这里插入图片描述
参考github上的一个项目:
https://github.com/erwen/lambda-cloudwatch-wechat

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值