0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

aws-sdk-railsの4系から5系へのバージョンアップ

Last updated at Posted at 2025-04-07

はじめに

こんにちは、Gakken LEAPのバックエンドエンジニアのmizunoです。
現在、私が担当しているプロダクトでは Amazon SES を使ってメール配信を行っていますが、aws-sdk-rails を 5 系へバージョンアップしたところ、テストが失敗しました。
この変更に伴う対応をメモとして残します。

発生したエラー

テストを実行したところ、以下のエラーが発生しました。
SES の設定に関するメソッドが存在しなくなっていました。

bin/rails aborted!
NoMethodError: undefined method 'add_action_mailer_delivery_method' for module Aws::Rails (NoMethodError)

Aws::Rails.add_action_mailer_delivery_method(

原因

aws-sdk-rails 5.0.0 のリリースノートを確認したところ、以下の変更が行われていました。

Feature - [Major Version] Remove dependencies on modular feature gems: aws-actiondispatch-dynamodb, aws-actionmailer-ses, aws-actionmailbox-ses, aws-activejob-sqs, and aws-record-rails.

つまり、aws-actionmailer-ses などの関連 gem への依存が削除されたため、これまで使えていたメソッドが利用できなくなったようです。
さらに、add_action_mailer_delivery_method メソッドは削除され、代替手段が提供されています。

Issue - Remove Aws::Rails.add_action_mailer_delivery_method in favor of ActionMailer::Base.add_delivery_method or the Railtie and configuration in aws-actionmailer-ses ~> 1.

対応方法

aws-actionmailer-sesのドキュメントを参考に対応しました。

1. Gemfile に aws-actionmailer-ses を追加

ドキュメントの手順通り、aws-actionmailer-ses を追加します。
これにより、aws-sdk-sesaws-sdk-sesv2が導入されます。

Gemfile
gem 'aws-sdk-rails', '~> 5'
+ gem 'aws-actionmailer-ses', '~> 1'

2. delivery_method の変更

これまで、Amazon SESのAPI v1を利用していましたが、aws-sdk-railsのバージョンアップを機にSES API v2へ移行しました。
SES API v2を利用することで、より効率的なメール配信が可能になり、今後の新機能開発にも対応しやすくなります。

そのため、config.action_mailer.delivery_method の設定を :ses から :ses_v2 に変更し、さらに ses_v2_settings を追加しました。
この設定により、SES API v2を利用したメール配信が可能になります。

config/environments/production.rb
Rails.application.configure do |config|
- config.action_mailer.delivery_method = :ses
+ config.action_mailer.delivery_method = :ses_v2
+ config.action_mailer.ses_v2_settings = { region: 'ap-northeast-1' }
end

3. add_action_mailer_delivery_method の削除

以下のコードは不要になったため削除しました。

- # SES
- Aws::Rails.add_action_mailer_delivery_method(
-   :ses,
-   credentials: 'credentials',
-   region: 'ses_region',
- )

まとめ

最後にテストを実行し、問題がないことを確認しました。
また、テスト環境で実際にメールを送信し、無事にメールが届くことを確認しました。
今回の対応を通じて、テストの重要性を改めて実感しました。

エンジニア募集

Gakken LEAP では教育をアップデートしていきたいエンジニアを絶賛大募集しています!!
ぜひお気軽にカジュアル面談へお越しください!!

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?