回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我已经设置了一些cloud watch警报,当警报关闭时,它会触发一个lambda函数。在lambda中,我试图读取json并将其通知给MS团队。但我无法从基于json的数据库中获取报警名称</p>
<p>下面是json</p>
<pre><code>{
'Records': [
{
'EventSource': 'aws:sns',
'EventVersion': '1.0',
'EventSubscriptionArn': 'arn:aws:sns:ap-southeast-1:123:-teams-lambda-trigger:123-971d-4f70-927e-123',
'Sns': {
'Type': 'Notification',
'MessageId': '12-d0b8-5a86-8b33-123',
'TopicArn': 'arn:aws:sns:ap-southeast-1:123:vip-prestogo-teams-lambda-trigger',
'Subject': 'ALARM: "AuthenticationFailedException-was101" in Asia Pacific (Singapore)',
'Message': '{"AlarmName":"AuthenticationFailedException-was101","AlarmDescription":"Found \\"AuthenticationFailedException\\" in 123","AWSAccountId":"123","NewStateValue":"ALARM","NewStateReason":"Threshold Crossed: 1 out of the last 1 datapoints [1.0 (30/08/20 07:38:00)] was greater than or equal to the threshold (-1.0) (minimum 1 datapoint for OK -> ALARM transition).","StateChangeTime":"2020-08-30T07:39:22.330+0000","Region":"Asia Pacific (Singapore)","AlarmArn":"arn:aws:cloudwatch:ap-southeast-1:123:alarm:AuthenticationFailedException-was101","OldStateValue":"OK","Trigger":{"MetricName":"AuthenticationFailedException-was101","Namespace":"AuthenticationFailedException-was101","StatisticType":"Statistic","Statistic":"AVERAGE","Unit":null,"Dimensions":[],"Period":60,"EvaluationPeriods":1,"ComparisonOperator":"GreaterThanOrEqualToThreshold","Threshold":-1.0,"TreatMissingData":"- TreatMissingData: notBreaching","EvaluateLowSampleCountPercentile":""}}',
'Timestamp': '2020-08-30T07:39:22.372Z',
'SignatureVersion': '1',
'Signature': '123/WJa6/3saRvSsz+eDW10LZaAlR7jMhnU4jE73UM/+123/123/123/123/123+j+pjE0nldGG+123/xouonYXLkBrfRQPtr1sv/RzrIJ/kTYr3EwSkGL032HNrOeWmdGZ9D4gIJ4ir/mbnbSZV7w==',
'SigningCertUrl': 'https://sns.ap-southeast-1.amazonaws.com/SimpleNotificationService.pem',
'UnsubscribeUrl': 'https://sns.ap-southeast-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:ap-southeast-1:123:-teams-lambda-trigger:46235663-971d-4f70-927e-0a420040a154',
'MessageAttributes': {
}
}
}
]
}
</code></pre>
<p>按以下方式尝试,但失败:</p>
<pre><code> message = event['Records'][0]['Sns']['Message']
alarm =message['AlarmName']
</code></pre>
<p>我得到的错误如下:</p>
<p>字符串索引必须是整数:TypeError
回溯(最近一次呼叫最后一次):
lambda_处理程序中的文件“/var/task/lambda_function.py”,第15行
“文本”:事件['Records'][0]['Sns']['Message']['AlarmDescription']
TypeError:字符串索引必须是整数</p>
<p>请帮忙</p>