4
votes

What are some possible reasons why the following is not working for me?

  1. Create a DynamoDB table card_auth_events with at least an id field.
  2. Create a trigger that calls a lambda. Here is the lambda:
 console.log('Loading publishCardAuthEvent...');

 var QUEUE_URL = 'https://sqs.us-west2.amazonaws.com/XXXXXXXXXXXX/CardAuthEvents';
 var AWS = require('aws-sdk');
 var sqs = new AWS.SQS({region : 'us-west-2'});
 var http = require('https');

 exports.handler = function(event, context) {
 event.Records.forEach(function(record) {
      console.log(record.eventID);
      console.log(record.eventName);
      console.log('DynamoDB Record: %j', record.dynamodb);
      sendToQueue(record);
 });
 context.succeed("Successfully processed " + event.Records.length + " records.");
};

function sendToQueue(message){
//Send SQS message with details of file uploaded to S3.
var params = {
  MessageBody: JSON.stringify(event),
  QueueUrl: QUEUE_URL
};

sqs.sendMessage(params, function(err,data){
  if (err) {
    console.log('error:',"Fail Send Message" + err);
    context.done('error', "ERROR Put SQS");  // ERROR with message

    } else {
      console.log('data:',data.MessageId);
      context.done(null,'');  // SUCCESS
    }
  });
}
  1. Configure the Lambda to have a role with AmazonSQSFullAccess, AmazonDynamoDBFullAccess, and AmazonAppStreamFullAccess.

  2. Create the Queue in SQS

  3. Insert a record into the table.

The trigger is getting called, but why is the message not put in the Queue???

There are no messages in the queue and no errors in Cloud Watch. Is there some secret I am missing?

2

2 Answers

5
votes

I think the problem is that you are calling your sendToQueue() function, and then calling context.succeed() without waiting for the resulting SQS API call to complete. This is terminating your Lambda function before it is finished.

Once you get past that, another problem is that you are calling context.done() inside the SQS callback, but you are potentially calling sendToQueue() multiple times. As soon as the first SQS API call returns your Lambda function is going to terminate.

1
votes

The problem is your access policy. If you are using IAMFullAccess all you have done is given your lambda permission to access and change IAM policies, groups, users, etc. You need to give your lambda access to SQS. If you want to fully test lambda I suggest using the managed policy: AWSLambdaFullAccess and AmazonSQSFullAccess. That will open up S3, Lambda, DynamoDb, SQS and Cloudwatch.