Kevin Meredith
Kevin Meredith

Reputation: 41939

Trying to Connect to Redshift Over AWS Lambda

I'm using the node-postgres client to my AWS Redshift database.

Locally, I'm able to run the following code in node, getting print statements for ">> connected" and ">>> successful query. jsonResult: ".

However, when I run this code in Amazon Lambda, I don't see any log statements besides "trying to connect...".

console.log("trying to connect...");
var r = pg.connect(conString, function(err, client) { 
  if(err) {
    return console.log('>> could not connect to redshift', err);
  }
  console.log(">> connected");
  client.query('SELECT * FROM my_table', function(err, result) {
    if(err) {
      return console.log('error running query', err);
    }
    var jsonResult = JSON.stringify( result );
    console.log(">>> successful query. jsonResult: " +  jsonResult);
    client.end();
    return jsonResult;
  });
});

I'm confused as to how no print statement, besides ">> trying to connect...," could show up in this code.

Upvotes: 5

Views: 10869

Answers (2)

ralfe
ralfe

Reputation: 1454

Is it possible that you didn't attach the Lambda to the VPC? If this is the case, I suggest attaching it to two private subnets, and ensure that there is a NAT Gateway available for those subnets. You an find more information in this tutorial: https://github.com/awslabs/aws-lambda-redshift-loader

Upvotes: 0

Theo
Theo

Reputation: 132942

There are two things you should check

  1. Make sure that your Lambda function doesn't return early, the snipplet you posted doesn't contain any call to context.done() so it's hard to know if you're handling this correctly. Are you calling context.done() somewhere else?
  2. Is your Redshift cluster open to the internet? When you connect to your Redshift cluster manually do you do it from an EC2 instance, or over the internet? If it's the former your Lambda function can probably not connect to the cluster because the cluster is not addressable (but it is from your EC2 instance because you've configured the cluster's security group to allow the instance's security group to connect). You can read more about how to configure access to your cluster here.

Upvotes: 3

Related Questions