brefphp / laravel-bridge

Package to use Laravel on AWS Lambda with Bref
https://bref.sh/docs/frameworks/laravel.html
MIT License
314 stars 63 forks source link

Upload to S3 failed - InvalidAccessKeyId #67

Closed aristidesneto-bnw closed 2 years ago

aristidesneto-bnw commented 2 years ago

Hi, I'm using an S3 bucket to save files from Laravel application uploads. When trying to upload the file, I get the following error:

[previous exception] [object] (GuzzleHttp\\Exception\\ClientException(code: 403): Client error: `PUT https://my-bucket.s3.amazonaws.com/1077c090.png` resulted in a `403 Forbidden ` response:
<Error><Code>InvalidAccessKeyId</Code><Message>The AWS Access Key Id you provided does not exist in our records.</Message><AWSAccessKeyId>ASIAZ7N74ORBT6RJ5AAA</AWSAccessKeyId><RequestId>KFZADDG3V6QX8AAA</RequestId><HostId >IH1lvnySucUhfJBX7k2HAAA=</HostId></Error> {"userId":2,"exception":"[object] (Aws\\S3\\Exception\\S3Exception(code: 0): Error executing \"PutObject\" on \"https://my-bucket.s3.amazonaws.com/1077c090.png\"; AWS HTTP error: Client error: `PUT https://my-bucket.s3.amazonaws.com/1077c090.png` resulted in a `403 Forbidden` response:

The problem in this case is that the second error says that the access key is invalid. I don't know where he gets this key from, it's not the key I inform in the .env file.

What is this key that it displays in the error: <AWSAccessKeyId>ASIAZ7N74ORBT6RJ5AAA</AWSAccessKeyId>? (not the original key that displays on panel, omitted characters).

Any idea what it could be? Thanks

mnapoli commented 2 years ago

On AWS Lambda you shouldn't set an API key explicitly, because the lambda function already has one (with permissions attached to it).

You can read https://www.serverless.com/framework/docs/providers/aws/guide/iam to learn more.

aristidesneto-bnw commented 2 years ago

The AWS API key I entered in the .env file was for access to the bucket I have already created. The .env file now looks like this, without the credentials:

AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=my-bucket

Even though I don't enter the credentials, I get the error I reported above.

The only AWS key I have is the configuration keys in ~/.aws/credentials.

As an attempt to solve it, I followed the documentation to create the bucket automatically by the lift plugin and I have the same error.

What I don't understand is the AWSAccessKeyId that is displayed in the log, I have no idea where it is.

My serverless.yml file looks like this:

iam:
         role:
             statements:
                 - Effect: Allow
                     Action: s3:*
                     Resource:
                         - 'arn:aws:s3:::my-bucket'
                         - 'arn:aws:s3:::my-bucket/*'

I do not know what else to do. I'll take a closer look at the link you gave me in the documentation.