serverless-sns-dlq 1.1.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +37 -6
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -14,18 +14,19 @@ class ServerlessPlugin {
14
14
 
15
15
  this.hooks = {
16
16
  "before:deploy:deploy": this.Deploy.bind(this),
17
+ "before:deploy:function:packageFunction": this.DeploySingleFunction.bind(this),
17
18
  };
18
19
 
19
20
  this.region = serverless.getProvider("aws").getRegion();
20
21
 
21
22
  serverless.configSchemaHandler.defineFunctionProperties("aws", {
22
23
  type: "object",
23
- properties: { setDlq: { type: "boolean" } },
24
+ properties: { enableSnsDlq: { type: "boolean" } },
24
25
  });
25
26
 
26
27
  serverless.configSchemaHandler.defineFunctionEventProperties("aws", "sns", {
27
28
  type: "object",
28
- properties: { setDlq: { type: "boolean" } },
29
+ properties: { enableSnsDlq: { type: "boolean" } },
29
30
  });
30
31
  }
31
32
 
@@ -39,26 +40,56 @@ class ServerlessPlugin {
39
40
  };
40
41
 
41
42
  Deploy = async () => {
43
+ const accountId = await this.serverless.getProvider("aws").getAccountId();
42
44
  const functions = this.serverless.service.functions;
43
45
  const template =
44
46
  this.serverless.service.provider.compiledCloudFormationTemplate;
45
47
 
46
48
  Object.entries(functions).flatMap(
47
49
  async ([fnName, fnDef]) => {
48
- if (fnDef.setDlq !== false) {
50
+ if (fnDef.enableSnsDlq !== false) {
49
51
  const snsEvents = (fnDef.events || [])
50
52
  .filter((evt) => evt.sns)
51
53
  .map((evt) => evt.sns);
52
54
 
53
- if (snsEvents.length > 0) this._configure(template, fnName, fnDef, snsEvents);
55
+ if (snsEvents.length > 0) this._configure(accountId, template, fnName, fnDef, snsEvents);
54
56
  }
55
57
  }
56
58
  );
57
59
  };
58
60
 
59
- _configure = (template, fnName, fnDef, snsEvents) => {
60
- const accountId = (snsEvents[0].arn || snsEvents[0]).split(":")[4];
61
+ DeploySingleFunction = async () => {
62
+ const fnName = this.options.function;
63
+ const fnDef = this.serverless.service.functions[fnName];
61
64
 
65
+ if (!fnDef) {
66
+ this.logger.error(`Function ${fnName} not found`);
67
+ return;
68
+ }
69
+
70
+ // Skip if SNS DLQ is explicitly disabled
71
+ if (fnDef.enableSnsDlq === false) return;
72
+
73
+ // Check if function has SNS events
74
+ const snsEvents = (fnDef.events || [])
75
+ .filter((evt) => evt.sns)
76
+ .map((evt) => evt.sns);
77
+
78
+ if (snsEvents.length === 0) return;
79
+
80
+ // Calculate DLQ URL
81
+ const accountId = await this.serverless.getProvider("aws").getAccountId();
82
+ const dlqName = `${fnDef.name}-dlq`;
83
+ const dlqUrl = `https://sqs.${this.region}.amazonaws.com/${accountId}/${dlqName}`;
84
+
85
+ // Add DLQ_QUEUE_URL to function environment
86
+ if (!fnDef.environment) fnDef.environment = {};
87
+ fnDef.environment.DLQ_QUEUE_URL = dlqUrl;
88
+
89
+ this.logger.notice(`Added DLQ_QUEUE_URL for function ${fnName}: ${dlqUrl}`);
90
+ };
91
+
92
+ _configure = (accountId, template, fnName, fnDef, snsEvents) => {
62
93
  const dlqName = `${fnDef.name}-dlq`;
63
94
  this._validateQueueName(dlqName);
64
95
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serverless-sns-dlq",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "Automatically attach SNS redrive policy to DLQ for every handler function specified with SNS trigger",
5
5
  "main": "index.js",
6
6
  "author": "Randy Hermawan",
@@ -19,4 +19,4 @@
19
19
  },
20
20
  "homepage": "https://github.com/randyhermawan/serverless-sns-dlq#readme",
21
21
  "dependencies": {}
22
- }
22
+ }