1

I'm working on Nodejs Lambda function. It is to fetch remote video file to upload to s3. While it works on the small size file, but large size case fails upon the time limitation of API gateway (29 seconds).

Are there ways that I receive api call back early to the request while the code is running on lambda?

I wrapped the function with Async but it takes same time. Probaly I was wrong at setting for the asynchronous job in Nodejs.

The below is the code.


'use strict';

const fetch = require('node-fetch');
const AWS = require('aws-sdk'); // eslint-disable-line import/no-extraneous-dependencies

const s3 = new AWS.S3();

module.exports.save = (event, context, callback) => {

  const url = "some_url";
  const Bucket = "recorded-video";
  const key = "some_key.mp4"

  fetch(url)
  .then((response) => {
    if (response.ok) {
      return response;
    }
    return Promise.reject(new Error(
          `Failed to fetch ${response.url}: ${response.status} ${response.statusText}`));
  })
  .then(response => response.buffer())
  .then(buffer => (
    {
      s3.putObject({
        Bucket: process.env.BUCKET,
        Key: key,
        Body: buffer,
      }).promise();   

      // then give permission.
    }
  ))

};


Sungpah Lee
  • 1,003
  • 1
  • 13
  • 31

1 Answers1

1

I don't think so, but I can think of few ways to do this,

  1. You can make another lambda to do the copying and invoke that lambda asynchronously from the current lambda. That will give you 15 minutes (lambdas can run upto 15 minutes).
  2. You can setup a docker task and setup AWS batch. Then submit a batch job from your lambda. There is no time limit in this method.
Arun Kamalanathan
  • 8,107
  • 4
  • 23
  • 39
  • Here is how you can run the code asynchronously. https://stackoverflow.com/questions/39126606/invoke-aws-lambda-from-another-lambda-asynchronously – Arun Kamalanathan Dec 18 '19 at 07:39
  • for 1, but still the first lambda has to be triggered with Gateway, which means it needs to wait the second lambda gives the result. – Sungpah Lee Dec 18 '19 at 07:53
  • 1
    No You can run it asynchronously. Which means , you don't wait for that to finish. – Arun Kamalanathan Dec 18 '19 at 08:23
  • 1
    I got your point. I implemented in the way you instructed me. In the first lambda, I invoked the second one as a function asynchronously. It worked out. Thanks (:! – Sungpah Lee Dec 18 '19 at 13:05