Amazon SESバウンスイベントをEventBridgeに送信時、入力トランスフォーマーでdiagnosticCodeを利用する場合エラーになることがあります

Amazon SESバウンスイベントをEventBridgeに送信時、入力トランスフォーマーでdiagnosticCodeを利用する場合エラーになることがあります

Clock Icon2025.06.20

困っていること

以下の記事を参考に、SESでバウンスが発生した場合、Amazon SESバウンスイベントをAmazon EventBridgeに送信し、Amazon SNSトピックでメール通知を行っています。

https://docs.aws.amazon.com/ja_jp/ses/latest/dg/monitor-sending-using-event-publishing-setup.html

EventBridgeでは入力トランスフォーマーを利用し、メール通知の可読性を向上させています。

現在、EventBridgeの入力パスは以下のとおりです。

{
  "account": "$.account",
  "bounceType": "$.detail.bounce.bounceType",
  "bounceSubType": "$.detail.bounce.bounceSubType",
  "status": "$.detail.bounce.bouncedRecipients[0].status",
  "diagnosticCode": "$.detail.bounce.bouncedRecipients[0].diagnosticCode",
  "emailAddress": "$.detail.bounce.bouncedRecipients[0].emailAddress",
  "source": "$.detail.mail.source",
  "subject": "$.detail.mail.commonHeaders.subject",
  "configurationSet": "$.detail.mail.tags[\"ses:configuration-set\"][0]",
  "timestamp": "$.detail.bounce.timestamp",
  "messageId": "$.detail.mail.messageId"
}

EventBridgeの入力テンプレートは以下のとおりです。

"🚨 SES メール配信エラー通知"
""
"【バウンス情報】"
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
"・発生時刻: <timestamp>"
"・対象アカウント: <account>"
"・バウンスタイプ: <bounceType>"
"・バウンスサブタイプ: <bounceSubType>"
"・HTTPステータス: <status>"
"・エラー詳細: <diagnosticCode>"
""
"【メール送信情報】"
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
"・送信元: <source>"
"・送信先: <emailAddress>"
"・件名: <subject>"
"・設定セット: <configurationSet>"
"・メッセージID: <messageId>"
""
"【⚠️ 必要な対応アクション】"
"━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
"■ Permanentバウンスの場合(即座に対応が必要):"
"・General/NoEmail/Suppressed → 該当メールアドレスを配信リストから即座に削除してください"
"・継続送信は送信者評価に悪影響を与え、送信機能停止の原因となります"
"・OnAccountSuppressionList → アカウントレベルサプレッションリストで既に制御済み"
""
"■ Transientバウンスの場合(将来的に再送信可能):"
"・General → 一時的な問題のため、時間を置いて再送信を検討してください"
"・MailboxFull → 受信者の受信トレイが満杯、後日再送信を検討してください"
"・MessageTooLarge → メッセージサイズを小さくして再送信を検討してください"
"・ContentRejected → メッセージ内容を見直して再送信を検討してください"
"・AttachmentRejected → 添付ファイルを削除/変更して再送信を検討してください"
""
"■ Undeterminedバウンスの場合:"
"・バウンス理由が不明のため、リターンパスアドレスに送信されたバウンスメールで詳細を確認してください"
""
"📖 詳細情報: https://docs.aws.amazon.com/ses/latest/dg/notification-contents.html#bounce-types"

マネジメントコンソールでは、サンプルイベントを利用し、問題なく出力されることが確認できました。
cm-hirai-screenshot 2025-06-17 13.55.02

