Skip to content

Commit ed0a7dd

Browse files
NilayJeeltilva
Nilay
authored andcommitted
feat: ✨ create plugin for recursiveLoop lambda property
1 parent e0f1c1f commit ed0a7dd

9 files changed

+11967
-1
lines changed

.editorconfig

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[*.{ts,js}]
2+
indent_style = space
3+
indent_size = 2
4+
trim_trailing_whitespace = true
5+
insert_final_newline = true

.github/workflows/cd.release.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Release
2+
on:
3+
push:
4+
branches:
5+
- main
6+
jobs:
7+
release:
8+
name: Release
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout
12+
uses: actions/checkout@v2
13+
with:
14+
fetch-depth: 0
15+
- name: Setup Node.js
16+
uses: actions/setup-node@v2
17+
with:
18+
node-version: lts/*
19+
- name: Install dependencies
20+
run: npm ci
21+
- name: Release
22+
env:
23+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
24+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
25+
run: npx semantic-release

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# package directories
2+
node_modules
3+
jspm_packages
4+
5+
# Serverless directories
6+
.serverless
7+
8+
# Webpack directories
9+
.webpack

.tool-versions

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nodejs 20.9.0

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Ryan Sonshine
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.

README.md

+50-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
1-
# serverless-lambda-recursiveloop
1+
## Getting started
2+
3+
### Installation
4+
5+
`npm install --save-dev @distinction-dev/serverless-lambda-recursiveloop`
6+
7+
# Serverless RecursiveLoop Plugin
8+
9+
A Serverless Framework plugin that adds a `RecursiveLoop` property to AWS Lambda function configurations. This property controls the recursive loop detection behavior for Lambda functions.
10+
11+
## Functionality
12+
13+
The `RecursiveLoop` property allows you to configure how AWS Lambda handles recursive loop detections for your functions.
14+
15+
- **Allowed Values**:
16+
- `Allow`: No action is taken if a recursive loop is detected.
17+
- `Terminate`: The function invocation is stopped, and a notification is sent when a recursive loop is detected.
18+
19+
- **Type**: String
20+
- **Required**: No
21+
- **Resource Type**: `AWS::Lambda::Function`
22+
23+
## Installation
24+
25+
To use this plugin in your Serverless project, follow these steps:
26+
27+
1. **Create the Plugin File**:
28+
Create a file named `serverless-recursive-loop-plugin.js` in the root directory of your Serverless project and paste the plugin code into it.
29+
30+
2. **Add the Plugin to Your Serverless Configuration**:
31+
In your `serverless.yml` file, add the plugin under the `plugins` section.
32+
33+
```yaml
34+
service: my-service
35+
36+
provider:
37+
name: aws
38+
runtime: nodejs14.x
39+
40+
plugins:
41+
- "@distinction-dev/serverless-lambda-recursiveloop"
42+
43+
functions:
44+
myFunction:
45+
handler: handler.myFunction
46+
recursiveLoop: Terminate
47+
48+
anotherFunction:
49+
handler: handler.anotherFunction
50+
recursiveLoop: Allow

index.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
3+
/**
4+
* Serverless Plugin to add a `RecursiveLoop` property to AWS Lambda functions.
5+
*
6+
* The `RecursiveLoop` property allows configuration of recursive loop detection
7+
* for Lambda functions. When set to "Allow", no action is taken if a loop is detected.
8+
* When set to "Terminate", the function invocation is stopped and a notification is sent.
9+
*
10+
* @example
11+
* // serverless.yml configuration
12+
* functions:
13+
* myFunction:
14+
* handler: handler.myFunction
15+
* recursiveLoop: Terminate
16+
*/
17+
18+
class ServerlessRecursiveLoopPlugin {
19+
constructor(serverless) {
20+
this.serverless = serverless;
21+
this.provider = this.serverless.getProvider("aws");
22+
this.hooks = {
23+
"package:compileFunctions": this.addRecursiveLoopProperty.bind(this),
24+
};
25+
}
26+
27+
/**
28+
* Adds the `RecursiveLoop` property to the Lambda function's CloudFormation template.
29+
*
30+
* This method iterates through all functions defined in the Serverless service.
31+
* If a function has a `recursiveLoop` property, it validates the value and adds
32+
* it to the CloudFormation template.
33+
*/
34+
addRecursiveLoopProperty() {
35+
const service = this.serverless.service;
36+
for (const functionName in service.functions) {
37+
const func = service.functions[functionName];
38+
if (func.recursiveLoop) {
39+
const recursiveLoop = func.recursiveLoop;
40+
if (["Allow", "Terminate"].includes(recursiveLoop)) {
41+
const functionResource =
42+
service.provider.compiledCloudFormationTemplate.Resources[
43+
this.provider.naming.getLambdaLogicalId(functionName)
44+
];
45+
functionResource.Properties.RecursiveLoop = recursiveLoop;
46+
} else {
47+
this.serverless.cli.log(
48+
`Invalid RecursiveLoop value in function ${functionName}. Allowed values are 'Allow' or 'Terminate'.`
49+
);
50+
}
51+
}
52+
}
53+
}
54+
}
55+
56+
module.exports = ServerlessRecursiveLoopPlugin;

0 commit comments

Comments
 (0)