Skip to content

Commit 4043a80

Browse files
authored
Use sdk-v3 with _uploadExisting() (#768)
GH-641
1 parent 3799061 commit 4043a80

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

lib/main.js

+12-16
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ const { createNamespace } = require('continuation-local-storage')
2727
* The default retry count is 3, and only retryable errors will be retried.
2828
* https://docs.aws.amazon.com/sdkref/latest/guide/feature-retry-behavior.html
2929
*/
30-
const { LambdaClient, CreateFunctionCommand } = require('@aws-sdk/client-lambda')
30+
const {
31+
LambdaClient,
32+
CreateFunctionCommand,
33+
GetFunctionCommand,
34+
UpdateFunctionCodeCommand,
35+
UpdateFunctionConfigurationCommand
36+
} = require('@aws-sdk/client-lambda')
3137

3238
const maxBufferSize = 50 * 1024 * 1024
3339

@@ -671,28 +677,18 @@ Emulate only the body of the API Gateway event.
671677
delete functionConfigParams.Layers
672678
}
673679

674-
const updateConfigRequest = lambda.updateFunctionConfiguration(functionConfigParams)
675-
updateConfigRequest.on('retry', (response) => {
676-
console.log(response.error.message)
677-
console.log('=> Retrying')
678-
})
679-
const updateConfigResponse = await updateConfigRequest.promise()
680+
const updateConfigResponse = await lambda.send(new UpdateFunctionConfigurationCommand(functionConfigParams))
680681

681682
// Wait for the `Configuration.LastUpdateStatus` to change from `InProgress` to `Successful`.
683+
const getFunction = new GetFunctionCommand({ FunctionName: params.FunctionName })
682684
for (let i = 0; i < 10; i++) {
683-
const data = await lambda.getFunction({ FunctionName: params.FunctionName }).promise()
685+
const data = await lambda.send(getFunction)
684686
if (data.Configuration.LastUpdateStatus === 'Successful') {
685687
break
686688
}
687689
await new Promise((resolve) => setTimeout(resolve, 3000))
688690
}
689-
690-
const updateCodeRequest = lambda.updateFunctionCode(functionCodeParams)
691-
updateCodeRequest.on('retry', (response) => {
692-
console.log(response.error.message)
693-
console.log('=> Retrying')
694-
})
695-
await updateCodeRequest.promise()
691+
lambda.send(new UpdateFunctionCodeCommand(functionCodeParams))
696692

697693
return updateConfigResponse
698694
}
@@ -1017,7 +1013,7 @@ they may not work as expected in the Lambda environment.
10171013
FunctionName: params.FunctionName
10181014
}).then((existingEventSourceList) => {
10191015
return Promise.all([
1020-
this._uploadExisting(lambda, params).then((results) => {
1016+
this._uploadExisting(lambdaClient, params).then((results) => {
10211017
console.log('=> Done uploading. Results follow: ')
10221018
console.log(results)
10231019
return results

test/main.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ awsMock.setSDK(path.resolve('node_modules/aws-sdk'))
1717

1818
// Migrating to v3.
1919
const { mockClient } = require('aws-sdk-client-mock')
20-
const { LambdaClient, CreateFunctionCommand } = require('@aws-sdk/client-lambda')
20+
const {
21+
LambdaClient,
22+
CreateFunctionCommand,
23+
GetFunctionCommand,
24+
UpdateFunctionCodeCommand,
25+
UpdateFunctionConfigurationCommand
26+
} = require('@aws-sdk/client-lambda')
2127
const mockLambdaClient = mockClient(LambdaClient)
2228
const lambdaClient = new LambdaClient({ region: 'us-east-1' })
2329

@@ -166,6 +172,9 @@ describe('lib/main', function () {
166172
// for sdk v3
167173
mockLambdaClient.reset()
168174
mockLambdaClient.on(CreateFunctionCommand).resolves(lambdaMockSettings.createFunction)
175+
mockLambdaClient.on(GetFunctionCommand).resolves(lambdaMockSettings.getFunction)
176+
mockLambdaClient.on(UpdateFunctionCodeCommand).resolves(lambdaMockSettings.updateFunctionCode)
177+
mockLambdaClient.on(UpdateFunctionConfigurationCommand).resolves(lambdaMockSettings.updateFunctionConfiguration)
169178
})
170179
after(() => {
171180
_awsRestore()
@@ -1577,7 +1586,7 @@ describe('lib/main', function () {
15771586
describe('_uploadExisting', () => {
15781587
it('simple test with mock', () => {
15791588
const params = lambda._params(program, null)
1580-
return lambda._uploadExisting(awsLambda, params).then((results) => {
1589+
return lambda._uploadExisting(lambdaClient, params).then((results) => {
15811590
assert.deepEqual(results, lambdaMockSettings.updateFunctionConfiguration)
15821591
})
15831592
})

0 commit comments

Comments
 (0)