I am trying to publish to my AWS SNS topic from a lambda function and I just don't understand why the following code isn't working.
I have tried so many variations of the following code but it never seems to work.
I have also made sure that my role has the correct privileges as described here: https://stackoverflow.com/a/31493841/4198290
My logging in my callback function never fires and no matter what I set my Lambda timeout period to it always just waits out the clock.
I have double checked and tripled checked my AWS ARN that I'm using and I'm pretty confident that it is correct.
If I use an invalid AWS ARN it behaves exactly the same way when my code is fired.
CloudWatch Logs doesn't provide me with any extra information. It's almost like it just silently fails without logging anywhere.
const logger = require('./Framework/logger');
const aws = require('aws-sdk');
exports.handler = (event, context, callback) => {
try {
var message = {
"default": "TEST"
};
var snsParameters = {
TopicArn: process.env.Topic,
Message: JSON.stringify(message)
}
logger.error(snsParameters);
var sns = new aws.SNS();
sns.publish(snsParameters, function (err, data) {
if (err) {
logger.error('error publishing to SNS');
context.fail(err);
} else {
logger.error('message published to SNS');
context.done(null, data);
}
});
}
catch (e) {
logger.error(e);
}
};
EDIT #1 I've changed my code to attempt an async call while specifying an AWS version, but it still isn't working
const repository = require('./Framework/database');
const logger = require('./Framework/logger');
const aws = require('aws-sdk');
aws.config.update({ region: 'eu-west-1' });
exports.handler = async (event, context, callback) => {
try {
var message = "TEST";
var snsParameters = {
TopicArn: process.env.Topic,
Message: message
}
logger.error(snsParameters);
var sns = new aws.SNS({ apiVersion: '2010-03-31' });
var snsPromise = sns.publish(snsParameters).promise();
await snsPromise
.then((data) => {
logger.error('message published to SNS');
context.done(null, data);
})
.catch((error) => {
logger.error('error publishing to SNS');
context.fail(err);
});
}
catch (e) {
logger.error(e);
}
};
Output Log:
START RequestId: [removed] Version: $LATEST
2018-08-22T14:25:35.126Z [removed] { TopicArn: 'arn:aws:sns:eu-west-1:[removed]', Message: 'TEST' }
END RequestId: [removed]
REPORT RequestId: [removed] Duration: 3003.27 ms Billed Duration: 3000 ms Memory Size: 128 MB Max Memory Used: 66 MB
2018-08-22T14:25:37.847Z [removed] Task timed out after 3.00 seconds
EDIT #2 I have updated my code as follows
var AWS = require('aws-sdk');
AWS.config.update({ region: 'eu-west-1' });
exports.handler = async (event, context, callback) => {
try {
var message = "TEST";
var snsParameters = {
TopicArn: process.env.Topic,
Message: message
};
console.log(snsParameters);
var publishTextPromise = new AWS.SNS({ apiVersion: '2010-03-31' }).publish(snsParameters).promise();
publishTextPromise
.then(function (data) {
console.log("Message ${params.Message} send sent to the topic ${params.TopicArn}");
console.log("MessageID is " + data.MessageId);
callback(null, data);
})
.catch(function (err) {
console.log(err);
callback(err);
});
console.log(publishTextPromise);
}
catch (e) {
console.log(e);
}
};
Output Log
START RequestId: 1155953d-b645-11e8-883f-bb9cff5533aa Version: $LATEST
2018-09-12T04:33:59.966Z 1155953d-b645-11e8-883f-bb9cff5533aa { TopicArn: 'arn:aws:sns:eu-west-1:XXX:XXX',
Message: 'TEST' }
2018-09-12T04:34:00.516Z 1155953d-b645-11e8-883f-bb9cff5533aa Promise { <pending> }
END RequestId: 1155953d-b645-11e8-883f-bb9cff5533aa
REPORT RequestId: 1155953d-b645-11e8-883f-bb9cff5533aa Duration: 610.75 ms Billed Duration: 700 ms Memory Size: 128 MB Max Memory Used: 32 MB