Skip to content

Commit 6d2bca4

Browse files
authored
feat: support for 'ImageUri' parameter (#548)
* upgrade aws-sdk * feat: support for 'ImageUri' parameter * chore: add the configuration file to .gitignore * update yarn.lock
1 parent f0f6896 commit 6d2bca4

File tree

7 files changed

+135
-44
lines changed

7 files changed

+135
-44
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,8 @@ node_modules
3333
!.env.example
3434
.env
3535
event.json
36+
context.json
37+
deploy.env
38+
event_sources.json
3639
npm-debug.log*
3740
.lambda

bin/node-lambda

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const ENABLE_RUN_MULTIPLE_EVENTS = true
5959
const KEEP_NODE_MODULES = process.env.KEEP_NODE_MODULES || false
6060
const DOCKER_VOLUMES = process.env.DOCKER_VOLUMES || ''
6161
const AWS_TAGS = process.env.AWS_TAGS || ''
62+
const IMAGE_URI = process.env.IMAGE_URI || ''
6263

6364
program
6465
.command('deploy')
@@ -104,6 +105,7 @@ program
104105
.option('-T, --deployTimeout [DEPLOY_TIMEOUT]', 'Deploy Timeout', DEPLOY_TIMEOUT)
105106
.option('-z, --deployZipfile [DEPLOY_ZIPFILE]', 'Deploy zipfile', DEPLOY_ZIPFILE)
106107
.option('-B, --deployUseS3 [DEPLOY_USE_S3]', 'Use S3 to deploy.', DEPLOY_USE_S3)
108+
.option('-i, --imageUri [IMAGE_URI]', 'URI of a container image in the Amazon ECR registry.', IMAGE_URI)
107109
.option('-y, --proxy [PROXY]', 'Proxy server', PROXY)
108110
.option('-A, --tags [AWS_TAGS]', 'Tags as key value pairs (e.g. "tagname1=tagvalue1,tagname2=tagvalue2)"', AWS_TAGS)
109111
.option('--silent', 'Silent or quiet mode', false)

lib/main.js

+68-39
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ Emulate only the body of the API Gateway event.
199199
return program.deployUseS3 === 'true'
200200
}
201201

202+
_useECR (program) {
203+
return program.imageUri != null && program.imageUri.length > 0
204+
}
205+
202206
_params (program, buffer) {
203207
const params = {
204208
FunctionName: program.functionName +
@@ -232,14 +236,21 @@ Emulate only the body of the API Gateway event.
232236
Mode: null
233237
},
234238
Layers: [],
235-
Tags: {}
239+
Tags: {},
240+
PackageType: 'Zip'
236241
}
237242

238243
if (this._isUseS3(program)) {
239244
params.Code = {
240245
S3Bucket: null,
241246
S3Key: null
242247
}
248+
} else if (this._useECR(program)) {
249+
params.Code = { ImageUri: program.imageUri }
250+
params.PackageType = 'Image'
251+
delete params.Handler
252+
delete params.Runtime
253+
delete params.KMSKeyArn
243254
} else {
244255
params.Code = { ZipFile: buffer }
245256
}
@@ -556,39 +567,50 @@ Emulate only the body of the API Gateway event.
556567
}
557568

