0

I created aws lambda function based on laravel framework using bref. Link: https://bref.sh

this is my code to download using media library spartie

$mediaItem = Media::where('model_id',$id)->where('model_type','App\MODEL')->first();

return $mediaItem;

My configuration is

AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXXXXXX
AWS_SECRET_ACCESS_KEY=XXXXX+XXXXXXXX
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=my-bucket

When I download file locally all work perfectly, but when I try to download on s3 in lambda function I got error:

{ "message" : "Internal server error" }

I use this package aws:

league/flysystem-aws-s3-v3 ~1.0

my serverless.yaml

service: serverless-api

provider:
    name: aws
    region: eu-central-1
    runtime: provided
    environment:
        APP_ENV: production
    iamRoleStatements:
      - Effect: Allow
        Action: 
          - s3:*
        Resource: 'arn:aws:s3:::bucket/*'       

plugins:
    - ./vendor/bref/bref

package:
  exclude:
    - node_modules/**
    - public/storage
    - storage/**
    - tests/**
    - .env
 functions:
    website:
        handler: public/index.php
        timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)
        layers:
            - arn:aws:lambda:eu-central-1:209497400698:layer:php-72-fpm:23
        events:
            -   http: 'ANY /'
            -   http: 'ANY /{proxy+}'
    artisan:
        handler: artisan
        timeout: 120 # in seconds
        layers:
            - arn:aws:lambda:eu-central-1:209497400698:layer:php-72:22
            - arn:aws:lambda:eu-central-1:209497400698:layer:console:22

also i assign role for s3 , But I have no idea how it's the problem .

thank you .

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • Did you find a solution for this? Cause I'm having the same problem. All the normal solutions don't seem to work. I ended up just creating a donwload link, which was exactly what I was trying to prevent. My server setup is also serverless, but by Vapor. – Matt May 05 '20 at 14:01
  • 1
    hi matt , Finally I've found the solution , I am running the function lambda in a vpc , but the vpc does not have permission to access the s3 service so I followed this guide. https://aws.amazon.com/es/blogs/aws/new-vpc-endpoint-for-amazon-s3/ and I ran into another error , By default API Gateway does not support binary HTTP requests or responses like images, PDF, binary files . so I my case i changed the serverless.yml like the documentation of bref apiGateway: binaryMediaTypes: - '*/*' environment: BREF_BINARY_RESPONSES: 1 and now it works perfectly . – Alejandro Gonzalez May 06 '20 at 15:25

0 Answers0