src/index.js
/**
 * Actual logic for processing a CloudWatch event. Unzips the event payload, translates it to a format appropriate
 * for Scalyr, then POSTs that to the Scalyr addEvents API.
 *
 * @param event
 * @param context
 * @param callback
 */
Actual logic for processing a CloudWatch event. Unzips the event payload, translates it to a format appropriate
for Scalyr, then POSTs that to the Scalyr addEvents API.


Actual logic for processing a CloudWatch event . Unzips the event payload translates it to a format appropriate for Scalyr then POSTs that to the Scalyr addEvents API .
function processEvent(event, context, callback) {
  const payload = new Buffer(event.awslogs.data, 'base64');
  zlib.gunzip(payload, (err, res) => {
    if (err) return callback(err);
    const cloudWatchMessage = JSON.parse(res.toString('utf8'));
    if (cloudWatchMessage.logEvents && cloudWatchMessage.logEvents.length) {
      request.post(transformToPost(cloudWatchMessage), (error, response, body) => {
        console.log('Response from Scalyr:', body);
        if (response && response.statusCode) {
          if (response.statusCode === 200) {
            const msg = `Successfully submitted ${cloudWatchMessage.logEvents.length} log events to Scalyr.`;
            console.log(msg);
            callback(null, msg);
          } else {
            let msg = `Received status code ${response.statusCode}`;
            if (error) msg += ` and error '${error}'`;
            msg += ' from Scalyr';
            console.log(msg);
            callback(null, msg);
          }
        }
      });
    }
  });
}
function processEvent ( event , context , callback ) { const payload = new Buffer ( event . awslogs . data , 'base64' ) ; zlib . gunzip ( payload , ( err , res ) => { if ( err ) return callback ( err ) ; const cloudWatchMessage = JSON . parse ( res . toString ( 'utf8' ) ) ; if ( cloudWatchMessage . logEvents && cloudWatchMessage . logEvents . length ) { request . post ( transformToPost ( cloudWatchMessage ) , ( error , response , body ) => { console . log ( 'Response from Scalyr:' , body ) ; if ( response && response . statusCode ) { if ( response . statusCode === 200 ) { const msg = ` ${ cloudWatchMessage . logEvents . length } ` ; console . log ( msg ) ; callback ( null , msg ) ; } else { let msg = ` ${ response . statusCode } ` ; if ( error ) msg += ` ${ error } ` ; msg += ' from Scalyr' ; console . log ( msg ) ; callback ( null , msg ) ; } } } ) ; } } ) ; }
Actual logic for processing a CloudWatch event.
Actual logic for processing a CloudWatch event .