From 6c5500522f9559091131ba821bf723ca83c8877c Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 20 Mar 2026 22:28:19 +0900 Subject: [PATCH] v3: fix --lambdaVersion publishes a version and creates an alias instead of appending to function name * GH-844 * GH-851 --- lib/main.js | 48 +++++++++++++++++++++++++++++++++++++++--------- test/main.js | 14 +++++++------- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2fcb8b2..4d8ce02 100644 --- a/lib/main.js +++ b/lib/main.js @@ -38,7 +38,10 @@ const { UntagResourceCommand, UpdateEventSourceMappingCommand, UpdateFunctionCodeCommand, - UpdateFunctionConfigurationCommand + UpdateFunctionConfigurationCommand, + GetAliasCommand, + CreateAliasCommand, + UpdateAliasCommand } = require('@aws-sdk/client-lambda') const maxBufferSize = 50 * 1024 * 1024 @@ -251,8 +254,7 @@ Emulate only the body of the API Gateway event. _params (program, buffer) { const params = { FunctionName: program.functionName + - (program.environment ? '-' + program.environment : '') + - (program.lambdaVersion ? '-' + program.lambdaVersion : ''), + (program.environment ? '-' + program.environment : ''), Code: {}, Handler: program.handler, Role: program.role, @@ -262,6 +264,7 @@ Emulate only the body of the API Gateway event. Timeout: Number(program.timeout), Architectures: program.architecture ? [program.architecture] : ['x86_64'], Publish: (() => { + if (program.lambdaVersion) return true if (typeof program.publish === 'boolean') { return program.publish } @@ -654,6 +657,29 @@ Emulate only the body of the API Gateway event. } } + async _alias (lambda, functionName, functionVersion, aliasName) { + try { + await lambda.send(new GetAliasCommand({ + FunctionName: functionName, + Name: aliasName + })) + return lambda.send(new UpdateAliasCommand({ + FunctionName: functionName, + Name: aliasName, + FunctionVersion: functionVersion + })) + } catch (err) { + if (err.name === 'ResourceNotFoundException') { + return lambda.send(new CreateAliasCommand({ + FunctionName: functionName, + Name: aliasName, + FunctionVersion: functionVersion + })) + } + throw err + } + } + async _uploadExisting (lambda, params) { const functionCodeParams = Object.assign({ FunctionName: params.FunctionName, @@ -683,7 +709,7 @@ Emulate only the body of the API Gateway event. delete functionConfigParams.Layers } - const updateConfigResponse = await lambda.send(new UpdateFunctionConfigurationCommand(functionConfigParams)) + await lambda.send(new UpdateFunctionConfigurationCommand(functionConfigParams)) // Wait for the `Configuration.LastUpdateStatus` to change from `InProgress` to `Successful`. const getFunction = new GetFunctionCommand({ FunctionName: params.FunctionName }) @@ -694,9 +720,7 @@ Emulate only the body of the API Gateway event. } await new Promise((resolve) => setTimeout(resolve, 3000)) } - lambda.send(new UpdateFunctionCodeCommand(functionCodeParams)) - - return updateConfigResponse + return lambda.send(new UpdateFunctionCodeCommand(functionCodeParams)) } _uploadNew (lambda, params) { @@ -1025,7 +1049,10 @@ they may not work as expected in the Lambda environment. cloudWatchLogs, program, params.FunctionName - ) + ), + ...(params.Publish && program.lambdaVersion + ? [this._alias(lambdaClient, params.FunctionName, results.Version, program.lambdaVersion)] + : []) ]) } else { const results = await this._uploadNew(lambdaClient, params) @@ -1053,7 +1080,10 @@ they may not work as expected in the Lambda environment. cloudWatchLogs, program, params.FunctionName - ) + ), + ...(params.Publish && program.lambdaVersion + ? [this._alias(lambdaClient, params.FunctionName, results.Version, program.lambdaVersion)] + : []) ]) } } diff --git a/test/main.js b/test/main.js index e36327f..2afca00 100644 --- a/test/main.js +++ b/test/main.js @@ -285,18 +285,18 @@ describe('lib/main', function () { assert.match(params.FunctionName, functionNamePattern) }) - it('appends version to original functionName', () => { + it('does not append version to functionName', () => { program.lambdaVersion = '2015-02-01' const params = lambda._params(program) - assert.equal(params.FunctionName, '___node-lambda-development-2015-02-01') + assert.equal(params.FunctionName, '___node-lambda-development') assert.match(params.FunctionName, functionNamePattern) }) - it('appends version to original functionName (value not allowed by AWS)', () => { - program.lambdaVersion = '2015.02.01' + it('sets Publish to true when lambdaVersion is set', () => { + program.lambdaVersion = 'v1' + program.publish = false const params = lambda._params(program) - assert.equal(params.FunctionName, '___node-lambda-development-2015_02_01') - assert.match(params.FunctionName, functionNamePattern) + assert.isTrue(params.Publish) }) it('appends VpcConfig to params when vpc params set', () => { @@ -1594,7 +1594,7 @@ describe('lib/main', function () { it('simple test with mock', () => { const params = lambda._params(program, null) return lambda._uploadExisting(lambdaClient, params).then((results) => { - assert.deepEqual(results, lambdaMockSettings.updateFunctionConfiguration) + assert.deepEqual(results, lambdaMockSettings.updateFunctionCode) }) }) })