forked from tensult/aws-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_sns_topic_and_subscriptions.js
75 lines (64 loc) · 2.19 KB
/
create_sns_topic_and_subscriptions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
*
* @description This function creates a SNS topic and then create multiple emails subscription on that topic.
*
*
* To run the function follow below command:
* AWS_PROFILE=<aws-profile> node create_sns_topic_and_subscriptions.js -r <aws-region> -t '<topic-name>' -d '<topic-display-name>' -s '<email_1>,<email_2>,..'
*/
const AWS = require('aws-sdk');
const cli = require('cli');
const awsConfigHelper = require('./util/awsConfigHelper');
const awsUtil = require('./util/aws');
const cliArgs = cli.parse({
region: ['r', 'AWSRegion', 'string'],
topicName: ['t', 'TopicName', 'string'],
topicDisplayName: ['d', 'TopicDisplayName', 'string'],
subscriptionEnpoints: ['s', 'SubscriptionEndpoints', 'string'],
});
if (!cliArgs.region || !cliArgs.topicName || !cliArgs.topicDisplayName || !cliArgs.subscriptionEnpoints) {
cli.getUsage();
}
function getTopicName() {
return cliArgs.topicName;
}
function getTopicDisplayName() {
return cliArgs.topicDisplayName;
}
function getSubscriptionEndpoints() {
return awsUtil.getStringArrayFromCommaSeparatedString(cliArgs.subscriptionEnpoints);
}
function createSubscription(sns, topicArn, subscriptionEndpoint) {
const params = {
Protocol: 'email',
TopicArn: topicArn,
Endpoint: subscriptionEndpoint
};
return sns.subscribe(params).promise();
}
function createTopic(sns, topicName, topicDisplayName) {
const params = {
Name: topicName,
Attributes: {
DisplayName: topicDisplayName
}
};
console.log(params);
return sns.createTopic(params).promise();
}
async function handler() {
try {
await awsConfigHelper.updateConfig(cliArgs.region);
const sns = new AWS.SNS();
const topicName = getTopicName();
const topicDisplayName = getTopicDisplayName();
const createdTopic = await createTopic(sns, topicName, topicDisplayName);
const subscriptionEndpoints = getSubscriptionEndpoints();
for (const subscriptionEndpoint of subscriptionEndpoints) {
await createSubscription(sns, createdTopic.TopicArn, subscriptionEndpoint);
}
} catch (error) {
console.error(error);
}
}
handler();