-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (61 loc) · 2.07 KB
/
index.js
File metadata and controls
65 lines (61 loc) · 2.07 KB
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
"use strict";
/**
* Serverless Plugin to add a `RecursiveLoop` property to AWS Lambda functions.
*
* The `RecursiveLoop` property allows configuration of recursive loop detection
* for Lambda functions. When set to "Allow", no action is taken if a loop is detected.
* When set to "Terminate", the function invocation is stopped and a notification is sent.
*
* @example
* // serverless.yml configuration
* functions:
* myFunction:
* handler: handler.myFunction
* recursiveLoop: Terminate
*/
class ServerlessRecursiveLoopPlugin {
constructor(serverless) {
this.serverless = serverless;
this.provider = this.serverless.getProvider("aws");
this.hooks = {
"package:compileFunctions": this.addRecursiveLoopProperty.bind(this),
};
if (
this.serverless.configSchemaHandler &&
this.serverless.configSchemaHandler.defineFunctionProperties
) {
this.serverless.configSchemaHandler.defineFunctionProperties("aws", {
properties: {
recursiveLoop: {
type: "string",
enum: ["Allow", "Terminate"],
},
},
});
}
}
/**
* Adds the `RecursiveLoop` property to the Lambda function's CloudFormation template.
*
* This method iterates through all functions defined in the Serverless service.
* If a function has a `recursiveLoop` property, it validates the value and adds
* it to the CloudFormation template.
*/
addRecursiveLoopProperty() {
const service = this.serverless.service;
for (const functionName in service.functions) {
const func = service.functions[functionName];
if (func.recursiveLoop) {
const recursiveLoop = func.recursiveLoop;
if (["Allow", "Terminate"].includes(recursiveLoop)) {
const functionResource =
service.provider.compiledCloudFormationTemplate.Resources[
this.provider.naming.getLambdaLogicalId(functionName)
];
functionResource.Properties.RecursiveLoop = recursiveLoop;
}
}
}
}
}
module.exports = ServerlessRecursiveLoopPlugin;