Repository: motdotla/node-lambda
Pull request #775 (2025-01-06 13:06:12)

sdk-v3: Use sdk-v3 with _updateTags()
GH-641


Patch:
diff --git a/lib/main.js b/lib/main.js
index adc9246..8727b3e 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -34,6 +34,9 @@ const {
   DeleteEventSourceMappingCommand,
   GetFunctionCommand,
   ListEventSourceMappingsCommand,
+  ListTagsCommand,
+  TagResourceCommand,
+  UntagResourceCommand,
   UpdateEventSourceMappingCommand,
   UpdateFunctionCodeCommand,
   UpdateFunctionConfigurationCommand
@@ -856,21 +859,17 @@ they may not work as expected in the Lambda environment.
     }))
   }
 
-  _updateTags (lambda, functionArn, tags) {
+  async _updateTags (lambda, functionArn, tags) {
     if (!tags || Object.keys(tags).length <= 0) {
-      return Promise.resolve([])
-    } else {
-      return lambda.listTags({ Resource: functionArn }).promise()
-        .then(data => {
-          const keys = Object.keys(data.Tags)
-          return keys && keys.length > 0
-            ? lambda.untagResource({ Resource: functionArn, TagKeys: keys }).promise()
-            : Promise.resolve()
-        })
-        .then(() => {
-          return lambda.tagResource({ Resource: functionArn, Tags: tags }).promise()
-        })
+      return []
+    }
+
+    const data = await lambda.send(new ListTagsCommand({ Resource: functionArn }))
+    const keys = Object.keys(data.Tags)
+    if (keys && keys.length > 0) {
+      await lambda.send(new UntagResourceCommand({ Resource: functionArn, TagKeys: keys }))
     }
+    return lambda.send(new TagResourceCommand({ Resource: functionArn, Tags: tags }))
   }
 
   _updateScheduleEvents (scheduleEvents, functionArn, scheduleList) {
@@ -1016,7 +1015,7 @@ they may not work as expected in the Lambda environment.
                   eventSourceList.S3Events
                 ),
                 this._updateTags(
-                  lambda,
+                  lambdaClient,
                   results.FunctionArn,
                   params.Tags)
               ])


Test patch:
diff --git a/test/main.js b/test/main.js
index ae29500..d89f72b 100644
--- a/test/main.js
+++ b/test/main.js
@@ -24,6 +24,9 @@ const {
   DeleteEventSourceMappingCommand,
   GetFunctionCommand,
   ListEventSourceMappingsCommand,
+  ListTagsCommand,
+  TagResourceCommand,
+  UntagResourceCommand,
   UpdateEventSourceMappingCommand,
   UpdateFunctionCodeCommand,
   UpdateFunctionConfigurationCommand
@@ -162,10 +165,8 @@ describe('lib/main', function () {
   }
 
   let aws = null // mock
-  let awsLambda = null // mock
   before(() => {
     aws = _mockSetting()
-    awsLambda = new aws.Lambda({ apiVersion: '2015-03-31' })
 
     if (process.platform === 'win32') {
       execFileSync('cmd.exe', ['/c', 'npm', 'ci'], { cwd: sourceDirectoryForTest })
@@ -180,6 +181,9 @@ describe('lib/main', function () {
     mockLambdaClient.on(DeleteEventSourceMappingCommand).resolves(lambdaMockSettings.deleteEventSourceMapping)
     mockLambdaClient.on(GetFunctionCommand).resolves(lambdaMockSettings.getFunction)
     mockLambdaClient.on(ListEventSourceMappingsCommand).resolves(lambdaMockSettings.listEventSourceMappings)
+    mockLambdaClient.on(ListTagsCommand).resolves(lambdaMockSettings.listTags)
+    mockLambdaClient.on(TagResourceCommand).resolves(lambdaMockSettings.tagResource)
+    mockLambdaClient.on(UntagResourceCommand).resolves(lambdaMockSettings.untagResource)
     mockLambdaClient.on(UpdateEventSourceMappingCommand).resolves(lambdaMockSettings.updateEventSourceMapping)
     mockLambdaClient.on(UpdateFunctionCodeCommand).resolves(lambdaMockSettings.updateFunctionCode)
     mockLambdaClient.on(UpdateFunctionConfigurationCommand).resolves(lambdaMockSettings.updateFunctionConfiguration)
@@ -1682,7 +1686,7 @@ describe('lib/main', function () {
   describe('Lambda.prototype._updateTags()', () => {
     it('simple test with mock', () => {
       return lambda._updateTags(
-        awsLambda,
+        lambdaClient,
         'arn:aws:lambda:eu-central-1:1234567:function:test',
         { tagKey: 'tagValue' }).then((result) => {
         assert.deepEqual(