2

is any one able successfully connect to Redshift from lambda.

I want to fetch some records from Redshift table and feed to my bot (aws lex)

Please suggest - this code is working outside lambda how to make it work inside lambda.

import psycopg2

con=psycopg2.connect(dbname= 'qa', host='name',
port= '5439', user= 'dwuser', password= '1234567')

cur = con.cursor()

cur.execute("SELECT * FROM pk.fact  limit 4;")

for result in cur:
    print (result)
cur.close()
con.close()

2 Answers 2

2

Here is the node lambda that works to connecting to Redshift and pulling data from it.

exports.handler = function(event, context, callback) {
    var response = {
        status: "SUCCESS",
        errors: [],
        response: {},
        verbose: {}
    };

    var client = new pg.Client(connectionString);
    client.connect(function(err) {
        if (err) {
            callback('Could not connect to RedShift ' + JSON.stringify(err));
        } else {
            client.query(sql.Sql, function(err, result) {
                client.end();
                if (err) {
                    callback('Error Cleaning up Redshift' + err);
                } else {
                    callback(null, ' Good ' + JSON.stringify(result));
                }
            });
        }
    });
};

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to fetch the records first.

results = cur.fetchall()
for result in results:
    ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.