obdfcascrape/setupQueue.js
Martin Donnelly be5d3eae07 init
2019-05-05 20:13:56 +01:00

147 lines
2.9 KiB
JavaScript

// https://github.com/markcallen/snssqs
const AWS = require('aws-sdk');
const util = require('util');
const async = require('async');
const fs = require('fs');
require('dotenv').config({
'path': `${__dirname }/.env`
});
// configure AWS
AWS.config.update({ 'accessKeyId': process.env.AWS_ACCESS_KEY_ID, 'secretAccessKey': process.env.AWS_SECRET_ACCESS_KEY, 'region': process.env.AWS_REGION || 'eu-west-1' });
const sns = new AWS.SNS();
const sqs = new AWS.SQS();
const config = {};
function createTopic(cb) {
sns.createTopic({
'Name': process.env.SQS_NAME
}, function (err, result) {
if (err !== null) {
console.log(util.inspect(err));
return cb(err);
}
console.log(util.inspect(result));
config.TopicArn = result.TopicArn;
cb();
});
}
function createQueue(cb) {
sqs.createQueue({
'QueueName': process.env.SQS_NAME
}, function (err, result) {
if (err !== null) {
console.log(util.inspect(err));
return cb(err);
}
console.log(util.inspect(result));
config.QueueUrl = result.QueueUrl;
cb();
});
}
function getQueueAttr(cb) {
sqs.getQueueAttributes({
'QueueUrl': config.QueueUrl,
'AttributeNames': ['QueueArn']
}, function (err, result) {
if (err !== null) {
console.log(util.inspect(err));
return cb(err);
}
console.log(util.inspect(result));
config.QueueArn = result.Attributes.QueueArn;
cb();
});
}
function snsSubscribe(cb) {
sns.subscribe({
'TopicArn': config.TopicArn,
'Protocol': 'sqs',
'Endpoint': config.QueueArn
}, function (err, result) {
if (err !== null) {
console.log(util.inspect(err));
return cb(err);
}
console.log(util.inspect(result));
cb();
});
}
function setQueueAttr(cb) {
const queueUrl = config.QueueUrl;
const topicArn = config.TopicArn;
const sqsArn = config.QueueArn;
const attributes = {
'Version': '2008-10-17',
'Id': `${sqsArn}/SQSDefaultPolicy`,
'Statement': [{
'Sid': `Sid${new Date().getTime()}`,
'Effect': 'Allow',
'Principal': {
'AWS': '*'
},
'Action': 'SQS:SendMessage',
'Resource': sqsArn,
'Condition': {
'ArnEquals': {
'aws:SourceArn': topicArn
}
}
}
]
};
sqs.setQueueAttributes({
'QueueUrl': queueUrl,
'Attributes': {
'Policy': JSON.stringify(attributes)
}
}, function (err, result) {
if (err !== null) {
console.log(util.inspect(err));
return cb(err);
}
console.log(util.inspect(result));
cb();
});
}
function writeConfigFile(cb) {
fs.writeFile('config.json', JSON.stringify(config, null, 4), function(err) {
if(err)
return cb(err);
console.log('config saved to config.json');
cb();
});
}
async.series([createTopic, createQueue, getQueueAttr, snsSubscribe, setQueueAttr, writeConfigFile]);