LoginSignup
1
1

More than 3 years have passed since last update.

rails on docker がAWS S3 で AccessDeniedが出ることのデバッグ

Last updated at Posted at 2020-03-27

今までVagrantで動かしていた、Railsをdockerに移したところ、何故かs3にホストしてある画像が取得出来なくなったのでその確認をしました。

1. dockerにaws-cliをインストールしてみる

  • ruby:2.6.5-slim-stretch イメージをベースにしていたことがありそもそもpythonが入っていなかったので色々とインストールしてみた
> docker-compose exec web bash
# curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
# python get-pip.py 
bash: python: command not found → インストールされていなかったので、apt install l-y pythonでまずpythonを導入
# apt install -y python
# python get-pip.py
# pip install awscli

2. s3の一覧を取得する

# aws s3 ls s3://[[バケットのURL]]

An error occurred (RequestTimeTooSkewed) when calling the ListObjectsV2 operation: The difference between the request ti
me and the current time is too large.

3. RequestTimeTooSkewed って?

https://qiita.com/sadakan5/items/4c0a394fd1d6be34efb2
を参照。

サーバーとAWSの時間がずれているとエラーになるとのことで date で時間を確認してみた

# date
Wed Mar 25 23:39:24 UTC 2020

ちなみに、EC2にホストしているサーバーで実行すると。。

# date
Thu Mar 26 23:11:39 UTC 2020

確かにずれています。。

ホストがずれているの?と見てみてもそんなことはないです。。
(ホストは日本時刻)

> date
2020年3月27日 8:12:11

システムを休止状態(Hybernate)にするとずれることがあると
https://docs.docker.com/docker-for-mac/troubleshoot/#known-issues

で、こうすればいいみたい

まずは、HostとGuestのタイムゾーンを合わせます。(タイムゾーンを無視して時間だけを同期してしまうので)

powershellだと
ホストの方をUTCにして再度時間を合わせてみる

> Get-Timezone
Id : Tokyo Standard Time 
...
> Set-Timezone -Id "UTC"

その後、同期するコマンドを実行します

(ホスト側で)
> docker run --rm --privileged alpine hwclock -s
> docker-compose exec web bash
# date
Fri Mar 27 08:28:29 UTC 2020

でなおった!

これ、順番逆にすると時計がずれたままで、直らないので要注意

ホスト側のタイムゾーンをTokyoに戻します

> Get-Timezone
Id : 
...
> Set-Timezone -Id "Tokyo Standard Time"
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