Repository: motdotla/node-lambda
Pull request #759 (2024-10-27 02:09:05)

sdk-v3: Migrate CreateFunction to v3 with test
GH-641

abetomo: The test fails on Windows, but just ignore it for now.
I will fix it later.

Patch:
diff --git a/lib/main.js b/lib/main.js
index 70412f3..4860ca1 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -28,7 +28,6 @@ const { createNamespace } = require('continuation-local-storage')
  * https://docs.aws.amazon.com/sdkref/latest/guide/feature-retry-behavior.html
  */
 const { LambdaClient, CreateFunctionCommand } = require('@aws-sdk/client-lambda')
-const lambdaClient = new LambdaClient()
 
 const maxBufferSize = 50 * 1024 * 1024
 
@@ -699,22 +698,7 @@ Emulate only the body of the API Gateway event.
   }
 
   _uploadNew (lambda, params) {
-    if (process.env.USE_AWS_SDK_V3) {
-      console.log('DEBUG: Use AWS SDK V3: CreateFunctionCommand()')
-      const command = new CreateFunctionCommand(params)
-      return lambdaClient.send(command)
-    }
-
-    return new Promise((resolve, reject) => {
-      const request = lambda.createFunction(params, (err, data) => {
-        if (err) return reject(err)
-        resolve(data)
-      })
-      request.on('retry', (response) => {
-        console.log(response.error.message)
-        console.log('=> Retrying')
-      })
-    })
+    return lambda.send(new CreateFunctionCommand(params))
   }
 
   _readArchive (program) {
@@ -991,6 +975,7 @@ they may not work as expected in the Lambda environment.
   }
 
   _deployToRegion (program, params, region, buffer) {
+    // sdk v3 todo: Migration of aws.updateConfig.
     aws.updateConfig(program, region)
 
     console.log('=> Reading event source file to memory')
@@ -1012,10 +997,13 @@ they may not work as expected in the Lambda environment.
       }
       console.log(params)
 
+      // Migrating to v3.
       const lambda = new aws.sdk.Lambda({
         region,
         apiVersion: '2015-03-31'
       })
+      const lambdaClient = new LambdaClient({ region })
+
       const scheduleEvents = new ScheduleEvents(aws.sdk, region)
       const s3Events = new S3Events(aws.sdk, region)
       const cloudWatchLogs = new CloudWatchLogs(aws.sdk, region)
@@ -1069,7 +1057,7 @@ they may not work as expected in the Lambda environment.
           throw err
         }
         // Function does not exist
-        return this._uploadNew(lambda, params).then((results) => {
+        return this._uploadNew(lambdaClient, params).then((results) => {
           console.log('=> Done uploading. Results follow: ')
           console.log(results)
 


Test patch:
diff --git a/test/main.js b/test/main.js
index 647b95d..af77e3b 100644
--- a/test/main.js
+++ b/test/main.js
@@ -11,9 +11,15 @@ let assert
 import('chai').then(chai => {
   assert = chai.assert
 })
+const sinon = require('sinon')
+
 const awsMock = require('aws-sdk-mock')
 awsMock.setSDK(path.resolve('node_modules/aws-sdk'))
 
+// Migrating to v3.
+const { LambdaClient } = require('@aws-sdk/client-lambda')
+const lambdaClient = new LambdaClient({ region: 'us-east-1' })
+
 const originalProgram = {
   packageManager: 'npm',
   environment: 'development',
@@ -22,7 +28,7 @@ const originalProgram = {
   sessionToken: 'token',
   functionName: '___node-lambda',
   handler: 'index.handler',
-  role: 'some:arn:aws:iam::role',
+  role: 'arn:aws:iam::999999999999:role/test',
   memorySize: 128,
   timeout: 3,
   description: '',
@@ -155,8 +161,15 @@ describe('lib/main', function () {
       return
     }
     execFileSync('npm', ['ci'], { cwd: sourceDirectoryForTest })
+
+    // for sdk v3
+    const stub = sinon.stub(lambdaClient, 'send')
+    stub.returns(lambdaMockSettings.createFunction)
+  })
+  after(() => {
+    _awsRestore()
+    sinon.restore() // for sdk v3
   })
-  after(() => _awsRestore())
 
   beforeEach(() => {
     program = Object.assign({}, originalProgram) // clone
@@ -1555,7 +1568,7 @@ describe('lib/main', function () {
   describe('_uploadNew', () => {
     it('simple test with mock', () => {
       const params = lambda._params(program, null)
-      return lambda._uploadNew(awsLambda, params, (results) => {
+      return lambda._uploadNew(lambdaClient, params, (results) => {
         assert.deepEqual(results, lambdaMockSettings.createFunction)
       })
     })