0

I have a lambda which will get triggered when anyone uploads a audio file to the bucket. I need to process the file using the AWS Transcribe asynchronously. I wrote the code to do that but the problem is its check only once, it is not the calling the handler function after the proceesing of the file is completed.

Below is the stackoverflow link for AWS transcribe but we have to wait for thr response till the job is completed and lambda function has a timeout for 5 min. After that the execution will stop.

// to create a async client object to call AWS Transcribe

private AmazonTranscribeAsync asyncClient = AmazonTranscribeAsyncClientBuilder.standard().build();

// below is the method that will call the AWS API with the audio file uploaded

private void startText(String guid, String bucket) {
    String jobName = UUID.randomUUID().toString();
    StartTranscriptionJobRequest request = new StartTranscriptionJobRequest();
    request.withLanguageCode(LanguageCode.EnUS);

    Settings channel_settings = new Settings();
    channel_settings.setChannelIdentification(true);
    channel_settings.withChannelIdentification(true);

    Media media = new Media();
    media.setMediaFileUri(s3.getUrl(bucket, guid).toString());
    request.withMedia(media);
    request.setTranscriptionJobName(jobName);
    request.withMediaFormat(getFileFormat(guid));
    request.withSettings(channel_settings);
    asyncClient.startTranscriptionJobAsync(request, new AsyncTranscriptionJobHandler());
}

// async handler method

private class AsyncTranscriptionJobHandler implements AsyncHandler<StartTranscriptionJobRequest, StartTranscriptionJobResult>
{
    public void onError(Exception e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }

    @Override
    public void onSuccess(StartTranscriptionJobRequest request, StartTranscriptionJobResult result) {
        logger.log(result.getTranscriptionJob().getTranscriptionJobName());
        TranscriptionJob transcriptionJob = result.getTranscriptionJob(); 
        if (transcriptionJob.getTranscriptionJobStatus().equals(TranscriptionJobStatus.COMPLETED.name())) {
            logger.log("completed");
        } else if(transcriptionJob.getTranscriptionJobStatus().equals(TranscriptionJobStatus.FAILED.name())) {
            logger.log("failed");
        } else if(transcriptionJob.getTranscriptionJobStatus().equals(TranscriptionJobStatus.IN_PROGRESS.name())) {
            logger.log("processing");
        }

    }
}
Viswanath
  • 3
  • 9

1 Answers1

1

The trick here is to not wait for transcribe to finish, but to invoke it in one lambda, and then trigger a separate lambda once the transcription has completed.

AWS Transcribe uses CloudWatch Events to notify when a job completes or fails (https://docs.aws.amazon.com/transcribe/latest/dg/cloud-watch-events.html) which are a supported event source for lambda (https://docs.aws.amazon.com/lambda/latest/dg/invoking-lambda-function.html#supported-event-source-cloudwatch-events)

thomasmichaelwallace
  • 7,926
  • 1
  • 27
  • 33
  • For creating rule in cloudwatch we can trigger a lambda and we can even give the alias or version to trigger. So, how does the cloud watch event knows which one to call when the job is completed – Viswanath Oct 31 '18 at 11:11
  • You should use two separate lambdas, the first lambda starts the transcribe job, then second lambda does whatever you have to do once it's completed. (Note that you can use `TranscriptionJobName` in the event to uniquely identify the transcription. – thomasmichaelwallace Oct 31 '18 at 11:18
  • When i am triggering a lambda from another lambda if the invoke request payload is more than 6MB it is throwing error as the lambda invoke request limit is 6MB. Any idea how can we overcome this scenario – Viswanath Oct 31 '18 at 13:19
  • If you have to send a payload larger than 6mb then you'll need to use S3 - upload the payload to s3 and invoke your lambda with a link for it to download. (e.g. https://sookocheff.com/post/api/uploading-large-payloads-through-api-gateway/) – thomasmichaelwallace Oct 31 '18 at 13:24