skip to Main Content

I’m using the snowflake node driver to connect to a DB. When running the connector from a local server I have no issues. However, when I try the same function running in lambda I can’t seem to connect. There are no errors, exceptions, or timeouts… just nothing. Here is the code I’m using per their documentation.

var snowflake = require("snowflake-sdk");
var connection = snowflake.createConnection({
  account: "****",
  username: "******",
  password: "******",
});

connect(connection);

const response = {
  statusCode: 200,
  body: JSON.stringify("Hello from Lambda!"),
};
return response;

function connect(connection) {
  console.log("in connection");
  let connection_ID;
  try {
    connection.connect(function (err, conn) {
      if (err) {
        console.error("Unable to connect: " + err);
      } else {
        console.log("Successfully connected to Snowflake");
        // Optional: store the connection ID.
        connection_ID = conn.getId();
      }
      console.log(connection_ID);
    });
  } catch (err) {
    console.log(err);
  }
}

For clarity, my lambda has no issues connecting to other API’s, and is not running behind a VPC.
Any help would be greatly appreciated.

2

Answers


  1. If you have not selected any VPC for your lambda function, it will use the default VPC of the region.
    Can you select a VPC, which has access to the snowflake public endpoints and check.
    If still an issue, please post the Cloud watch logs, it should give a clue.
    You can also check on snowflake History page, if you get any Client-side connection request from the lambda or not.

    Login or Signup to reply.
  2. I ran into this same issue. This behavior occurs due to how the SDK is written, it is actually using promises and asynchronous calls. This is not intuitive from the documentation, which makes the code look like it was written to behave synchronously.

    I stumbled across this post David Szmolka while investigating the issue- https://github.com/snowflakedb/snowflake-connector-nodejs/issues/464#issuecomment-1495923219

    It has great reference code on how to show how to chain the promises together to arrive a synchronous behavior. The connectToSnoflake usage below comes from it.

    var snowflake = require("snowflake-sdk");
    var connection = snowflake.createConnection({
      account: "****",
      username: "******",
      password: "******",
    });
    
    
    await connectToSnoflake(connection)
            .then( (conn) => {
              let connection_ID = conn.getId();;
              console.log('connection_ID ', connection_ID)
            } );
    
    const response = {
      statusCode: 200,
      body: JSON.stringify("Hello from Lambda!"),
    };
    return response;
    
    
    async function connectToSnoflake(connection) {
      return new Promise( (resolve, reject) => {
          connection.connect( (err, conn) => {
            try {
              if (err) {
                  console.error('Unable to connect: ' + err.message);
                  reject(err)
              }
              console.log('Successfully connected to Snowflake.');
              resolve(conn)
            }catch (err) {
              console.log(err);
            }
          })
      })
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search