Lambda Coding node.JS尝试向SQS发送消息

时间:2016-03-10 14:28:20

标签: node.js amazon-s3 lambda

我一直试图让这项行动奏效。当我使用内联编辑器时,我可以自己向SQS发送消息。我能够使用我上传的代码与node-mysql进行数据库插入但是我不能让两个人一起工作。目前我在运行我的代码时没有输出是我正在运行的

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 个答案:

答案 0 :(得分:0)

您需要将context.succeed('Exit');置于sqs.sendMessage回调中。现在你的Lambda函数正在排队一堆异步工作,但是在处理函数结束之前你会在它完成任务之前同步退出。