-
-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nextjs-component, aws-lambda): allow removing old lambda versions (
- Loading branch information
Showing
15 changed files
with
297 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const Sequencer = require("@jest/test-sequencer").default; | ||
|
||
class CustomSequencer extends Sequencer { | ||
constructor() { | ||
super(); | ||
} | ||
|
||
sort(tests) { | ||
// Test structure information | ||
// https://github.com/facebook/jest/blob/6b8b1404a1d9254e7d5d90a8934087a9c9899dab/packages/jest-runner/src/types.ts#L17-L21 | ||
const copyTests = Array.from(tests); | ||
return copyTests.sort((testA, testB) => { | ||
// FIXME: figure out why this test started failing if run after another test | ||
if (testA.path.includes("serverless-trace.test")) { | ||
return -1; | ||
} | ||
if (testB.path.includes("serverless-trace.test")) { | ||
return 1; | ||
} | ||
return testA.path > testB.path ? 1 : -1; | ||
}); | ||
} | ||
} | ||
|
||
module.exports = CustomSequencer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/serverless-components/aws-lambda/__tests__/removeLambdaVersions.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { | ||
mockGetFunctionConfigurationPromise, | ||
mockListVersionsByFunctionPromise, | ||
mockGetFunctionConfiguration, | ||
mockListVersionsByFunction, | ||
mockDeleteFunction, | ||
mockDeleteFunctionPromise | ||
} from "../__mocks__/aws-sdk.mock"; | ||
import { removeLambdaVersions } from "../src/removeLambdaVersions"; | ||
import { jest } from "@jest/globals"; | ||
|
||
jest.mock("aws-sdk", () => require("../__mocks__/aws-sdk.mock")); | ||
|
||
describe("publishVersion", () => { | ||
it("removes all old lambda versions", async () => { | ||
mockGetFunctionConfigurationPromise.mockResolvedValue({ | ||
FunctionName: "test-function", | ||
Version: "4" | ||
}); | ||
|
||
mockListVersionsByFunctionPromise.mockResolvedValue({ | ||
Versions: [ | ||
{ | ||
FunctionName: "test-function", | ||
Version: "1" | ||
}, | ||
{ | ||
FunctionName: "test-function", | ||
Version: "2" | ||
}, | ||
{ | ||
FunctionName: "test-function", | ||
Version: "3" | ||
}, | ||
{ | ||
FunctionName: "test-function", | ||
Version: "4" | ||
} | ||
] | ||
}); | ||
|
||
mockDeleteFunctionPromise.mockResolvedValueOnce(undefined); | ||
mockDeleteFunctionPromise.mockResolvedValueOnce(undefined); | ||
// Simulate last function couldn't be deleted, but it will not fail the process. | ||
mockDeleteFunctionPromise.mockRejectedValueOnce({ | ||
message: "Mocked error" | ||
}); | ||
|
||
await removeLambdaVersions( | ||
{ | ||
debug: () => { | ||
// intentionally empty | ||
} | ||
}, | ||
"test-function", | ||
"us-east-1" | ||
); | ||
|
||
expect(mockDeleteFunction).toBeCalledWith({ | ||
FunctionName: "test-function", | ||
Qualifier: "1" | ||
}); | ||
|
||
expect(mockDeleteFunction).toBeCalledWith({ | ||
FunctionName: "test-function", | ||
Qualifier: "2" | ||
}); | ||
|
||
expect(mockDeleteFunction).toBeCalledWith({ | ||
FunctionName: "test-function", | ||
Qualifier: "3" | ||
}); | ||
|
||
expect(mockDeleteFunction).toBeCalledTimes(3); | ||
|
||
expect(mockGetFunctionConfiguration).toBeCalledWith({ | ||
FunctionName: "test-function" | ||
}); | ||
expect(mockGetFunctionConfiguration).toBeCalledTimes(1); | ||
|
||
expect(mockListVersionsByFunction).toBeCalledWith({ | ||
FunctionName: "test-function", | ||
MaxItems: 50 | ||
}); | ||
expect(mockListVersionsByFunction).toBeCalledTimes(1); | ||
}); | ||
}); |
75 changes: 75 additions & 0 deletions
75
packages/serverless-components/aws-lambda/src/removeLambdaVersions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Cleanup Lambda code adapted from https://github.com/davidmenger/cleanup-lambda-versions/blob/master/src/cleanupVersions.js | ||
import AWS from "aws-sdk"; | ||
import { | ||
FunctionConfiguration, | ||
ListVersionsByFunctionResponse | ||
} from "aws-sdk/clients/lambda"; | ||
|
||
async function listLambdaVersions( | ||
lambda: AWS.Lambda, | ||
fnName: string | ||
): Promise<ListVersionsByFunctionResponse> { | ||
return await lambda | ||
.listVersionsByFunction({ | ||
FunctionName: fnName, | ||
MaxItems: 50 | ||
}) | ||
.promise(); | ||
} | ||
|
||
async function removeLambdaVersion( | ||
lambda: AWS.Lambda, | ||
fnName: string, | ||
version: string | ||
): Promise<unknown> { | ||
return await lambda | ||
.deleteFunction({ FunctionName: fnName, Qualifier: version }) | ||
.promise(); | ||
} | ||
|
||
async function getLambdaFunction( | ||
lambda: AWS.Lambda, | ||
fnName: string | ||
): Promise<FunctionConfiguration> { | ||
return await lambda | ||
.getFunctionConfiguration({ FunctionName: fnName }) | ||
.promise(); | ||
} | ||
|
||
/** | ||
* Clean up old lambda versions, up to 50 at a time. | ||
* Currently it just removes the version that's not the current version, | ||
* but if needed we could add support for preserving the latest X versions. | ||
* @param context | ||
* @param fnName | ||
* @param region | ||
*/ | ||
export async function removeLambdaVersions( | ||
context: any, | ||
fnName: string, | ||
region: string | ||
) { | ||
const lambda: AWS.Lambda = new AWS.Lambda({ region }); | ||
const fnConfig = await getLambdaFunction(lambda, fnName); | ||
|
||
const versions = await listLambdaVersions(lambda, fnConfig.FunctionName); | ||
|
||
for (const version of versions.Versions ?? []) { | ||
if (version.Version && version.Version !== fnConfig.Version) { | ||
try { | ||
context.debug( | ||
`Removing function: ${fnConfig.FunctionName} - ${version.Version}` | ||
); | ||
await removeLambdaVersion( | ||
lambda, | ||
fnConfig.FunctionName, | ||
version.Version | ||
); | ||
} catch (e) { | ||
context.debug( | ||
`Remove failed (${fnConfig.FunctionName} - ${version.Version}): ${e.message}` | ||
); | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...s-components/nextjs-component/__mocks__/@sls-next/aws-lambda/dist/removeLambdaVersions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { jest } from "@jest/globals"; | ||
|
||
const mockRemoveLambdaVersions = jest.fn(); | ||
|
||
module.exports = { | ||
mockRemoveLambdaVersions, | ||
removeLambdaVersions: mockRemoveLambdaVersions | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.