AWS Lambda permission denied when trying to use ffmpeg

11,781

Solution 1

AWS Lambda runs on Amazon Linux. It is a known issue. Try building (with static enabled) and check if it works on Amazon Linux and upload that binary. You do not have the privileges to chmod the files in /var/task/. Or try this solution that works:

  • Move ffmpeg to /tmp
  • chmod 755 /tmp/ffmpeg
  • Call /tmp/ffmpeg

See this discussion for more info.

Solution 2

I ran into this issue recently, and after messing with various manual solutions, what really solved the issue was:

  1. Create a Lambda Layer, with only the ffmpeg binary inside a bin/ folder
  2. Create a Lambda Function to implement said layer, and in the python code run /opt/bin/ffmpeg

See https://aws.amazon.com/blogs/media/processing-user-generated-content-using-aws-lambda-and-ffmpeg/

Share:
11,781
moarCoffee
Author by

moarCoffee

Updated on July 24, 2022

Comments

  • moarCoffee
    moarCoffee almost 2 years

    I want to write a handler that responds to S3 put events to convert any avi files that are uploaded to mp4. I doing it in Java, in Eclipse, with the AWS toolkit plugin. For video conversion, I am using ffmpeg with ffmpeg-cli-wrapper, and I have provided a static (linux) binary of ffmpeg in the source tree.

    I have found that when I upload the function, the binary gets put in /var/task, but when I try to use the test function I've written, I get a "permission denied" error.

    import net.bramp.ffmpeg.FFmpeg;
    
    public class LambdaFunctionHandler implements RequestHandler<S3Event, String> {
    
        private static final String FFMPEG = "/var/task/ffmpeg";
    
        public String handleRequest(S3Event event, Context context) {
    
            try {
                FFmpeg ff = new FFmpeg(FFMPEG);
                System.out.println(ff.version());
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return "foo";
        }
    }
    

    And the first line of the stacktrace: java.io.IOException: Cannot run program "/var/task/ffmpeg": error=13, Permission denied.

    How do I execute this binary? I have done as others have suggested and chmod 755 the binary before uploading, but it hasn't made a difference.

  • moarCoffee
    moarCoffee about 8 years
    How do I move the file? Is it something I can do once from the command line or do I need to do this in the code at the start of the method?
  • helloV
    helloV about 8 years
    At the start of the method. If mv fails, try cp. It always worked for me.
  • moarCoffee
    moarCoffee about 8 years
    Sorry for not getting back to you earlier. I used ProcessBuilder to do as you said and it all works nicely. Thanks for the help!
  • voho
    voho over 7 years
    you can also use Files.copy in Java