Python Cloudwatch发送给SNS的JSON负载中有什么?我如何读取这些数据?
当Cloudwatch中触发警报时,我尝试使用Lambda解析数据。我目前正在使用SNS触发lambda;但是,我想知道发送给该Lambda的是什么数据,以便正确解析它Python Cloudwatch发送给SNS的JSON负载中有什么?我如何读取这些数据?,python,amazon-web-services,aws-lambda,amazon-sns,amazon-cloudwatch,Python,Amazon Web Services,Aws Lambda,Amazon Sns,Amazon Cloudwatch,当Cloudwatch中触发警报时,我尝试使用Lambda解析数据。我目前正在使用SNS触发lambda;但是,我想知道发送给该Lambda的是什么数据,以便正确解析它 如何读取SNS传递到Lambda的JSON报警数据?最简单、最准确的方法是打印(事件)并实际查看有效负载中的内容,官方AWS doco为SNS事件Amazon SNS示例事件提供了以下结构 此外,这个特定于CloudWatch的示例可能很有用 { "Records": [ { "EventSource": "aws:sns"
如何读取SNS传递到Lambda的JSON报警数据?最简单、最准确的方法是打印(事件)并实际查看有效负载中的内容,官方AWS doco为SNS事件Amazon SNS示例事件提供了以下结构 此外,这个特定于CloudWatch的示例可能很有用
{
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:eu-west-1:000000000000:cloudwatch-alarms:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"Sns": {
"Type": "Notification",
"MessageId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"TopicArn": "arn:aws:sns:eu-west-1:000000000000:cloudwatch-alarms",
"Subject": "ALARM: \"Example alarm name\" in EU - Ireland",
"Message": "{\"AlarmName\":\"Example alarm name\",\"AlarmDescription\":\"Example alarm description.\",\"AWSAccountId\":\"000000000000\",\"NewStateValue\":\"ALARM\",\"NewStateReason\":\"Threshold Crossed: 1 datapoint (10.0) was greater than or equal to the threshold (1.0).\",\"StateChangeTime\":\"2017-01-12T16:30:42.236+0000\",\"Region\":\"EU - Ireland\",\"OldStateValue\":\"OK\",\"Trigger\":{\"MetricName\":\"DeliveryErrors\",\"Namespace\":\"ExampleNamespace\",\"Statistic\":\"SUM\",\"Unit\":null,\"Dimensions\":[],\"Period\":300,\"EvaluationPeriods\":1,\"ComparisonOperator\":\"GreaterThanOrEqualToThreshold\",\"Threshold\":1.0}}",
"Timestamp": "2017-01-12T16:30:42.318Z",
"SignatureVersion": "1",
"Signature": "Cg==",
"SigningCertUrl": "https://sns.eu-west-1.amazonaws.com/SimpleNotificationService-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.pem",
"UnsubscribeUrl": "https://sns.eu-west-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-1:000000000000:cloudwatch-alarms:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"MessageAttributes": {}
}
}
]
}
最简单的方法是使用电子邮件JSON协议为cloudwatch发送警报数据的SNS主题创建新订阅,然后输入您的电子邮件并创建订阅
通过单击电子邮件中的验证链接确认订阅。当CloudWatch下次发送警报时,您将在电子邮件中获得JSON数据,然后您可以了解如何正确解析它。CloudWatch发送给SNS的实际有效负载
{
"AlarmName": "Example alarm name",
"AlarmDescription": "Example alarm description.",
"AWSAccountId": "000000000000",
"NewStateValue": "ALARM",
"NewStateReason": "Threshold Crossed: 1 datapoint (10.0) was greater than or equal to the threshold (1.0).",
"StateChangeTime": "2017-01-12T16:30:42.236+0000",
"Region": "EU - Ireland",
"OldStateValue": "OK",
"Trigger": {
"MetricName": "DeliveryErrors",
"Namespace": "ExampleNamespace",
"Statistic": "SUM",
"Unit": null,
"Dimensions": [],
"Period": 300,
"EvaluationPeriods": 1,
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"Threshold": 1
}
}
如果您想在Lambda中阅读此事件
import json
def lambda_handler(event, context):
alert_message = json.loads(json.dumps(event))["Records"][0]["Sns"]["Message"]
return {
'statusCode': 200,
'body': json.dumps(alert_message)
}
这是最明智的回答。非常感谢。
import json
def lambda_handler(event, context):
alert_message = json.loads(json.dumps(event))["Records"][0]["Sns"]["Message"]
return {
'statusCode': 200,
'body': json.dumps(alert_message)
}