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

オープン 初心者向け
#8,913 コメント 0 件 リアクション 0 件 担当者 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時間
マージ済み PR(30日)
50

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

aws-powertools/powertools-lambda-python のほかの issue

aws-powertools/powertools-lambda-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。