Skip to content

Commit 638c003

Browse files
authored
fix aws sns topic subscribe (#193)
1 parent 23c7480 commit 638c003

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

lib/autoscale-manager.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = (logger) => {
88
// listen for SNS lifecycle changes
99
let lifecycleEmitter = new Emitter();
1010
lifecycleEmitter.dryUpCalls = false;
11-
if (process.env.AWS_SNS_TOPIC_ARM) {
11+
if (process.env.AWS_SNS_TOPIC_ARN) {
1212

1313
(async function() {
1414
try {

lib/aws-sns-lifecycle.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,17 @@ class SnsNotifier extends Emitter {
6969
subscriptionRequestId: this.subscriptionRequestId
7070
}, 'response from SNS SubscribeURL');
7171
const data = await this.describeInstance();
72-
this.lifecycleState = data.AutoScalingGroups[0].Instances[0].LifecycleState;
72+
73+
const group = data.AutoScalingGroups.find((group) =>
74+
group.Instances && group.Instances.some((instance) => instance.InstanceId === this.instanceId)
75+
);
76+
if (!group) {
77+
this.logger.error('Current instance not found in any Auto Scaling group', data);
78+
} else {
79+
const instance = group.Instances.find((instance) => instance.InstanceId === this.instanceId);
80+
this.lifecycleState = instance.LifecycleState;
81+
}
82+
7383
this.emit('SubscriptionConfirmation', {publicIp: this.publicIp});
7484
break;
7585

@@ -137,13 +147,13 @@ class SnsNotifier extends Emitter {
137147
try {
138148
const params = {
139149
Protocol: 'http',
140-
TopicArn: process.env.AWS_SNS_TOPIC_ARM,
150+
TopicArn: process.env.AWS_SNS_TOPIC_ARN,
141151
Endpoint: this.snsEndpoint
142152
};
143153
const response = await snsClient.send(new SubscribeCommand(params));
144-
this.logger.info({response}, `response to SNS subscribe to ${process.env.AWS_SNS_TOPIC_ARM}`);
154+
this.logger.info({response}, `response to SNS subscribe to ${process.env.AWS_SNS_TOPIC_ARN}`);
145155
} catch (err) {
146-
this.logger.error({err}, `Error subscribing to SNS topic arn ${process.env.AWS_SNS_TOPIC_ARM}`);
156+
this.logger.error({err}, `Error subscribing to SNS topic arn ${process.env.AWS_SNS_TOPIC_ARN}`);
147157
}
148158
}
149159

@@ -154,9 +164,9 @@ class SnsNotifier extends Emitter {
154164
SubscriptionArn: this.subscriptionArn
155165
};
156166
const response = await snsClient.send(new UnsubscribeCommand(params));
157-
this.logger.info({response}, `response to SNS unsubscribe to ${process.env.AWS_SNS_TOPIC_ARM}`);
167+
this.logger.info({response}, `response to SNS unsubscribe to ${process.env.AWS_SNS_TOPIC_ARN}`);
158168
} catch (err) {
159-
this.logger.error({err}, `Error unsubscribing to SNS topic arn ${process.env.AWS_SNS_TOPIC_ARM}`);
169+
this.logger.error({err}, `Error unsubscribing to SNS topic arn ${process.env.AWS_SNS_TOPIC_ARN}`);
160170
}
161171
}
162172

0 commit comments

Comments
 (0)