Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ const {
UntagResourceCommand,
UpdateEventSourceMappingCommand,
UpdateFunctionCodeCommand,
UpdateFunctionConfigurationCommand
UpdateFunctionConfigurationCommand,
GetAliasCommand,
CreateAliasCommand,
UpdateAliasCommand
} = require('@aws-sdk/client-lambda')

const maxBufferSize = 50 * 1024 * 1024
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 })
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)]
: [])
])
}
}
Expand Down
14 changes: 7 additions & 7 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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)
})
})
})
Expand Down
Loading