-
-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(aws-lambda): patch aws-lambda to wait for lambda to be in ready s…
…tate
- Loading branch information
Showing
7 changed files
with
2,312 additions
and
2,007 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
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
43 changes: 43 additions & 0 deletions
43
packages/serverless-components/aws-lambda/__tests__/waitUntilReady.test.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,43 @@ | ||
const { | ||
mockGetFunctionPromise, | ||
mockGetFunction | ||
} = require("../__mocks__/aws-sdk.mock"); | ||
const { waitUntilReady } = require("../waitUntilReady"); | ||
|
||
jest.mock("aws-sdk", () => require("../__mocks__/aws-sdk.mock")); | ||
|
||
describe("waitLambdaReady", () => { | ||
it("waits until lambda is ready", async () => { | ||
mockGetFunctionPromise.mockResolvedValueOnce({ | ||
Configuration: { | ||
State: "Pending", | ||
LastUpdateStatus: "InProgress" | ||
} | ||
}); | ||
|
||
mockGetFunctionPromise.mockResolvedValueOnce({ | ||
Configuration: { | ||
State: "Active", | ||
LastUpdateStatus: "Successful" | ||
} | ||
}); | ||
|
||
const ready = await waitUntilReady( | ||
{ | ||
debug: () => { | ||
// intentionally empty | ||
} | ||
}, | ||
"test-function", | ||
"us-east-1", | ||
1 | ||
); | ||
|
||
expect(ready).toBe(true); | ||
|
||
expect(mockGetFunction).toBeCalledWith({ | ||
FunctionName: "test-function" | ||
}); | ||
expect(mockGetFunction).toBeCalledTimes(2); // since first time it's mocked as not ready | ||
}); | ||
}); |
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
31 changes: 31 additions & 0 deletions
31
packages/serverless-components/aws-lambda/waitUntilReady.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,31 @@ | ||
const AWS = require("aws-sdk"); | ||
|
||
/** | ||
* Wait up to 10 minutes for the Lambda to be ready. | ||
* This is needed due to: https://docs.aws.amazon.com/lambda/latest/dg/functions-states.html | ||
*/ | ||
const waitUntilReady = async (context, fnName, region, pollInterval = 5000) => { | ||
const lambda = new AWS.Lambda({ region }); | ||
const startDate = new Date(); | ||
const startTime = startDate.getTime(); | ||
const waitDurationMillis = 600000; // 10 minutes max wait time | ||
|
||
context.debug(`Waiting up to 600 seconds for Lambda ${fnName} to be ready.`); | ||
|
||
while (new Date().getTime() - startTime < waitDurationMillis) { | ||
const { | ||
Configuration: { LastUpdateStatus, State } | ||
} = await lambda.getFunction({ FunctionName: fnName }).promise(); | ||
|
||
if (State === "Active" && LastUpdateStatus === "Successful") { | ||
return true; | ||
} | ||
await new Promise((r) => setTimeout(r, pollInterval)); // retry every 5 seconds | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
module.exports = { | ||
waitUntilReady | ||
}; |
Oops, something went wrong.