{
  "version": "0",
  "id": "12a18625-3328-fafd-2809-a5e16004f112",
  "detail-type": "Email Bounced",
  "source": "aws.ses",
  "account": "123456789012",
  "time": "2023-07-17T16:48:05Z",
  "region": "us-east-1",
  "resources": ["arn:aws:ses:us-east-1:123456789012:identity/example.com"],
  "detail": {
    "eventType": "Bounce",
    "bounce": {
      "bounceType": "Permanent",
      "bounceSubType": "General",
      "bouncedRecipients": [{
        "emailAddress": "recipient@example.com",
        "action": "failed",
        "status": "5.1.1",
        "diagnosticCode": "smtp; 550 5.1.1 user unknown"
      }],
      "timestamp": "2017-08-05T00:41:02.669Z",
      "feedbackId": "01000157c44f053b-61b59c11-9236-11e6-8f96-7be8aexample-000000",
      "reportingMTA": "dsn; mta.example.com"
    },
    "mail": {
      "timestamp": "2017-08-05T00:40:02.012Z",
      "source": "Sender Name <sender@example.com>",
      "sourceArn": "arn:aws:ses:us-east-1:123456789012:identity/sender@example.com",
      "sendingAccountId": "123456789012",
      "messageId": "EXAMPLE7c191be45-e9aedb9a-02f9-4d12-a87d-dd0099a07f8a-000000",
      "destination": ["recipient@example.com"],
      "headersTruncated": false,
      "headers": [{
        "name": "From",
        "value": "Sender Name <sender@example.com>"
      }, {
        "name": "To",
        "value": "recipient@example.com"
      }, {
        "name": "Subject",
        "value": "Message sent from Amazon SES"
      }, {
        "name": "MIME-Version",
        "value": "1.0"
      }, {
        "name": "Content-Type",
        "value": "multipart/alternative; boundary=\"----=_Part_7307378_1629847660.1516840721503\""
      }],
      "commonHeaders": {
        "from": ["Sender Name <sender@example.com>"],
        "to": ["recipient@example.com"],
        "messageId": "EXAMPLE7c191be45-e9aedb9a-02f9-4d12-a87d-dd0099a07f8a-000000",
        "subject": "Message sent from Amazon SES"
      },
      "tags": {
        "ses:configuration-set": ["ConfigSet"],
        "ses:source-ip": ["192.0.2.0"],
        "ses:from-domain": ["example.com"],
        "ses:caller-identity": ["ses_user"]
      }
    }
  }
}

しかし、導入後、一部のイベントでは通知されず、EventBridgeはFailedInvocationsとなり、メール通知ができません。

原因をご教示ください。

エラー原因

調査の結果、通知に失敗する EventBridge に渡されるイベント内容は以下のとおりでした。
これは、メール送信先のアドレスが存在しないことによるエラーです。

{
  "version": "0",
  "id": "bdd45965-5254-58da-02c9-8d3978b99b80",
  "detail-type": "Email Bounced",
  "source": "aws.ses",
  "account": "アカウントID",
  "time": "2025-06-16T01:52:26Z",
  "region": "ap-northeast-1",
  "replay-name": "ses-Bounce-notify-2",
  "resources": [
    "arn:aws:ses:ap-northeast-1:アカウントID:configuration-set/test"
  ],
  "detail": {
    "eventType": "Bounce",
    "bounce": {
      "feedbackId": "01060197767016a8-6ffcd8e6-4e05-4b73-88a9-30e8c4c68c68-000000",
      "bounceType": "Permanent",
      "bounceSubType": "General",
      "bouncedRecipients": [
        {
          "emailAddress": "abcabcrr@example.com",
          "action": "failed",
          "status": "5.1.1",
          "diagnosticCode": "smtp; 550-5.1.1 The email account that you tried to reach does not exist. Please try\r\n 550-5.1.1 double-checking the recipient's email address for typos or\r\n 550-5.1.1 unnecessary spaces. For more information, go to\r\n 550 5.1.1  https://support.google.com/mail/?p=NoSuchUser xxx - gsmtp"
        }
      ],
      "timestamp": "2025-06-16T01:52:26.872Z",
      "reportingMTA": "dns; googlemail.com"
    },
    "mail": {
      "timestamp": "2025-06-16T01:52:22.168Z",
      "source": "example@example.com",
      "sourceArn": "arn:aws:ses:ap-northeast-1:アカウントID:identity/example.com",
      "sendingAccountId": "アカウントID",
      "messageId": "xxx",
      "destination": ["abcabcrr@example.com"],
      "headersTruncated": false,
      "headers": [
        { "name": "From", "value": "example@example.com" },
        { "name": "Reply-To", "value": "reply@example.com" },
        { "name": "To", "value": "abcabcrr@example.com" },
        {
          "name": "Subject",
          "value": "xxxxx"
        },
        { "name": "MIME-Version", "value": "1.0" },
        { "name": "Content-Type", "value": "text/plain; charset=UTF-8" },
        { "name": "Content-Transfer-Encoding", "value": "base64" }
      ],
      "commonHeaders": {
        "from": ["example1@example.com"],
        "replyTo": ["reply@example.com"],
        "to": ["abcabcrr@example.com"],
        "messageId": "0106019776700498-58d33a7d-de79-4d2a-8fd0-57dde6174693-000000",
        "subject": "xxxxx"
      },
      "tags": {
        "ses:source-tls-version": ["TLSv1.3"],
        "ses:operation": ["SendEmail"],
        "ses:configuration-set": ["test"],
        "ses:source-ip": ["xx.xx.xx.xx"],
        "ses:from-domain": ["example.com"],
        "ses:caller-identity": ["StepFunctions-ses-email-role-eor8ynebg"]
      }
    }
  }
}

