Using EC2 metadata credentials in the Laravel filesystem

Viewed 985

How do I tell the Laravel filesystem layer to use the s3 metadata on an EC2 instance? I don't want to provide hardcoded keys and secrets for my s3 buckets. I'm unclear on what the configuration should look like. When I exclude the key and secret from the filesystem configuration I get the following error

ErrorException
Undefined index: key
2 Answers

The fix is to leave empty placeholder values in place for key and secret. eg, in config/filesystems.php

return [
    'cloud' => 's3',    
    'disks' => [
         's3' => [
             'driver' => 's3',
             'key' => '',
             'secret' => '',
             'region' => env('S3_REGION'),
             'bucket' => env('S3_BUCKET'),
         ],
    ],
];

The correct way to provide your credentials is by using the .env file.

In your .env file, add something like that:

EC2_SECRET=your_ec2_secret
EC3_KEY=your_ec2_key

and in the `` config file, use something like that:

'ec2' => [
  ...
  'key' => env('EC2_SECRET'),
  'secret' => env('EC3_KEY'),
],

You should now be able to use the service without having the credentials stored in the repository.

Related