python代码从AWS lambda向Microsoft团队发送通知

时间:2020-08-05 14:52:42

标签: python amazon-web-services aws-lambda

想法是将来自云监视警报的消息通知给Microsoft团队。这些消息在云监视警报描述中。

下面是传递给lambda的JSON:

{   'Records': [{       'EventSource': 'aws:sns',       'EventVersion': '1.0',      'EventSubscriptionArn': '71d-4f70-927e-0a420040a154',       'Sns': {            'Type': 'Notification',             'MessageId': '152c308c--bca8-67281c9c3c36',',           'Subject': 'ALARM: "StoreException-" in ()',            'Message': '{"AlarmName":"StoreException-WAS102","AlarmDescription":"StoreClosedException-in-1.1.1.1","AWSAccountId":"xxxxx","NewStateValue":"ALARM"}}

下面是我的python lambda代码:

    import urllib3 
    import json
    http = urllib3.PoolManager() 
    def lambda_handler(event, context): 
        print(event)
        message = event['Records'][0]['Sns']['Message']
        alarm_name = message['AlarmName']
        app_name = message['AlarmDescription']
        msg = {
       
            "text": app_name
        }
        encoded_msg = json.dumps(msg).encode('utf-8')
        resp = http.request('POST',url, body=encoded_msg)
    

但是我收到如下错误:

> string indices must be integers: TypeError Traceback (most recent call
> last):   File "/var/task/lambda_function.py", line 15, in
> lambda_handler
>     "text": app_name TypeError: string indices must be integers

我认为我在使用字典时遇到了问题。但不确定如何使其工作。谁能帮助我。

0 个答案:

没有答案