【发布时间】:2021-05-26 01:18:00
【问题描述】:
我已将 AWS Inspector 设置为在扫描完成时向 SNS 主题发送消息。我还有一个配置为在该 SNS 主题上看到消息时触发的 Lambda。 Lambda 只是读取消息,调用 Inspector API 以生成扫描报告的 URL,然后将该 URL 通过电子邮件发送到邮箱。
所有这些都很好,除了当我在 Lambda 界面中使用 test SNS 消息时它出错。我认为问题一定出在 SNS 消息格式上,因为这一切都在发生变化,但我真的不知道如何获得一个真正的完整 SNS 以用作测试。
我的测试 SNS 如下所示:
{
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:eu-west-2:{{{accountId}}}:ExampleTopic",
"Sns": {
"Type": "Notification",
"MessageId": "95df01b4-ee98-9cb9-9999-4c221d41eb5e",
"TopicArn": "arn:aws:sns:eu-west-2:123456789012:ExampleTopic",
"Subject": "example subject",
"Message": {
"template": "arn:aws:inspector:eu-west-2:accountID:target/0-0000aaaa/template/0-0000aaaa",
"findingsCount": "{arn:aws:inspector:eu-west-2:account:rulespackage/0-0000aaaa}",
"run": "arn:aws:inspector:eu-west-2:123456789012:target/0-0000aaaa/template/0-0000aaaa/run/0-0000aaaa",
"time": "2021-01-29T12:29:24.758Z",
"event": "ASSESSMENT_RUN_COMPLETED",
"target": "arn:aws:inspector:eu-west-2:accountId:target/0000aaaa"
},
"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"
}
}
}
}
]
}
我的代码中出错的部分,包括我所做的所有调试工作是:
def lambda_handler(event, context):
#get message from SNS
#record = event['Records']
#logger.debug(type(record))
#logger.debug(record)
message = json.loads(event['Records'][0]['Sns']['Message'])
logger.debug(type(message))
logger.debug(message)
run_arn = message['run']
logger.debug(type(run_arn))
logger.debug(run_arn)
Lambda 控制台出现的错误是:
"errorMessage": "the JSON object must be str, bytes or bytearray, not dict",
指的是 json.loads 行。但是,正如我所提到的,当我运行 Inspector 扫描时,Lambda 可以正常工作(除了调试 CloudWatch 日志中的内容)并发送电子邮件。我哪里错了?!
【问题讨论】:
标签: python aws-lambda amazon-sns