title: Get result from Amazon Transcribe directly (serverless)
body: <p>I use serverless Lambda services to transcribe from speech to text with Amazon Transcribe. My current scripts are able to transcribe file from S3 and store the result as a JSON file also in S3.</p>
  
  <p>Is there a possibility to get the result directly, because I want to store it in a database (PostgreSQL in AWS RDS)?</p>
  
  <p>Thank you for your hints</p>
  
  <p><strong>serverless.yml</strong></p>
  
  <pre><code>...
  provider:
    name: aws
    runtime: nodejs10.x
    region: eu-central-1
    memorySize: 128
    timeout: 30
    environment:
      S3_AUDIO_BUCKET: ${self:service}-${opt:stage, self:provider.stage}-records
      S3_TRANSCRIPTION_BUCKET: ${self:service}-${opt:stage, self:provider.stage}-transcriptions
      LANGUAGE_CODE: de-DE
    iamRoleStatements:
      - Effect: Allow
        Action:
          - s3:PutObject
          - s3:GetObject
        Resource:
          - 'arn:aws:s3:::${self:provider.environment.S3_AUDIO_BUCKET}/*'
          - 'arn:aws:s3:::${self:provider.environment.S3_TRANSCRIPTION_BUCKET}/*'
      - Effect: Allow
        Action:
          - transcribe:StartTranscriptionJob
        Resource: '*'
  
  functions:
  
    transcribe:
      handler: handler.transcribe
      events:
        - s3:
            bucket: ${self:provider.environment.S3_AUDIO_BUCKET}
            event: s3:ObjectCreated:*
  
    createTextinput:
      handler: handler.createTextinput
      events:
        - http:
            path: textinputs
            method: post
            cors: true
  ...
  
  resources:
    Resources:
      S3TranscriptionBucket:
        Type: 'AWS::S3::Bucket'
        Properties:
          BucketName: ${self:provider.environment.S3_TRANSCRIPTION_BUCKET}  
  ...
  </code></pre>
  
  <p><strong>handler.js</strong></p>
  
  <pre><code>const db = require('./db_connect');
  
  const awsSdk = require('aws-sdk');
  
  const transcribeService = new awsSdk.TranscribeService();
  
  module.exports.transcribe = (event, context, callback) =&gt; {
    const records = event.Records;
  
    const transcribingPromises = records.map((record) =&gt; {
      const recordUrl = [
        'https://s3.amazonaws.com',
        process.env.S3_AUDIO_BUCKET,
        record.s3.object.key,
      ].join('/');
  
      // create random filename to avoid conflicts in amazon transcribe jobs
  
      function makeid(length) {
         var result           = '';
         var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
         var charactersLength = characters.length;
         for ( var i = 0; i &lt; length; i++ ) {
            result += characters.charAt(Math.floor(Math.random() * charactersLength));
         }
         return result;
      }
  
      const TranscriptionJobName = makeid(7);
  
      return transcribeService.startTranscriptionJob({
        LanguageCode: process.env.LANGUAGE_CODE,
        Media: { MediaFileUri: recordUrl },
        MediaFormat: 'wav',
        TranscriptionJobName,
        //MediaSampleRateHertz: 8000, // normally 8000 if you are using wav file
        OutputBucketName: process.env.S3_TRANSCRIPTION_BUCKET,
      }).promise();
    });
  
    Promise.all(transcribingPromises)
      .then(() =&gt; {
        callback(null, { message: 'Start transcription job successfully' });
      })
      .catch(err =&gt; callback(err, { message: 'Error start transcription job' }));
  };
  
  module.exports.createTextinput = (event, context, callback) =&gt; {
    context.callbackWaitsForEmptyEventLoop = false;
    const data = JSON.parse(event.body);
    db.insert('textinputs', data)
      .then(res =&gt; {
        callback(null,{
          statusCode: 200,
          body: "Textinput Created! id: " + res
        })
      })
      .catch(e =&gt; {
        callback(null,{
          statusCode: e.statusCode || 500,
          body: "Could not create a Textinput " + e
        })
      }) 
  };
  </code></pre>