1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Lambda × SESでのメール送信エラー対応

Last updated at Posted at 2025-01-27

DKIM、DMARCの登録していないことによるエラー

SESからメールを送信されると受信したメールには下記のメッセージが入っていました。

Delivery has failed to these recipients or groups
Your message wasn't delivered because recipient's email prividor reject

調べてみると送信元となるメールアドレスがDKIM、DMARCポリシーを登録しないといけないため
受信側でリジェクトされていました。突貫で作っているとこういった初歩的な設定を飛ばしがちです。

これらはDKIMのCNAMEレコード、DMARCのTXTレコードを取得して
Route53に登録しておくことで解決しました。

参考にしたドキュメント

Lambdaのタイムアウトエラー

CloudWatchのログストリームからこんなエラーが出力されました。

Task timed out after 3.00 seconds

送信対象のIAMユーザーを読み込む処理をする際、処理の秒数が小さいとタイムアウトを起こしていました。
Lambdaのタイムアウトの時間を増やして解決しました。

例外処理の記述なし

LAMBDA_WARNING: Unhandled exception. The most likely cause is an issue in the function code. However, in rare cases, a Lambda runtime update can cause unexpected function behavior. For functions using managed runtimes, runtime updates can be triggered by a function change, or can be applied automatically. To determine if the runtime has been updated, check the runtime version in the INIT_START log entry. If this error correlates with a change in the runtime version, you may be able to mitigate this error by temporarily rolling back to the previous runtime version. For more information, see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-update.html

このエラーをまとめると下記の2点です。

・Lambda関数で未処理の例外が発生した
・ ランタイムの更新による影響も考えられる

この2つでしたがPythonのパージョンを3.13に変えても特に変化なし
例外時の処理except Exception as e:を使用することで解決しました。

SESでメール送信エラー

サンドボックス環境で対象のResourceにIAMポリシーに権限が付与されていない

検証したはずのIDでメールが送信できていたのに送信先のメールアドレスを変えると送信ができないエラーが起きました。

MessageRejected: Email address is not verified. The following identities failed the check in region…

サンドボックス環境だと送信元と送信先のメールアドレスをID検証しておく必要があります。
IAMポリシーの"ses:SendEmail","ses:SendRawEmail"のResourceに検証した2つのIDのARNを入れておくことで解決しました。

ポリシーのResourceが*だと送信されるのにメールのARNを入れると送信されない

設定セットでメールの送信ログをKinesisやS3に送る設定が入っている可能性があります。
このARNをResourceにいれることで送信することができました。

参考にしたドキュメント

他にもこんな記事を書いています!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?