1

I have been trying to get this action to work. i am able to send a message to SQS on my own when i use the inline editor. and i am able to use my uploaded code with the node-mysql to do a database insert however i can not get to two to work together. currently i get no output when running my code here is what i am running

console.log('Loading function');
var mysql = require('mysql');
var AWS = require('aws-sdk');
var s3 = new AWS.S3({ apiVersion: '2006-03-01' });
var QUEUE_URL = 'https://sqs.us-east-1.amazonaws.com/1234/dev-upload-test';
var sqs = new AWS.SQS({region:'eu-east-1'}); 


exports.handler = function (event, context) {
  var srcBucket = event.Records[0].s3.bucket.name;
  var srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));

  var company = 123;
  var user = 321;
  var connection = mysql.createConnection({
    host     : '10.0.1.22', //lamdba has access to this VPC
    user     : 'username',
    password : 'password',
    database : 'development',
  });
  connection.connect(function(err) {
    if(err){
      console.log('Error connecting to Db');
      return;
    }
    console.log('Connection established');
  });
  var query = connection.query('INSERT INTO messages (created_at, processing) VALUES (now(),1)', function(err,results){
    if (err) throw err;
    console.log(results.insertId);
    console.log('trying to send message');

    var msg = { payload: srcBucket,srcKey };
    var sqsParams = {
      MessageBody: JSON.stringify(msg),
      QueueUrl: 'https://sqs.us-east-1.amazonaws.com/1234/dev-upload-test'
    };
    console.log(sqsParams)

    var sqsdata = sqs.sendMessage(sqsParams, function(err, data) {
      if (err) {
        console.log('ERR', err);
      }
      console.log(data);
    });
    console.log('message sent')
  });
  context.succeed('Exit');
};

1 Answer 1

3

You need to put context.succeed('Exit'); inside the sqs.sendMessage callback. Right now your Lambda function is queuing up a bunch of asynchronous work, but then you exit synchronously at the end of the handler function before any of it has finished.

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

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.