serverless-sns-dlq 1.0.0
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.
- package/.github/workflows/publish_npmjs.yml +18 -0
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/index.js +137 -0
- package/package.json +22 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
name: Publish Package to NPM JS
|
|
2
|
+
on:
|
|
3
|
+
release:
|
|
4
|
+
types: [released]
|
|
5
|
+
workflow_dispatch:
|
|
6
|
+
jobs:
|
|
7
|
+
build:
|
|
8
|
+
runs-on: ubuntu-latest
|
|
9
|
+
steps:
|
|
10
|
+
- uses: actions/checkout@v3
|
|
11
|
+
- uses: actions/setup-node@v3
|
|
12
|
+
with:
|
|
13
|
+
node-version: "16.x"
|
|
14
|
+
registry-url: "https://registry.npmjs.org"
|
|
15
|
+
- run: npm ci
|
|
16
|
+
- run: npm publish
|
|
17
|
+
env:
|
|
18
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Randy Hermawan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const pascalCase = (camelCase) =>
|
|
4
|
+
camelCase.slice(0, 1).toUpperCase() + camelCase.slice(1);
|
|
5
|
+
|
|
6
|
+
const randomText = (length) =>
|
|
7
|
+
Array.from({ length }, () => Math.random().toString(36)[2]).join("");
|
|
8
|
+
|
|
9
|
+
class ServerlessPlugin {
|
|
10
|
+
constructor(serverless, options, { log }) {
|
|
11
|
+
this.serverless = serverless;
|
|
12
|
+
this.options = options;
|
|
13
|
+
this.logger = log;
|
|
14
|
+
|
|
15
|
+
this.hooks = {
|
|
16
|
+
"before:deploy:deploy": this.Deploy.bind(this),
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
this.region = serverless.getProvider("aws").getRegion();
|
|
20
|
+
|
|
21
|
+
serverless.configSchemaHandler.defineFunctionProperties("aws", {
|
|
22
|
+
type: "object",
|
|
23
|
+
properties: { setDlq: { type: "boolean" } },
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
serverless.configSchemaHandler.defineFunctionEventProperties("aws", "sns", {
|
|
27
|
+
type: "object",
|
|
28
|
+
properties: { setDlq: { type: "boolean" } },
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
_validateQueueName = (queueName) => {
|
|
33
|
+
if (queueName.length > 80) {
|
|
34
|
+
this.logger.error(
|
|
35
|
+
`Generated queue name [${queueName}] is longer than 80 characters.`
|
|
36
|
+
);
|
|
37
|
+
process.exit(1);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
Deploy = async () => {
|
|
42
|
+
const functions = this.serverless.service.functions;
|
|
43
|
+
const template =
|
|
44
|
+
this.serverless.service.provider.compiledCloudFormationTemplate;
|
|
45
|
+
|
|
46
|
+
const validateSubs = Object.entries(functions).flatMap(
|
|
47
|
+
async ([fnName, fnDef]) => {
|
|
48
|
+
if (fnDef.setDlq !== false) {
|
|
49
|
+
const snsEvents = (fnDef.events || []).map((evt) => evt.sns);
|
|
50
|
+
|
|
51
|
+
await snsEvents.forEach((snsEvent) => {
|
|
52
|
+
this._addSQSDeadLetter(template, fnName, fnDef, snsEvents);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
await Promise.all(validateSubs);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
_addSQSDeadLetter = (template, fnName, fnDef, snsEvents) => {
|
|
62
|
+
const accountId = (snsEvents[0].arn || snsEvents[0]).split(":")[4];
|
|
63
|
+
|
|
64
|
+
const dlqName = `${fnDef.name}-dlq`;
|
|
65
|
+
this._validateQueueName(dlqName);
|
|
66
|
+
|
|
67
|
+
const dlqArn = `arn:aws:sqs:${this.region}:${accountId}:${dlqName}`;
|
|
68
|
+
const dlqUrl = `https://sqs.${this.region}.amazonaws.com/${accountId}/${dlqName}`;
|
|
69
|
+
|
|
70
|
+
const queueId = pascalCase(`${fnName}SQSDeadLetterQueue`);
|
|
71
|
+
const queueDef = {
|
|
72
|
+
Type: "AWS::SQS::Queue",
|
|
73
|
+
Properties: { QueueName: dlqName },
|
|
74
|
+
};
|
|
75
|
+
template.Resources[queueId] = queueDef;
|
|
76
|
+
|
|
77
|
+
const funcId = pascalCase(`${fnName}LambdaFunction`);
|
|
78
|
+
const funcDef = template.Resources[funcId];
|
|
79
|
+
|
|
80
|
+
if (funcDef.Properties.Environment)
|
|
81
|
+
funcDef.Properties.Environment.Variables["DLQ_QUEUE_URL"] = dlqUrl;
|
|
82
|
+
else
|
|
83
|
+
funcDef.Properties.Environment = { Variables: { DLQ_QUEUE_URL: dlqUrl } };
|
|
84
|
+
|
|
85
|
+
template.Resources[funcId] = funcDef;
|
|
86
|
+
|
|
87
|
+
const CHUNK_SIZE = 10;
|
|
88
|
+
|
|
89
|
+
for (let i = 0; i < snsEvents.length; i += CHUNK_SIZE) {
|
|
90
|
+
const queuePolicyId = pascalCase(
|
|
91
|
+
`${fnName}SQSDeadLetterQueuePolicyIter${i}`
|
|
92
|
+
);
|
|
93
|
+
const policyStatements = [];
|
|
94
|
+
|
|
95
|
+
const chunk = snsEvents.slice(i, i + CHUNK_SIZE);
|
|
96
|
+
chunk.forEach((sns) => {
|
|
97
|
+
if (sns.setDlq !== false) {
|
|
98
|
+
const snsSubId = pascalCase(
|
|
99
|
+
`${fnName}SnsSubscription${(sns.arn || sns).split(":").pop()}`
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
const snsSubDef = template.Resources[snsSubId];
|
|
103
|
+
snsSubDef.Properties["RedrivePolicy"] = {
|
|
104
|
+
deadLetterTargetArn: dlqArn,
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
template.Resources[snsSubId] = snsSubDef;
|
|
108
|
+
|
|
109
|
+
policyStatements.push({
|
|
110
|
+
Effect: "Allow",
|
|
111
|
+
Principal: { Service: "sns.amazonaws.com" },
|
|
112
|
+
Action: "sqs:SendMessage",
|
|
113
|
+
Resource: { "Fn::GetAtt": [queueId, "Arn"] },
|
|
114
|
+
Condition: { ArnEquals: { "aws:SourceArn": sns.arn || sns } },
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
if (policyStatements.length > 0) {
|
|
120
|
+
const queuePolicyDef = {
|
|
121
|
+
Type: "AWS::SQS::QueuePolicy",
|
|
122
|
+
Properties: {
|
|
123
|
+
Queues: [{ Ref: queueId }],
|
|
124
|
+
PolicyDocument: {
|
|
125
|
+
Version: "2012-10-17",
|
|
126
|
+
Statement: policyStatements,
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
template.Resources[queuePolicyId] = queuePolicyDef;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
module.exports = ServerlessPlugin;
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "serverless-sns-dlq",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Automatically attach SNS redrive policy to DLQ for every handler function specified with SNS trigger",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"author": "Randy Hermawan",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/randyhermawan/serverless-sns-dlq.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"serverless",
|
|
13
|
+
"sns",
|
|
14
|
+
"dlq"
|
|
15
|
+
],
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/randyhermawan/serverless-sns-dlq/issues"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/randyhermawan/serverless-sns-dlq#readme",
|
|
21
|
+
"dependencies": {}
|
|
22
|
+
}
|