0

What is the callback doing in lambda handler? It seems to be using sns variable and I intend to modify the variables.

exports.handler = function(event, context, callback) {
   console.log("AWS lambda and SNS trigger ");
   console.log(event);
   const sns = event.Records[0].Sns.Message;
   console.log(sns)
   callback(null, sns);   
};

I'm trying to modify the code to something like this and not sure what I should pass for callback. I also want to trigger another lambda and pass these new variables to it.

exports.handler = function(event, context, callback) {
   console.log("AWS lambda and SNS trigger ");
   console.log(event);
   const sns_NameSpace = event.Records[0].Sns.Message.Trigger.Namespace;
   const sns_ApiId = event.Records[0].Sns.Message.Trigger.Dimensions.value;
   const sns_MetricName = event.Records[0].Sns.Message.Trigger.MetricName;
   console.log(sns_ApiId + '_' + sns_MetricName)
   console.log(sns_NameSpace)   
   
   callback(null, sns); // Wondering what I should pass for the callback.
   
};
user630702
  • 2,529
  • 5
  • 35
  • 98

1 Answers1

1

If you have configured SNS -> Lambda trigger, you don't need to do anything to the callback. SNS sends a message to a subscriber (in this case a Lambda function) exactly once and does not wait for a response from the subscriber.

And what exactly do you mean by 'another lambda'?

Day Break
  • 88
  • 10
  • I have another lamdba called zabbixPy. I want to call zabbixPy lambda from this lambda and while calling I want to pass these variables as event or something else so the variables are processed by zabbixPy lambda function. – user630702 Nov 01 '21 at 12:13
  • can you please also answer what's the use of the callback here? You're only saying that nothing needs to be done and its not needed if we have configured SNS -> Lambda trigger – user630702 Nov 01 '21 at 12:18
  • OK. I understand that you want to configure something like SNS -> Trigger Lambda -> zabbixPy Lambda (which receives Trigger Lambda variable). If so, the easiest way is to call zabbixPy Lambda using aws-sdk from Trigger Lambda. This allows you to send the desired value as a payload to zabbixPy Lambda. Please refer to the following link for how to call another Lambda function using nodejs aws-sdk. https://stackoverflow.com/questions/69679359/how-to-invoke-a-lambda-function-in-aws-sdk-v3 – Day Break Nov 01 '21 at 12:28
  • If you need to synchronously wait for a response from zabbixPy Lambda in Trigger Lambda, specify the RequestResponse value in the InvocationType parameter of the invoke function. If you want an asynchronous call, specify an Event value. If you want to know more about the invoke function, please refer to the following link. https://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html – Day Break Nov 01 '21 at 12:28
  • For the Lambda nodejs runtime, callbacks are used to end the execution of a function and send a response. In the case of SNS -> Lambda trigger, the response sent by the Lambda function's callback is meaningless because SNS does not wait for a response after sending a message to the subscriber. – Day Break Nov 01 '21 at 12:37