1

So i've been looking into the Promises and Waiters, and i was wondering if there is a way to start a Transcription Job, while providing a callback, so it would tick (periodically check by itself) until the result of the Transcription would be COMPLETED, and then use the callback to get the json with transcriptions and write the result to db. So all i would have to do is start the job and provide the callback, and the waiter would periodically block the thread and check the status, allowing me to throw other requests in between, instead of doing all this with while loops.

I tried the example provided here, but it just uses the wait() method and blocks the thread anyway until it gets the result.

Is it even possible to do with the Transcribe service? A small code example of how to do it would be oh so much appreciated!

ChungaChanga
  • 103
  • 6

1 Answers1

2

I achieved a callback response from AWS Transcribe by doing the following:

I create a Transcribe Job using the PHP SDK:

use Aws\TranscribeService\TranscribeServiceClient;
...

$transcriber = new TranscribeServiceClient([<config>]);
$job = $transcriber->startTranscriptionJob([<config>, 'TranscriptionJobName' => 'unique-job-name']);

Next I log into the AWS console and go to AWS Lambda. Inside Lambda I have created a function using the Node.js 8.10 runtime:

var https = require('https');

exports.handler = function(event, context) {

    var body='';

    // the post options
    var optionspost = {
        host: 'example.com',
        path: '/transcriptions/callback',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        }
    };

    var reqPost = https.request(optionspost, function(res) {
        res.on('data', function (chunk) {
            body += chunk;
        });

        context.succeed(body);
   });

   reqPost.write(JSON.stringify(event));
   reqPost.end();
};

This sends a POST request to https://<host><path> with the event data as the body

Next go into AWS Cloudwatch and create a rule Events->rules. Under Event Source - Service Name select Transcribe and configure your options (Event Type -> Transcribe Job State Change, Specific Status -> Completed). Under Targets select Lambda Function and then select your function

This will trigger a call to your Lambda Function when the Transcribe Job has completed. The Lambda Function posts to your server with the details of the Transcribe Job, including Job Name: unique-job-name.

At this point you can go back into Cloudwatch and:

  • select your Rule and click show metrics for this rule.
  • select Logs to see any logging information from your Lambda Function
Shawn Northrop
  • 5,826
  • 6
  • 42
  • 80
  • can u more clearly discuss the lambda function with their portal pics?. i pasted ur function into function and saved it. but i couldn't post data to my server. – muneeb_ahmed Aug 22 '20 at 01:20