LoginSignup
blue-night-blue
@blue-night-blue

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

herokuの環境変数RAILS_MASTER_KEY設定時のみエラーが出ます

解決したいこと

herokuでデプロイ後にエラーが出ないようにしたいです。解決方法を教えてください。

発生している問題・エラー

rails,active storage,s3,herokuという環境でrails練習用に画像投稿bbsを作っています。画像を保存するため、
https://qiita.com/hmmrjn/items/7cc5e5348755c517458a
に記載の内容の通りのことを実行した結果、ローカルだとエラーが出ない(※)のですが、デプロイするとエラーになりました。原因を突き詰めると、環境変数「RAILS_MASTER_KEY」を設定するとエラーになることが判明しました(エラー下記参照)。RAILS_MASTER_KEYを削除すると正常に動きますが、それだと当然S3との連携がききません。

※ローカルサーバーの画像保存先はローカルなのでS3と連携がうまくいってるかは不明

デプロイ後のサイト(ルートやそれ以外も同様)

Application error

An error occurred in the application 
and your page could not be served. 
If you are the application owner, 
check your logs for details. 
You can do this from the Heroku CLI with the command

heroku logs --tail
(デプロイ後にサイトにアクセスしたタイミング)

(中略)
2023-05-29T07:35:15.717335+00:00 app[web.1]: 	from /app/vendor/bundle/ruby/3.2.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
2023-05-29T07:35:15.717335+00:00 app[web.1]: 	from bin/rails:4:in `<main>'
2023-05-29T07:35:15.858884+00:00 heroku[web.1]: Process exited with status 1
2023-05-29T07:35:15.892876+00:00 heroku[web.1]: State changed from starting to crashed
2023-05-29T07:39:45.376661+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=bnb-imgbbs.herokuapp.com request_id=79a41334-133e-47ef-90d6-35ad449366da fwd="xxx" dyno= connect= service= status=503 bytes= protocol=https
2023-05-29T07:39:45.880471+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=bnb-imgbbs.herokuapp.com request_id=50aa4eb4-6738-487b-b783-4f5355c713d5 fwd="xxx" dyno= connect= service= status=503 bytes= protocol=httpsxxx

heroku logs --tail
(herokuにRAILS_MASTER_KEYをセットしたタイミング)

2023-05-29T07:34:35.366359+00:00 heroku[router]: at=info method=GET path="/" host=bnb-imgbbs.herokuapp.com request_id=4a8d10b4-d4ec-452a-878b-b588734ac032 fwd="xxx" dyno=web.1 connect=0ms service=214ms status=200 bytes=5602 protocol=https
2023-05-29T07:35:11.262532+00:00 heroku[web.1]: Restarting
2023-05-29T07:35:11.276374+00:00 heroku[web.1]: State changed from up to starting
2023-05-29T07:35:11.769443+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2023-05-29T07:35:11.795664+00:00 app[web.1]: - Gracefully stopping, waiting for requests to finish
2023-05-29T07:35:11.796241+00:00 app[web.1]: Exiting
2023-05-29T07:35:11.944541+00:00 heroku[web.1]: xxx with status 143
2023-05-29T07:35:10.769580+00:00 app[api]: Set RAILS_MASTER_KEY config vars by user xxx
2023-05-29T07:35:10.769580+00:00 app[api]: Release v33 created by user xxx
2023-05-29T07:35:13.636580+00:00 heroku[web.1]: Starting process with command `bin/rails server -p ${PORT:-5000} -e production`
2023-05-29T07:35:15.205805+00:00 app[web.1]: => Booting Puma
2023-05-29T07:35:15.205823+00:00 app[web.1]: => Rails 7.0.4.3 application starting in production 
2023-05-29T07:35:15.205823+00:00 app[web.1]: => Run `bin/rails server --help` for more startup options
2023-05-29T07:35:15.717108+00:00 app[web.1]: Exiting
2023-05-29T07:35:15.717310+00:00 app[web.1]: /app/vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.4.3/lib/active_support/core_ext/module/delegation.rb:304:in `dig': String does not have #dig method (TypeError)
2023-05-29T07:35:15.717312+00:00 app[web.1]: 	from /app/vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.4.3/lib/active_support/core_ext/module/delegation.rb:304:in `public_send'
(中略)
2023-05-29T07:35:15.717335+00:00 app[web.1]: 	from /app/vendor/bundle/ruby/3.2.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
2023-05-29T07:35:15.717335+00:00 app[web.1]: 	from bin/rails:4:in `<main>'
2023-05-29T07:35:15.858884+00:00 heroku[web.1]: xxx with status 1
2023-05-29T07:35:15.892876+00:00 heroku[web.1]: State changed from starting to crashed

