Murti
Murti

Reputation: 51

AWS Lambda Error - "errorType": "string", "errorMessage": "handled"

I am trying to write my first lambda function and use my MySQL database. The credentials to the database are correct and both are in the same subnet. The purpose of the function is to show all tables. Here my Node.js 10.x script:

'use strict';
console.log("Loading controls function");

const AWS = require('aws-sdk');
const mysql = require('mysql');

exports.handler = (event, context) => {
    // context.callbackWaitsForEmptyEventLoop = false; 
    
    const connection = mysql.createConnection({
        host: process.env.RDS_HOST,
        user: process.env.RDS_USER,
        password: process.env.RDS_PASSWORD,
        database: process.env.RDS_DATABASE
    });
    
    connection.query('select * from controls;', function (error, results, fields) {
      if (error){
        context.fail();  
        return { statusCode: 500, body: JSON.stringify(error) }
      } 
      else{
        context.succeed('Success');  
        return { statusCode: 200, body: JSON.stringify(results) }
      } 
    });
};

The error looks like this:

{ "errorType": "string", "errorMessage": "handled", "trace": [] }

Does anyone could imagine what could be the issue?

Upvotes: 4

Views: 3214

Answers (2)

Daniele Ricci
Daniele Ricci

Reputation: 15797

AFAIK a lambda handler should either be an async function or accept a callback. Given your code it is simpler to make it accept a callback.

'use strict';
console.log("Loading controls function");

const AWS = require('aws-sdk');
const mysql = require('mysql');

exports.handler = (event, context, callback) => {
  const connection = mysql.createConnection({
    host: process.env.RDS_HOST,
    user: process.env.RDS_USER,
    password: process.env.RDS_PASSWORD,
    database: process.env.RDS_DATABASE
  });
    
  connection.query('select * from controls;', function (error, results, fields) {
    if(error) {
      context.fail();
      return callback(null, { statusCode: 500, body: JSON.stringify(error) });
    } 

    context.succeed('Success');  
    callback(null, { statusCode: 200, body: JSON.stringify(results) });
  });
};

Upvotes: 1

Derrops
Derrops

Reputation: 8107

I am going to take a while guess that you have not configured the VPC settings for your Lambda function. You see, by default, Lambda doesn't actually get an IP as far as your concerned, or a security group. So it cannot communicate to anything in your VPC by default really.

Go to the Lambda, go to the VPC settings, and make sure you configure the Lambda to run in Subnets in your VPC, and give it a security group which will enable it to access your database.

Upvotes: 0

Related Questions