558569
_uploadExisting (lambda, params) {
559-
const _params = Object.assign({
570+
const functionCodeParams = Object.assign({
560571
FunctionName: params.FunctionName,
561572
Publish: params.Publish
562573
}, params.Code)
563574

564-
return new Promise((resolve, reject) => {
565-
const updateConfigRequest = lambda.updateFunctionConfiguration({
566-
FunctionName: params.FunctionName,
567-
Description: params.Description,
568-
Handler: params.Handler,
569-
MemorySize: params.MemorySize,
570-
Role: params.Role,
571-
Timeout: params.Timeout,
572-
Runtime: params.Runtime,
573-
VpcConfig: params.VpcConfig,
574-
Environment: params.Environment,
575-
KMSKeyArn: params.KMSKeyArn,
576-
DeadLetterConfig: params.DeadLetterConfig,
577-
TracingConfig: params.TracingConfig,
578-
Layers: params.Layers
579-
}, (err, configResponse) => {
580-
if (err) return reject(err)
575+
const functionConfigParams = {
576+
FunctionName: params.FunctionName,
577+
Description: params.Description,
578+
Handler: params.Handler,
579+
MemorySize: params.MemorySize,
580+
Role: params.Role,
581+
Timeout: params.Timeout,
582+
Runtime: params.Runtime,
583+
VpcConfig: params.VpcConfig,
584+
Environment: params.Environment,
585+
KMSKeyArn: params.KMSKeyArn,
586+
DeadLetterConfig: params.DeadLetterConfig,
587+
TracingConfig: params.TracingConfig,
588+
Layers: params.Layers
589+
}
590+
if (functionCodeParams.ImageUri != null) {
591+
delete functionConfigParams.Handler
592+
delete functionConfigParams.Runtime
593+
delete functionConfigParams.KMSKeyArn
594+
delete functionConfigParams.Layers
595+
}
581596

582-
const updateCodeRequest = lambda.updateFunctionCode(_params, (err) => {
597+
return new Promise((resolve, reject) => {
598+
const updateConfigRequest = lambda.updateFunctionConfiguration(
599+
functionConfigParams,
600+
(err, configResponse) => {
583601
if (err) return reject(err)
584-
resolve(configResponse)
585-
})
586602

587-
updateCodeRequest.on('retry', (response) => {
588-
console.log(response.error.message)
589-
console.log('=> Retrying')
590-
})
591-
})
603+
const updateCodeRequest = lambda.updateFunctionCode(functionCodeParams, (err) => {
604+
if (err) return reject(err)
605+
resolve(configResponse)
606+
})
607+
608+
updateCodeRequest.on('retry', (response) => {
609+
console.log(response.error.message)
610+
console.log('=> Retrying')
611+
})
612+
}
613+
)
592614

593615
updateConfigRequest.on('retry', (response) => {
594616
console.log(response.error.message)
@@ -1009,28 +1031,35 @@ they may not work as expected in the Lambda environment.
10091031
})
10101032
}
10111033

1012-
deploy (program) {
1034+
async deploy (program) {
10131035
const regions = program.region.split(',')
1014-
return this._archive(program).then((buffer) => {
1015-
console.log('=> Reading zip file to memory')
1016-
return buffer
1017-
}).then((buffer) => {
1018-
const params = this._params(program, buffer)
1036+
let buffer = null
1037+
if (!this._useECR(program)) {
1038+
try {
1039+
buffer = await this._archive(program)
1040+
console.log('=> Reading zip file to memory')
1041+
} catch (err) {
1042+
process.exitCode = 1
1043+
console.log(err)
1044+
return
1045+
}
1046+
}
10191047

1020-
return Promise.all(regions.map((region) => {
1048+
try {
1049+
const params = this._params(program, buffer)
1050+
const results = await Promise.all(regions.map((region) => {
10211051
return this._deployToRegion(
10221052
program,
10231053
params,
10241054
region,
10251055
this._isUseS3(program) ? buffer : null
10261056
)
1027-
})).then(results => {
1028-
this._printDeployResults(results, true)
1029-
})
1030-
}).catch((err) => {
1057+
}))
1058+
this._printDeployResults(results, true)
1059+
} catch (err) {
10311060
process.exitCode = 1
10321061
console.log(err)
1033-
})
1062+
}
10341063
}
10351064
}
10361065

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
},
4141
"dependencies": {
4242
"archiver": "^5.0.0",
43-
"aws-sdk": "^2.724.0",
43+
"aws-sdk": "^2.831.0",
4444
"aws-xray-sdk-core": "^3.1.0",
4545
"commander": "^7.0.0",
4646
"continuation-local-storage": "^3.2.1",

test/main.js

+42
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ describe('lib/main', function () {
205205
})
206206
})
207207

208+
describe('_useECR', () => {
209+
it('=== true', () => {
210+
assert.isTrue(lambda._useECR({ imageUri: 'xxx' }))
211+
})
212+
213+
it('=== false', () => {
214+
[
215+
{},
216+
{ imageUri: null },
217+
{ imageUri: '' }
218+
].forEach((params) => {
219+
assert.isFalse(lambda._useECR(params), params)
220+
})
221+
})
222+
})
223+
208224
describe('_params', () => {
209225
// http://docs.aws.amazon.com/lambda/latest/dg/API_CreateFunction.html#SSS-CreateFunction-request-FunctionName
210226
const functionNamePattern =
@@ -326,6 +342,32 @@ describe('lib/main', function () {
326342
})
327343
})
328344

345+
describe('PackageType: Zip|Image', () => {
346+
it('PackageType: Zip', () => {
347+
const params = lambda._params(program, 'Buffer')
348+
assert.equal(params.PackageType, 'Zip')
349+
assert.deepEqual(
350+
params.Code,
351+
{ ZipFile: 'Buffer' }
352+
)
353+
})
354+
355+
it('PackageType: Image', () => {
356+
program.imageUri = 'xxx'
357+
const params = lambda._params(program, 'Buffer')
358+
assert.equal(params.PackageType, 'Image')
359+
360+
assert.isUndefined(params.Handler)
361+
assert.isUndefined(params.Runtime)
362+
assert.isUndefined(params.KMSKeyArn)
363+
364+
assert.deepEqual(
365+
params.Code,
366+
{ ImageUri: 'xxx' }
367+
)
368+
})
369+
})
370+
329371
describe('params.Publish', () => {
330372
describe('boolean', () => {
331373
it('If true, it is set to true', () => {

yarn.lock

+16-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ aws-sdk-mock@^5.1.0:
323323
sinon "^9.0.1"
324324
traverse "^0.6.6"
325325

326-
aws-sdk@^2.637.0, aws-sdk@^2.724.0:
326+
aws-sdk@^2.637.0:
327327
version "2.724.0"
328328
resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.724.0.tgz#30e18c62930a17b5b0b5ec43f53e82996008b54d"
329329
integrity sha512-XfTPfNAkpLn/RE9ODMs8PDoZ1cI5npeS4p452mDKWUGqdr8DthdQ+6QU786KSzBRB9Bs+k5MCXI62Nv//aLFZA==
@@ -338,6 +338,21 @@ aws-sdk@^2.637.0, aws-sdk@^2.724.0:
338338
uuid "3.3.2"
339339
xml2js "0.4.19"
340340

341+
aws-sdk@^2.831.0:
342+
version "2.831.0"
343+
resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.831.0.tgz#02607cc911a2136e5aabe624c1282e821830aef2"
344+
integrity sha512-lrOjbGFpjk2xpESyUx2PGsTZgptCy5xycZazPeakNbFO19cOoxjHx3xyxOHsMCYb3pQwns35UvChQT60B4u6cw==
345+
dependencies:
346+
buffer "4.9.2"
347+
events "1.1.1"
348+
ieee754 "1.1.13"
349+
jmespath "0.15.0"
350+
querystring "0.2.0"
351+
sax "1.2.1"
352+
url "0.10.3"
353+
uuid "3.3.2"
354+
xml2js "0.4.19"
355+
341356
aws-xray-sdk-core@^3.1.0:
342357
version "3.1.0"
343358
resolved "https://registry.yarnpkg.com/aws-xray-sdk-core/-/aws-xray-sdk-core-3.1.0.tgz#a68c79808a92c2752a9b4ed12a54b6439539546d"

0 commit comments

Comments
 (0)