0
votes

This is the code I have for publishing to the topic (I have changed the target and topic arn for security reasons):

var AWS = require("aws-sdk");
var sns = new AWS.SNS();
var targetArn = 'arn:aws:sns:us-east-1:4363657289:endpoint/GCM/APP_NAME/3185sfdnfe283925sgSeaa0e';
var topicArn = 'arn:aws:s-s:us-east-1:4363657289436:TOPIC_NAME';
var payload = {
    GCM: {
        data: {
            message: "test"
        }
    }
};
payload.GCM = JSON.stringify(payload.GCM);
payload = JSON.stringify(payload);
var params= { 
    TopicArn: topicArn,
    TargetArn: targetArn,
    Message: payload,
    MessageStructure: 'json'
};
var responsefromSNS = sns.publish(params , function(error, data) {
    if (error) {
        console.log("ERROR: " + error.stack);
    }
    else {
        console.log("SENT DATA: " + JSON.stringify(data));
        context.done(null, data);
    }
});
console.log(responsefromSNS);

My issue is that I never see log statements from either the if or else block and the push notification never reaches the mobile app. I have consulted both the AWS JavaScript SDK Documentation and countless stack overflow posts about this and nothing that I have tried works. And, I have given the lambda function permission to publish to the topic.

---UPDATE----- I have changed my code a bit and now it looks like this:

var AWS = require("aws-sdk");
AWS.config.update({region:'us-east-1'});
var topicarn = 'arn:aws:s-s:us-east-1:927579412028:alexapushdemo';
var targetarn = 'arn:aws:sns:us-east-1:927579412028:endpoint/GCM/automation.home.visa.com.homeautomation/3af761b2-1955-34d8-b66a-85e232e0aa0e'; 
var payload = {
    default: "test",
    GCM: {
        data: {
            message: "test"
        }
    }
};
payload.GCM = JSON.stringify(payload.GCM);
payload = JSON.stringify(payload);
var sns = new AWS.SNS();
console.log('start of sns function')
sns.publish({
    TargetArn: targetarn,
    Message: payload,
    MessageStructure: 'json'
}, function(err, data) {
    if (err) {
        console.log(err.stack);

        // Notify Lambda that we are finished, but with errors
        context.done(err, 'sns function finished with errors!');  
        return;
    }
    console.log('push sent');
    console.log(data);
    // Notify Lambda that we are finished 
    context.done(null, 'sns function finished!');  
});
console.log('end of sns functions');

The error I get is: ConfigError: Missing region in config\\n
at Request.VALIDATE_REGION (/node_modules/aws-sdk/lib/event_listeners.js:81:45)\\n
at Request.callListeners (/node_modules/aws-sdk/lib/sequential_executor.js:105:20)\\n
at callNextListener (/node_modules/aws-sdk/lib/sequential_executor.js:95:12)\\n
at /node_modules/aws-sdk/lib/event_listeners.js:75:9\\n
at finish (/node_modules/aws-sdk/lib/config.js:228:7)\\n
at /node_modules/aws-sdk/lib/config.js:268:9\\n
at resolveNext (/node_modules/aws-sdk/lib/credentials/credential_provider_chain.js:84:9)\\n
at /node_modules/aws-sdk/lib/credentials/credential_provider_chain.js:97:11\\n
at /node_modules/aws-sdk/lib/credentials.js:123:23\\n
at /node_modules/aws-sdk/lib/credentials/ec2_metadata_credentials.js:66:7\\"\",\"ip\":\"127.0.0.1\"}",

Why am I getting this even though I'm calling AWS.config.update.

2
Is your lambda code wrapped in a exports.handler function call?ataylor
This code is actually inside a function, which I then call within a exports.handler call.iram
Make sure that you call AWS.config.update({region:'us-east-1'}); after var AWS = require("aws-sdk"); Also make sure that you have no duplicate lines "var AWS = require("aws-sdk");"bpavlov

2 Answers

0
votes

iram,

If I take your exact code and paste it into a Lambda Node.js 4.3 function and execute a test from the Lambda Console, this is the result:

ERROR: InvalidParameter: Invalid parameter: TopicArn Reason: Both TopicArn and TargetArn specified. Use only one or the other

This means that in your params, you need to comment out either TopicArn or TargetArn or put in some logic to determine if the incoming payload contains an Arn that is a target endpoint or a topic endpoint.

You could still have permissions issues with Lambda execution role to SNS or to CW Logs, however, regardless if you have permission to publish or send logs to CloudWatch from your Lambda function, running a test from the console will always spit out some logging of what's going on.

Good luck.

0
votes
const AWS = require('aws-sdk');
AWS.config.region = 'us-east-1';

specify the regeion to be used by the aws sdk like this