Skip to content

Commit 6c55005

Browse files
committed
v3: fix --lambdaVersion publishes a version and creates an alias instead of appending to function name
* GH-844 * GH-851
1 parent f95cd44 commit 6c55005

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

lib/main.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ const {
3838
UntagResourceCommand,
3939
UpdateEventSourceMappingCommand,
4040
UpdateFunctionCodeCommand,
41-
UpdateFunctionConfigurationCommand
41+
UpdateFunctionConfigurationCommand,
42+
GetAliasCommand,
43+
CreateAliasCommand,
44+
UpdateAliasCommand
4245
} = require('@aws-sdk/client-lambda')
4346

4447
const maxBufferSize = 50 * 1024 * 1024
@@ -251,8 +254,7 @@ Emulate only the body of the API Gateway event.
251254
_params (program, buffer) {
252255
const params = {
253256
FunctionName: program.functionName +
254-
(program.environment ? '-' + program.environment : '') +
255-
(program.lambdaVersion ? '-' + program.lambdaVersion : ''),
257+
(program.environment ? '-' + program.environment : ''),
256258
Code: {},
257259
Handler: program.handler,
258260
Role: program.role,
@@ -262,6 +264,7 @@ Emulate only the body of the API Gateway event.
262264
Timeout: Number(program.timeout),
263265
Architectures: program.architecture ? [program.architecture] : ['x86_64'],
264266
Publish: (() => {
267+
if (program.lambdaVersion) return true
265268
if (typeof program.publish === 'boolean') {
266269
return program.publish
267270
}
@@ -654,6 +657,29 @@ Emulate only the body of the API Gateway event.
654657
}
655658
}
656659

660+
async _alias (lambda, functionName, functionVersion, aliasName) {
661+
try {
662+
await lambda.send(new GetAliasCommand({
663+
FunctionName: functionName,
664+
Name: aliasName
665+
}))
666+
return lambda.send(new UpdateAliasCommand({
667+
FunctionName: functionName,
668+
Name: aliasName,
669+
FunctionVersion: functionVersion
670+
}))
671+
} catch (err) {
672+
if (err.name === 'ResourceNotFoundException') {
673+
return lambda.send(new CreateAliasCommand({
674+
FunctionName: functionName,
675+
Name: aliasName,
676+
FunctionVersion: functionVersion
677+
}))
678+
}
679+
throw err
680+
}
681+
}
682+
657683
async _uploadExisting (lambda, params) {
658684
const functionCodeParams = Object.assign({
659685
FunctionName: params.FunctionName,
@@ -683,7 +709,7 @@ Emulate only the body of the API Gateway event.
683709
delete functionConfigParams.Layers
684710
}
685711

686-
const updateConfigResponse = await lambda.send(new UpdateFunctionConfigurationCommand(functionConfigParams))
712+
await lambda.send(new UpdateFunctionConfigurationCommand(functionConfigParams))
687713

688714
// Wait for the `Configuration.LastUpdateStatus` to change from `InProgress` to `Successful`.
689715
const getFunction = new GetFunctionCommand({ FunctionName: params.FunctionName })
@@ -694,9 +720,7 @@ Emulate only the body of the API Gateway event.
694720
}
695721
await new Promise((resolve) => setTimeout(resolve, 3000))
696722
}
697-
lambda.send(new UpdateFunctionCodeCommand(functionCodeParams))
698-
699-
return updateConfigResponse
723+
return lambda.send(new UpdateFunctionCodeCommand(functionCodeParams))
700724
}
701725

702726
_uploadNew (lambda, params) {
@@ -1025,7 +1049,10 @@ they may not work as expected in the Lambda environment.
10251049
cloudWatchLogs,
10261050
program,
10271051
params.FunctionName
1028-
)
1052+
),
1053+
...(params.Publish && program.lambdaVersion
1054+
? [this._alias(lambdaClient, params.FunctionName, results.Version, program.lambdaVersion)]
1055+
: [])
10291056
])
10301057
} else {
10311058
const results = await this._uploadNew(lambdaClient, params)
@@ -1053,7 +1080,10 @@ they may not work as expected in the Lambda environment.
10531080
cloudWatchLogs,
10541081
program,
10551082
params.FunctionName
1056-
)
1083+
),
1084+
...(params.Publish && program.lambdaVersion
1085+
? [this._alias(lambdaClient, params.FunctionName, results.Version, program.lambdaVersion)]
1086+
: [])
10571087
])
10581088
}
10591089
}

test/main.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,18 +285,18 @@ describe('lib/main', function () {
285285
assert.match(params.FunctionName, functionNamePattern)
286286
})
287287

288-
it('appends version to original functionName', () => {
288+
it('does not append version to functionName', () => {
289289
program.lambdaVersion = '2015-02-01'
290290
const params = lambda._params(program)
291-
assert.equal(params.FunctionName, '___node-lambda-development-2015-02-01')
291+
assert.equal(params.FunctionName, '___node-lambda-development')
292292
assert.match(params.FunctionName, functionNamePattern)
293293
})
294294

295-
it('appends version to original functionName (value not allowed by AWS)', () => {
296-
program.lambdaVersion = '2015.02.01'
295+
it('sets Publish to true when lambdaVersion is set', () => {
296+
program.lambdaVersion = 'v1'
297+
program.publish = false
297298
const params = lambda._params(program)
298-
assert.equal(params.FunctionName, '___node-lambda-development-2015_02_01')
299-
assert.match(params.FunctionName, functionNamePattern)
299+
assert.isTrue(params.Publish)
300300
})
301301

302302
it('appends VpcConfig to params when vpc params set', () => {
@@ -1594,7 +1594,7 @@ describe('lib/main', function () {
15941594
it('simple test with mock', () => {
15951595
const params = lambda._params(program, null)
15961596
return lambda._uploadExisting(lambdaClient, params).then((results) => {
1597-
assert.deepEqual(results, lambdaMockSettings.updateFunctionConfiguration)
1597+
assert.deepEqual(results, lambdaMockSettings.updateFunctionCode)
15981598
})
15991599
})
16001600
})

0 commit comments

Comments
 (0)