Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(rules): limit the Lambda number of versions #698

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"sls-dev-tools": "./lib/index.js"
},
"dependencies": {
"aws-sdk": "^2.655.0",
"aws-sdk": "2.1124.0",
"blessed": "^0.1.81",
"blessed-contrib": "^4.8.17",
"chalk": "^4.0.0",
Expand Down
2 changes: 2 additions & 0 deletions src/guardian/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import NoMaximumTimeout from "./rules/best_practices/no-max-timeout";
import NoMaximumMemory from "./rules/best_practices/no-max-memory";
import NoIdenticalCode from "./rules/best_practices/no-identical-code";
import NoSharedRoles from "./rules/best_practices/no-shared-roles";
import LimitedNumberOfVersions from "./rules/best_practices/limited-number-of-versions";
import {
getAWSCredentials,
getStackResources,
Expand Down Expand Up @@ -43,6 +44,7 @@ class GuardianCI {
NoMaximumMemory,
NoIdenticalCode,
NoSharedRoles,
LimitedNumberOfVersions,
];
this.failingChecks = [];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
class LimitedNumberOfVersions {
constructor(AWS, stackName, stackFunctions) {
this.name = "limited-number-of-versions";
this.AWS = AWS;
this.stackName = stackName;
this.stackFunctions = stackFunctions;
this.result = false;
this.failingResources = [];
this.failureMessage =
"The following functions have a number of deployed versions greater than 3";
this.rulePage =
"See (https://m33.notion.site/Serverless-Sustainability-Audit-a36847289fd64339a60e40bc5aa63092) for impact.";
this.lambda = new AWS.Lambda();
}

// Given the name of a lambda, returns its number of versions (excluding $Latest)
async getNumberOfLambdaVersions(FunctionName) {
const lambdaVersions = await this.lambda
.listVersionsByFunction({
FunctionName,
})
.promise();
return lambdaVersions.Versions.length - 1;
}

async run() {
try {
const functionsWithCount = await Promise.all(
this.stackFunctions.map(async (lambda) => {
const numberOfVersions = await this.getNumberOfLambdaVersions(
lambda.FunctionName
);
return {
functionArn: lambda.FunctionArn,
numberOfVersions,
};
})
);

const functionsWithTooManyVersions = functionsWithCount.reduce(
(acc, current) =>
current.numberOfVersions > 3 ? [...acc, current] : acc,
[]
);

this.failingResources = functionsWithTooManyVersions.map(
(lambdaWithCount) => ({
arn: lambdaWithCount.functionArn,
numberOfVersions: lambdaWithCount.numberOfVersions,
})
);

if (functionsWithTooManyVersions.length > 0) {
this.result = false;
} else {
this.result = true;
}
} catch (e) {
console.error(e);
this.result = false;
}
return this.result;
}
}

export default LimitedNumberOfVersions;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Functions should have less than 3 versions

It is most of the time useless to keep more than 3 online versions for your lambdas.
Storing useless data has a negative impact on teh environment. (For more info, see https://www.notion.so/m33/Serverless-Sustainability-Audit-a36847289fd64339a60e40bc5aa63092?p=a0471b2c407144fe839150326ed804d8)

AWS Lambda service furthermore has limited storage space, which can be easily reached if you keep every single version of your lambdas.

---

## Suggested Actions

- Use [serverless-prune-versions](https://github.com/manwaring/serverless-prune-versions) to regularly prune the versions of the affected lambdas
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1689,15 +1689,15 @@ atob@^2.1.2:
resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9"
integrity sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg==

aws-sdk@^2.655.0:
version "2.855.0"
resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.855.0.tgz#2788e2655d8e2eecfc9d86acafd13d872725e73a"
integrity sha512-u1lO75V82E4REeu0Pt3RE1Kg2CdtUoMgMnyR02cUxdWnoTBpdSlwvz2X2U0VEGW2oYzeJJYp0N0H1sgSRnv8vg==
aws-sdk@2.1124.0:
version "2.1124.0"
resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.1124.0.tgz#350550b973f210b092a45184036659251d7cbd1e"
integrity sha512-LIj2EmvduQmyjpSLoDPe2TvRBrGWhJKlCuC7lungN4f+qYPOfVWg6/c9GdP99W0q5p2IqhDkCmQ/ZZBpO1Z/Fg==
dependencies:
buffer "4.9.2"
events "1.1.1"
ieee754 "1.1.13"
jmespath "0.15.0"
jmespath "0.16.0"
querystring "0.2.0"
sax "1.2.1"
url "0.10.3"
Expand Down Expand Up @@ -4382,10 +4382,10 @@ jest@^26.6.3:
import-local "^3.0.2"
jest-cli "^26.6.3"

jmespath@0.15.0:
version "0.15.0"
resolved "https://registry.yarnpkg.com/jmespath/-/jmespath-0.15.0.tgz#a3f222a9aae9f966f5d27c796510e28091764217"
integrity sha1-o/Iiqarp+Wb10nx5ZRDigJF2Qhc=
jmespath@0.16.0:
version "0.16.0"
resolved "https://registry.yarnpkg.com/jmespath/-/jmespath-0.16.0.tgz#b15b0a85dfd4d930d43e69ed605943c802785076"
integrity sha512-9FzQjJ7MATs1tSpnco1K6ayiYE3figslrXA72G2HQ/n76RzvYlofyi5QM+iX4YRs/pu3yzxlVQSST23+dMDknw==

"js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0:
version "4.0.0"
Expand Down