3

I am trying to set up an AWS Lambda function that will query a MySQL database and upload the results to Geckoboard for analysis. However, it will always time out. Here's my code:

'use strict';

var API_KEY = [API KEY];
var gb = require('geckoboard')(API_KEY);
var AWS = require('aws-sdk');
var mysql = require('mysql');

var connection = mysql.createConnection({
    [DATABASE DETAILS]
});

var mysqlQuery = '    
    SELECT DATE(created_at) as date, COUNT(DATE(created_at)) as number_of_entries
    FROM table
    WHERE updated_at IS NOT NULL
    GROUP BY date
    ';

var schema = {
    id: 'geckoboard_target',
    fields: {
        date: {
            type: 'datetime',
            name: 'date'
        },
        number_of_entries: {
            type: 'number',
            name: 'number_of_entries'
        }
    }
};

function uploadToGeckoboard(schema, data, context) {
    gb.datasets.findOrCreate( schema,
        function (err, dataset) {
            if (err) {
                console.error('Error connecting to Geckoboard:',err);
                context.fail('Failed');
            }
                dataset.put(
                data,
                function (err) {
                    if (err) {
                        console.error('Error uploading to Geckoboard',err);
                        context.fail('Failed');
                    }
                    console.log('Dataset created and data added');
                    context.succeed('Success');
                }
            );
        }
    );
}

exports.handler = (event, context) => {

    connection.connect(function(err) {
        if (!err) {

            connection.query(mysqlQuery, function(err, data) {
                if (!err) {

                    console.log("Results:", JSON.stringify(data));
                    uploadToGeckoboard(schema, data, context);

                    connection.end();


                } else {
                    console.log("Query error:", err);
                    context.fail();
                }
            });
        } else {
            console.log("Error connecting database:", err.message);
            context.fail();
        }
    });
};

It succeeds up till the point the data returns and the uploadToGeckoboard function is called. After that, it just times out. I've tried the same code, with the lambda handler and context removed, and it runs from my local machine just fine.

Any help would be greatly appreciated!

Joseph Wolf
  • 123
  • 1
  • 1
  • 6

1 Answers1

0

As it turns out, the code is just fine. The problem was that, along the development process we had enabled a VPC in the lambda configuration - in the hopes of enabling what we thought were necessary RDS read abilities.

Turning it off solved the perpetual time-outs, and revealed that no VPC was needed to make RDS queries. Oops!

Joseph Wolf
  • 123
  • 1
  • 1
  • 6