diagnosticCodeの以下の値が原因と考えられます。

smtp; 550-5.1.1 The email account that you tried to reach does not exist. Please try\r\n 550-5.1.1 double-checking the recipient's email address for typos or\r\n 550-5.1.1 unnecessary spaces. For more information, go to\r\n 550 5.1.1 https://support.google.com/mail/?p=NoSuchUser xxx - gsmtp

この diagnosticCode の値には、入力パスで定義した変数の値としてサポートされていない \ (バックスラッシュ) が含まれているため、EventBridge が失敗した可能性が高いです。

EventBridge ですべての JSON パス構文がサポートされているわけではなく、構文は実行時に評価されます。サポートされている構文には以下が含まれます。

なお、変数として利用した場合にエラーとなりますが、入力テンプレートにて、直接 \ (バックスラッシュ) を含む diagnosticCode の値(固定値)であれば送信可能です。

"・エラー診断: smtp; 550-5.1.1 The email account that you tried to reach does not exist. Please try\r\n 550-5.1.1 double-checking the recipient's email address for typos or\r\n 550-5.1.1 unnecessary spaces. For more information, go to\r\n 550 5.1.1 https://support.google.com/mail/?p=NoSuchUser xxx - gsmtp"
固定文章

また、サンプルイベントを利用すると以下のとおり、改行が存在することが確認できますが、エラーにはならず出力されます。実際にはエラーになりますので、注意が必要です。

cm-hirai-screenshot 2025-06-17 15.14.46

改行が存在しない場合もある

diagnosticCode には改行が含まれない場合もあり、その際はエラーなく通知されるケースもあります。

以下は、抑制リストに登録されているメールアドレスに送信した際のバウンス内容です。diagnosticCode の値に関して、実際にどのような内容が含まれるかは公式ドキュメントに網羅的な情報がないため、いくつかのパターンでバウンスイベントを発生させて確認する必要がありました。

Amazon SES did not send the message to this address because it is on the suppression list for your account. For more information about removing addresses from the suppression list, see the Amazon SES Developer Guide at https://docs.aws.amazon.com/ses/latest/DeveloperGuide/sending-email-suppression-list.html

対応方法

対応策として、以下が考えられます。

  1. diagnosticCode を利用しない

    • 診断コードはバウンスの原因を確認するために有用ですが、今回の通り、EventBridge の入力トランスフォーマーでの処理に問題が生じてしまいます。
  2. 入力トランスフォーマーを利用せずに直接 SNS トピックに送信

    • JSON 形式での送信となるため可読性が低くなります。
  3. 入力トランスフォーマーを利用せずに Lambda 関数や AWS Step Functions ステートマシンをターゲットとして指定し、メッセージを整形して SNS へ送信する

    • リソースの作成が必要ですが、1と2に比べ、件名や本文を自由に修正でき、可読性も高くなります。

特に要件がなければ、可読性が高い3.の方法が適切でしょう。

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.