Bug: SQSRecord.decoded_nested_sns_event raises KeyError on SNS messages delivered to SQS

未关闭 适合新手
#8,913 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@hyeonsang010716 已经在做这个了。

开始于 2026年11月26日。

  • #8914 来自 @hyeonsang010716 —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
79/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
aws, python
领域
backend, cloud

调研方向

先阅读 SNSMessage 和现有的 test_decode_nested_sns_event 测试,然后检查 tests/events/snsSqsEvent.json 和 tests/events/snsSqsFifoEvent.json。添加测试覆盖所列的可选属性和键的其他拼写形式,并将 issue 提出的行为作为预期结果。完成标准是测试确认通过 SQS 传递的 SNS 消息能够公开这些属性且不会出现 KeyError,同时 Lambda SNS 事件的值保持不变。

由索引模型根据 Issue 内容生成。

描述

Expected Behaviour

SQSRecord.decoded_nested_sns_event returns an SNSMessage whose properties work on the message SNS sends to SQS, the same way the SnsNotificationModel parser model and SnsSqsEnvelope already handle it (#272, #1299, #1606).

Current Behaviour

SNSMessage only reads the keys of the Lambda SNS event (Records[].Sns). The SNS message in an SQS body is different:

  • It uses UnsubscribeURL and SigningCertURL instead of UnsubscribeUrl and SigningCertUrl, as in the SNS to SQS example.
  • It leaves out Subject and MessageAttributes when they are not set.
  • For FIFO topics with content-based deduplication it has no Signature, SignatureVersion or SigningCertURL (payload in #1590).

So unsubscribe_url, signing_cert_url, subject and message_attributes raise KeyError on tests/events/snsSqsEvent.json, and signature and signature_version also raise on tests/events/snsSqsFifoEvent.json. The existing test_decode_nested_sns_event doesn't read these properties.

Code snippet
import json

from aws_lambda_powertools.utilities.data_classes import SQSEvent

# Body of an SQS message that an SNS topic delivered (raw message delivery disabled)
sns_body = {
    "Type": "Notification",
    "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
    "TopicArn": "arn:aws:sns:eu-west-1:123456789012:orders",
    "Message": "hello world",
    "Timestamp": "2026-11-26T10:00:00.000Z",
    "SignatureVersion": "1",
    "Signature": "EXAMPLEpH+..",
    "SigningCertURL": "https://sns.eu-west-1.amazonaws.com/SimpleNotificationService-0000000000000000000000.pem",
    "UnsubscribeURL": "https://sns.eu-west-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-1:123456789012:orders:0000",
}
event = SQSEvent({"Records": [{"messageId": "1", "body": json.dumps(sns_body), "eventSource": "aws:sqs"}]})
sns_message = next(event.records).decoded_nested_sns_event

for prop in ("unsubscribe_url", "signing_cert_url", "subject", "message_attributes"):
    try:
        print(f"{prop}: {getattr(sns_message, prop)!r}")
    except KeyError as exc:
        print(f"{prop}: KeyError {exc}")

Output:

unsubscribe_url: KeyError 'UnsubscribeUrl'
signing_cert_url: KeyError 'SigningCertUrl'
subject: KeyError 'Subject'
message_attributes: KeyError 'MessageAttributes'
Possible Solution

In SNSMessage, read both spellings for unsubscribe_url and signing_cert_url, return None from subject, signature, signature_version and signing_cert_url when the key is missing, and an empty dict from message_attributes. This matches the optional fields of SnsNotificationModel. The Lambda SNS event keeps returning the same values.

Steps to Reproduce
  1. Subscribe an SQS queue to an SNS topic, with raw message delivery disabled.
  2. Read record.decoded_nested_sns_event.unsubscribe_url in a Lambda function triggered by the queue, or run the snippet above.
  3. It raises KeyError: 'UnsubscribeUrl'.
Did you run the reproduction yourself?

Yes, I ran it and saw the failure

Powertools for AWS Lambda (Python) version

3.41.0

AWS Lambda function runtime

3.14

Packaging format used

PyPi

Debugging logs

主要语言
Python
星标
3.3k
派生
511
平均合并
2 天 23 小时
30 天内合并 PR
50

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

aws-powertools/powertools-lambda-python 的其他 Issue

查看 aws-powertools/powertools-lambda-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。