【问题标题】:How to I parse an SNS test notification from Lambda in python?如何在 python 中解析来自 Lambda 的 SNS 测试通知?
【发布时间】: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


    【解决方案1】:

    似乎您试图将 dict 解析为应该是 json 的字符串。
    message = json.loads(event['Records'][0]['Sns']['Message'])

    Message 组件是一个字典,不需要解析为 json。

    只是不要将其解析为 json:

    message = event['Records'][0]['Sns']['Message']
    

    【讨论】:

    • 所以我在 Lambda 控制台中的测试现在可以正常工作,但是当我运行 Inspector 扫描并触发 Lambda 时,它会引发错误,“字符串索引必须是整数”行“run_arn = message ['运行']"。
    • 打印message的值并贴在这里
    • 这是消息的值:{ "template": "arn:aws:inspector:eu-west-2:123456789123:target/0-ee11ee11/template/0-ee11ee11", "findingsCount": "{arn:aws:inspector:eu-west-2:123456789123:rulespackage/0-AizSYyNq=2}", "run": "arn:aws:inspector:eu-west-2:123456789123:target/0-ee11ee11/template/0-ee11ee11/run/0-1nkYs9qe", "time": "2021-02-24T09:46:15.296Z", "event": "ASSESSMENT_RUN_COMPLETED", "target": "arn:aws:inspector:eu-west-2:123456789123:target/0-ee11ee11" }
    • 然而这行得通:message = { "template": "arn:aws:inspector:eu-west-2:516368158875:target/0-eqO1NPFu/template/0-A72WFYi0", "findingsCount": "{arn:aws:inspector:eu-west-2:146838936955:rulespackage/0-AizSYyNq=2}", "run": "arn:aws:inspector:eu-west-2:516368158875:target/0-eqO1NPFu/template/0-A72WFYi0/run/0-1nkYs9qe", "time": "2021-02-24T09:46:15.296Z", "event": "ASSESSMENT_RUN_COMPLETED", "target": "arn:aws:inspector:eu-west-2:516368158875:target/0-eqO1NPFu" } run_arn = message["run"] print(run_arn)
    猜你喜欢
    • 1970-01-01
    • 2018-01-12
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 2018-12-21
    • 2017-04-18
    相关资源
    最近更新 更多