Repository: motdotla/node-lambda
Pull request #768 (2024-12-10 08:37:54)

Use sdk-v3 with `_uploadExisting()`
GH-641


Patch:
diff --git a/lib/main.js b/lib/main.js
index 4860ca1..e5f71f0 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -27,7 +27,13 @@ const { createNamespace } = require('continuation-local-storage')
  * The default retry count is 3, and only retryable errors will be retried.
  * https://docs.aws.amazon.com/sdkref/latest/guide/feature-retry-behavior.html
  */
-const { LambdaClient, CreateFunctionCommand } = require('@aws-sdk/client-lambda')
+const {
+  LambdaClient,
+  CreateFunctionCommand,
+  GetFunctionCommand,
+  UpdateFunctionCodeCommand,
+  UpdateFunctionConfigurationCommand
+} = require('@aws-sdk/client-lambda')
 
 const maxBufferSize = 50 * 1024 * 1024
 
@@ -671,28 +677,18 @@ Emulate only the body of the API Gateway event.
       delete functionConfigParams.Layers
     }
 
-    const updateConfigRequest = lambda.updateFunctionConfiguration(functionConfigParams)
-    updateConfigRequest.on('retry', (response) => {
-      console.log(response.error.message)
-      console.log('=> Retrying')
-    })
-    const updateConfigResponse = await updateConfigRequest.promise()
+    const updateConfigResponse = await lambda.send(new UpdateFunctionConfigurationCommand(functionConfigParams))
 
     // Wait for the `Configuration.LastUpdateStatus` to change from `InProgress` to `Successful`.
+    const getFunction = new GetFunctionCommand({ FunctionName: params.FunctionName })
     for (let i = 0; i < 10; i++) {
-      const data = await lambda.getFunction({ FunctionName: params.FunctionName }).promise()
+      const data = await lambda.send(getFunction)
       if (data.Configuration.LastUpdateStatus === 'Successful') {
         break
       }
       await new Promise((resolve) => setTimeout(resolve, 3000))
     }
-
-    const updateCodeRequest = lambda.updateFunctionCode(functionCodeParams)
-    updateCodeRequest.on('retry', (response) => {
-      console.log(response.error.message)
-      console.log('=> Retrying')
-    })
-    await updateCodeRequest.promise()
+    lambda.send(new UpdateFunctionCodeCommand(functionCodeParams))
 
     return updateConfigResponse
   }
@@ -1017,7 +1013,7 @@ they may not work as expected in the Lambda environment.
           FunctionName: params.FunctionName
         }).then((existingEventSourceList) => {
           return Promise.all([
-            this._uploadExisting(lambda, params).then((results) => {
+            this._uploadExisting(lambdaClient, params).then((results) => {
               console.log('=> Done uploading. Results follow: ')
               console.log(results)
               return results


Test patch:
diff --git a/test/main.js b/test/main.js
index b350515..23b37f9 100644
--- a/test/main.js
+++ b/test/main.js
@@ -17,7 +17,13 @@ awsMock.setSDK(path.resolve('node_modules/aws-sdk'))
 
 // Migrating to v3.
 const { mockClient } = require('aws-sdk-client-mock')
-const { LambdaClient, CreateFunctionCommand } = require('@aws-sdk/client-lambda')
+const {
+  LambdaClient,
+  CreateFunctionCommand,
+  GetFunctionCommand,
+  UpdateFunctionCodeCommand,
+  UpdateFunctionConfigurationCommand
+} = require('@aws-sdk/client-lambda')
 const mockLambdaClient = mockClient(LambdaClient)
 const lambdaClient = new LambdaClient({ region: 'us-east-1' })
 
@@ -166,6 +172,9 @@ describe('lib/main', function () {
     // for sdk v3
     mockLambdaClient.reset()
     mockLambdaClient.on(CreateFunctionCommand).resolves(lambdaMockSettings.createFunction)
+    mockLambdaClient.on(GetFunctionCommand).resolves(lambdaMockSettings.getFunction)
+    mockLambdaClient.on(UpdateFunctionCodeCommand).resolves(lambdaMockSettings.updateFunctionCode)
+    mockLambdaClient.on(UpdateFunctionConfigurationCommand).resolves(lambdaMockSettings.updateFunctionConfiguration)
   })
   after(() => {
     _awsRestore()
@@ -1577,7 +1586,7 @@ describe('lib/main', function () {
   describe('_uploadExisting', () => {
     it('simple test with mock', () => {
       const params = lambda._params(program, null)
-      return lambda._uploadExisting(awsLambda, params).then((results) => {
+      return lambda._uploadExisting(lambdaClient, params).then((results) => {
         assert.deepEqual(results, lambdaMockSettings.updateFunctionConfiguration)
       })
     })