Skip to content

Commit 60d6c1c

Browse files
authored
Merge pull request #844 from abetomo/support-alias
Support the alias
2 parents c38d6da + 70fbd14 commit 60d6c1c

File tree

2 files changed

+80
-8
lines changed

2 files changed

+80
-8
lines changed

lib/main.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,28 @@ Emulate only the body of the API Gateway event.
632632
}
633633
}
634634

635+
_alias (lambda, functionName, functionVersion, aliasName) {
636+
return lambda.getAlias({
637+
FunctionName: functionName,
638+
Name: aliasName
639+
}).promise().then(() => {
640+
return lambda.updateAlias({
641+
FunctionName: functionName,
642+
Name: aliasName,
643+
FunctionVersion: functionVersion
644+
}).promise()
645+
}).catch((err) => {
646+
if (err.code === 'ResourceNotFoundException') {
647+
return lambda.createAlias({
648+
FunctionName: functionName,
649+
Name: aliasName,
650+
FunctionVersion: functionVersion
651+
}).promise()
652+
}
653+
throw err
654+
})
655+
}
656+
635657
async _uploadExisting (lambda, params) {
636658
const functionCodeParams = Object.assign({
637659
FunctionName: params.FunctionName,
@@ -666,7 +688,7 @@ Emulate only the body of the API Gateway event.
666688
console.log(response.error.message)
667689
console.log('=> Retrying')
668690
})
669-
const updateConfigResponse = await updateConfigRequest.promise()
691+
await updateConfigRequest.promise()
670692

671693
// Wait for the `Configuration.LastUpdateStatus` to change from `InProgress` to `Successful`.
672694
for (let i = 0; i < 10; i++) {
@@ -682,9 +704,9 @@ Emulate only the body of the API Gateway event.
682704
console.log(response.error.message)
683705
console.log('=> Retrying')
684706
})
685-
await updateCodeRequest.promise()
707+
const updateCodeResponse = await updateCodeRequest.promise()
686708

687-
return updateConfigResponse
709+
return updateCodeResponse
688710
}
689711

690712
_uploadNew (lambda, params) {
@@ -1044,7 +1066,10 @@ they may not work as expected in the Lambda environment.
10441066
cloudWatchLogs,
10451067
program,
10461068
params.FunctionName
1047-
)
1069+
),
1070+
...(params.Publish && program.lambdaVersion
1071+
? [this._alias(lambda, params.FunctionName, results.Version, program.lambdaVersion)]
1072+
: [])
10481073
])
10491074
} else {
10501075
const results = await this._uploadNew(lambda, params)
@@ -1072,7 +1097,10 @@ they may not work as expected in the Lambda environment.
10721097
cloudWatchLogs,
10731098
program,
10741099
params.FunctionName
1075-
)
1100+
),
1101+
...(params.Publish && program.lambdaVersion
1102+
? [this._alias(lambda, params.FunctionName, results.Version, program.lambdaVersion)]
1103+
: [])
10761104
])
10771105
}
10781106
}

test/main.js

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ const lambdaMockSettings = {
8585
},
8686
updateFunctionCode: {
8787
FunctionArn: 'Lambda.updateFunctionCode.mock.FunctionArn',
88-
FunctionName: 'Lambda.updateFunctionCode.mock.FunctionName'
88+
FunctionName: 'Lambda.updateFunctionCode.mock.FunctionName',
89+
Version: '1'
8990
},
9091
updateFunctionConfiguration: {
9192
FunctionArn: 'Lambda.updateFunctionConfiguration.mock.FunctionArn',
@@ -107,7 +108,19 @@ const lambdaMockSettings = {
107108
Tags: { tag1: 'key1' }
108109
},
109110
untagResource: {},
110-
tagResource: {}
111+
tagResource: {},
112+
getAlias: {
113+
FunctionArn: 'Lambda.getAlias.mock.FunctionArn',
114+
Name: 'mockAlias'
115+
},
116+
createAlias: {
117+
FunctionArn: 'Lambda.createAlias.mock.FunctionArn',
118+
Name: 'mockAlias'
119+
},
120+
updateAlias: {
121+
FunctionArn: 'Lambda.updateAlias.mock.FunctionArn',
122+
Name: 'mockAlias'
123+
}
111124
}
112125

113126
const _mockSetting = () => {
@@ -1575,7 +1588,27 @@ describe('lib/main', function () {
15751588
it('simple test with mock', () => {
15761589
const params = lambda._params(program, null)
15771590
return lambda._uploadExisting(awsLambda, params).then((results) => {
1578-
assert.deepEqual(results, lambdaMockSettings.updateFunctionConfiguration)
1591+
assert.deepEqual(results, lambdaMockSettings.updateFunctionCode)
1592+
})
1593+
})
1594+
})
1595+
1596+
describe('_alias', () => {
1597+
it('updates alias when it already exists', () => {
1598+
return lambda._alias(awsLambda, 'myFunc', '1', 'mockAlias').then((results) => {
1599+
assert.deepEqual(results, lambdaMockSettings.updateAlias)
1600+
})
1601+
})
1602+
1603+
it('creates alias when it does not exist', () => {
1604+
const err = new Error('ResourceNotFoundException')
1605+
err.code = 'ResourceNotFoundException'
1606+
const mockLambda = {
1607+
getAlias: () => ({ promise: () => Promise.reject(err) }),
1608+
createAlias: () => ({ promise: () => Promise.resolve(lambdaMockSettings.createAlias) })
1609+
}
1610+
return lambda._alias(mockLambda, 'myFunc', '1', 'mockAlias').then((results) => {
1611+
assert.deepEqual(results, lambdaMockSettings.createAlias)
15791612
})
15801613
})
15811614
})
@@ -1648,6 +1681,17 @@ describe('lib/main', function () {
16481681
)
16491682
})
16501683
})
1684+
1685+
it('creates alias when publish and lambdaVersion are set', () => {
1686+
program.publish = true
1687+
program.lambdaVersion = 'v1'
1688+
const params = lambda._params(program, null)
1689+
params.Publish = true
1690+
return lambda._deployToRegion(program, params, 'us-east-1').then((result) => {
1691+
assert.equal(result.length, 4)
1692+
assert.deepEqual(result[3], lambdaMockSettings.updateAlias)
1693+
})
1694+
})
16511695
})
16521696

16531697
describe('Lambda.prototype.deploy()', () => {

0 commit comments

Comments
 (0)