該当するソースコード

不明

自分で試したこと

・master.keyとcredentials.ymlの削除&再作成→gitへ再プッシュ→heroku再デプロイ→駄目でした
・エラーコード+RAILS_MASTER_KEY のキーワードでグーグル叩きました。master.keyとcredentials.ymlの記載や作成ミスなどの記事は見つかりましたが、本件に該当するものはありませんでした。

備考

RAILS_MASTER_KEYを設定しない状態で画像アップした際のherokuのエラーログも参考までに記載します。未設定のため当然認証エラーが出てますが。

2023-05-29T08:15:22.978278+00:00 app[web.1]: I, [2023-05-29T08:15:22.978195 #2]  INFO -- : [5571e67a-088c-4acf-9643-3206ed32388c] Started POST "/comments" for xxx at 2023-05-29 08:15:22 +0000
2023-05-29T08:15:22.979123+00:00 app[web.1]: I, [2023-05-29T08:15:22.979079 #2]  INFO -- : [5571e67a-088c-4acf-9643-3206ed32388c] Processing by CommentsController#create as TURBO_STREAM
2023-05-29T08:15:22.979182+00:00 app[web.1]: I, [2023-05-29T08:15:22.979164 #2]  INFO -- : [5571e67a-088c-4acf-9643-3206ed32388c]   Parameters: {"authenticity_token"=>"[FILTERED]", "comment"=>{"content"=>"aa", "image"=>#<ActionDispatch::Http::UploadedFile:0x00007ffa0948a980 @tempfile=#<Tempfile:/tmp/RackMultipart20230529-2-ukpgab.jpg>, @original_filename="test.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"comment[image]\"; filename=\"test.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"posuto"}
2023-05-29T08:15:23.071308+00:00 app[web.1]: I, [2023-05-29T08:15:23.071222 #2]  INFO -- : [5571e67a-088c-4acf-9643-3206ed32388c] [36m  S3 Storage (30.6ms) [0m[32mUploaded file to key: rl6665c82fxwialpzjon0srfnj9c (checksum: Trn5DA27Wo04IEwrr83BqQ==)[0m
2023-05-29T08:15:23.071503+00:00 app[web.1]: I, [2023-05-29T08:15:23.071480 #2]  INFO -- : [5571e67a-088c-4acf-9643-3206ed32388c] Completed 500 Internal Server Error in 92ms (ActiveRecord: 34.1ms | Allocations: 111139)
2023-05-29T08:15:23.072059+00:00 app[web.1]: F, [2023-05-29T08:15:23.072037 #2] FATAL -- : [5571e67a-088c-4acf-9643-3206ed32388c]   
2023-05-29T08:15:23.072059+00:00 app[web.1]: [5571e67a-088c-4acf-9643-3206ed32388c] Aws::Errors::MissingCredentialsError (unable to sign request without credentials set):
2023-05-29T08:15:23.072060+00:00 app[web.1]: [5571e67a-088c-4acf-9643-3206ed32388c]   
2023-05-29T08:15:23.072060+00:00 app[web.1]: [5571e67a-088c-4acf-9643-3206ed32388c] app/controllers/comments_controller.rb:7:in `create'
2023-05-29T08:15:23.072338+00:00 heroku[router]: at=info method=POST path="/comments" host=bnb-imgbbs.herokuapp.com request_id=5571e67a-088c-4acf-9643-3206ed32388c fwd="xxx" dyno=web.1 connect=0ms service=289ms status=500 bytes=1827 protocol=https
2023-05-29T08:17:01.322545+00:00 app[web.1]: I, [2023-05-29T08:17:01.322432 #2]  INFO -- : [37842d95-667f-4876-8513-abfbdab0c2cf] Started POST "/comments" for xxx at 2023-05-29 08:17:01 +0000
2023-05-29T08:17:01.323441+00:00 app[web.1]: I, [2023-05-29T08:17:01.323392 #2]  INFO -- : [37842d95-667f-4876-8513-abfbdab0c2cf] Processing by CommentsController#create as TURBO_STREAM
2023-05-29T08:17:01.323503+00:00 app[web.1]: I, [2023-05-29T08:17:01.323477 #2]  INFO -- : [37842d95-667f-4876-8513-abfbdab0c2cf]   Parameters: {"authenticity_token"=>"[FILTERED]", "comment"=>{"content"=>"aiueo", "image"=>#<ActionDispatch::Http::UploadedFile:0x00007ffa09869240 @tempfile=#<Tempfile:/tmp/RackMultipart20230529-2-fyd2ib.jpg>, @original_filename="test.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"comment[image]\"; filename=\"test.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"posuto"}
2023-05-29T08:17:01.343681+00:00 app[web.1]: I, [2023-05-29T08:17:01.343573 #2]  INFO -- : [37842d95-667f-4876-8513-abfbdab0c2cf] [36m  S3 Storage (1.4ms) [0m[32mUploaded file to key: jgkjydcu78xqqwy2s5y7e9k626cy (checksum: Trn5DA27Wo04IEwrr83BqQ==)[0m
2023-05-29T08:17:01.343965+00:00 app[web.1]: I, [2023-05-29T08:17:01.343924 #2]  INFO -- : [37842d95-667f-4876-8513-abfbdab0c2cf] Completed 500 Internal Server Error in 20ms (ActiveRecord: 7.2ms | Allocations: 3187)
2023-05-29T08:17:01.344938+00:00 app[web.1]: F, [2023-05-29T08:17:01.344892 #2] FATAL -- : [37842d95-667f-4876-8513-abfbdab0c2cf]   
2023-05-29T08:17:01.344939+00:00 app[web.1]: [37842d95-667f-4876-8513-abfbdab0c2cf] Aws::Errors::MissingCredentialsError (unable to sign request without credentials set):
2023-05-29T08:17:01.344940+00:00 app[web.1]: [37842d95-667f-4876-8513-abfbdab0c2cf]   
2023-05-29T08:17:01.344940+00:00 app[web.1]: [37842d95-667f-4876-8513-abfbdab0c2cf] app/controllers/comments_controller.rb:7:in `create'
2023-05-29T08:17:01.345247+00:00 heroku[router]: at=info method=POST path="/comments" host=bnb-imgbbs.herokuapp.com request_id=37842d95-667f-4876-8513-abfbdab0c2cf fwd="xxx" dyno=web.1 connect=0ms service=227ms status=500 bytes=1827 protocol=https

追記:自己解決しました

エラーの検索が足りなかったと反省しています。
上記エラー(key設定時)の「`dig': String does not have #dig method」をググった際に出た
https://qiita.com/kenz-dev/items/641ea1de1a4499817f10
の記事で、このエラーが出た方はcredentialsの記載ミスだったようで、原因は違う(というかもっと初歩的なやつ)のですが、本来

aws:
  access_key_id: hoge
  secret_access_key: hoge

と書かないとだめなところを

aws:
  access_key_id:hoge
  secret_access_key:hoge

こう書いており(コロンのあとのスペース無し)、digメソッドが動かず・・というところで引っかかっておりました。

もし追記前で対処法を調べてくださった方がいらっしゃいましたら、本当に申し訳ないです。

0

No Answers yet.

Your answer might help someone💌