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?

More than 3 years have passed since last update.

Lambda上でLaravelを動かしてs3にアクセスしようとしたが出来ない時のメモ

Last updated at Posted at 2021-04-26

環境

AWS Lambda
bref
Laravel
thephpleague/flysystem-aws-s3-v3

結論

公式ドキュメントは良く読むこと。
https://bref.sh/docs/frameworks/laravel.html#file-storage-on-s3

解説

Lambdaは関数実行時に実行ロールから以下の情報を環境変数に設定する。
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_SESSION_TOKEN
上の情報を使用しないとs3にアクセス出来ない。

デフォルトのLaravelだと上2つはconfigで渡してくれるが、
AWS_SESSION_TOKENは渡してくれないので下のように追記する必要がある。

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
+           'token' => env('AWS_SESSION_TOKEN'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
        ],

Lambdaが勝手に設定してくれるので.envにAWS_ACCESS_KEY_ID、AWS_SECRET_ACCESS_KEY、AWS_SESSION_TOKENを追記する必要は無い。

参考

https://qiita.com/kenji0302/items/fec0c3fd79ceeabaf5fd
https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime
https://github.com/laravel/laravel/pull/5138

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?