Search code examples
ffmpegaws-lambdaserverless-frameworkfluent-ffmpeg

How to import fluent-ffmpeg in AWS lambda?


I'm trying to use fluent-ffmpeg in AWS Lambda, but cannot get it setup correctly. At the top of my index.js:

import ffmpeg from "fluent-ffmpeg";

But it is always undefined.

ffmpeg === undefined.

I'm using Serverless and have ffmpeg included as a layer.

serverless.yaml

functions:
  createGifFromVideo:
    handler: src/services/createGifFromVideo/index.handler
    layers:
      - { Ref: FfmpegLambdaLayer }
    events:
      - sns: arn:aws:sns:us-east-1:${self:custom.accountId}:NewVideoPostContentTopic-${self:provider.stage}

layers:
  ffmpeg:
    path: src/layers

package.json

{
  "name": "createGifFromVideo",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "private": true,
  "dependencies": {
    "fluent-ffmpeg": "^2.1.2"
  }
}

The uploaded lambda seems to be constructed correctly from what I can tell. Webpack builds the file with fluent-ffmpeg merged in and it is linked to the ffmpeg layer.

I can load other packages. It's just fluent-ffmpeg that comes back undefined.

From the docs it mentions passing FFMPEG_PATH and FFPROBE_PATH as environment variables. Are these necessary with a layer?

I would be grateful to see a configuration that works.


Solution

  • Problem turned out to be an issue with the environment variables. When using FFmpeg as a layer, assuming it is in a directory called ffmpeg, use these environment variables:

    FFMPEG_PATH: /opt/ffmpeg/ffmpeg
    FFPROBE_PATH: /opt/ffmpeg/ffprobe