From a90fc40912990a028eb69f5d6a8f3b6f4d0f70ad Mon Sep 17 00:00:00 2001 From: Steven White Date: Wed, 8 Feb 2017 17:04:22 -0500 Subject: [PATCH 1/8] Add -C, --dockerContainer option for installing dependencies --- bin/node-lambda | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bin/node-lambda b/bin/node-lambda index 9044c896..3fc2804b 100755 --- a/bin/node-lambda +++ b/bin/node-lambda @@ -34,6 +34,7 @@ var EVENT_FILE = process.env.EVENT_FILE || 'event.json'; var PACKAGE_DIRECTORY = process.env.PACKAGE_DIRECTORY; var CONTEXT_FILE = process.env.CONTEXT_FILE || 'context.json'; var PREBUILT_DIRECTORY = process.env.PREBUILT_DIRECTORY || ''; +var DOCKER_CONTAINER = process.env.DOCKER_CONTAINER || ''; program .command('deploy') @@ -59,6 +60,7 @@ program .option('-g, --vpcSecurityGroups [' + AWS_VPC_SECURITY_GROUPS + ']', 'Lambda VPC Security Group', AWS_VPC_SECURITY_GROUPS) .option('-A, --packageDirectory [' + PACKAGE_DIRECTORY + ']', 'Local Package Directory', PACKAGE_DIRECTORY) + .option('-C, --dockerContainer [' + DOCKER_CONTAINER + ']', 'Docker container', DOCKER_CONTAINER) .option('-f, --configFile [' + CONFIG_FILE + ']', 'Path to file holding secret environment variables (e.g. "deploy.env")', CONFIG_FILE) .option('-x, --excludeGlobs [' + EXCLUDE_GLOBS + ']', @@ -73,6 +75,7 @@ program .alias('zip') .description('Create zipped package for Amazon Lambda deployment') .option('-A, --packageDirectory [' + PACKAGE_DIRECTORY + ']', 'Local Package Directory', PACKAGE_DIRECTORY) + .option('-C, --dockerContainer [' + DOCKER_CONTAINER + ']', 'Docker container', DOCKER_CONTAINER) .option('-n, --functionName [' + AWS_FUNCTION_NAME + ']', 'Lambda FunctionName', AWS_FUNCTION_NAME) .option('-H, --handler [' + AWS_HANDLER + ']', 'Lambda Handler {index.handler}', AWS_HANDLER) .option('-e, --environment [' + AWS_ENVIRONMENT + ']', 'Choose environment {dev, staging, production}', From 527caa56e50eedf7644484ab9bcc422f425d6946 Mon Sep 17 00:00:00 2001 From: Steven White Date: Wed, 8 Feb 2017 17:05:11 -0500 Subject: [PATCH 2/8] Invoke docker container if provided; move tmp dir to local path --- lib/main.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 3f43f941..d0c6a247 100644 --- a/lib/main.js +++ b/lib/main.js @@ -195,7 +195,10 @@ Lambda.prototype._rsync = function (program, src, dest, excludeNodeModules, call }; Lambda.prototype._npmInstall = function (program, codeDirectory, callback) { - exec('npm -s install --production --prefix ' + codeDirectory, function (err) { + var command = program.dockerContainer ? + 'docker run --rm -v ' + codeDirectory + ':/var/task ' + program.dockerContainer + ' npm -s install --production' : + 'npm -s install --production --prefix ' + codeDirectory; + exec(command, function (err) { if (err) { return callback(err); } @@ -270,7 +273,7 @@ Lambda.prototype._nativeZip = function (program, codeDirectory, callback) { Lambda.prototype._codeDirectory = function (program) { var epoch_time = +new Date(); - return path.join(os.tmpDir(), program.functionName + '-' + epoch_time); + return path.resolve('.', '.lambda'); }; Lambda.prototype._cleanDirectory = function (codeDirectory, callback) { @@ -360,7 +363,7 @@ Lambda.prototype._buildAndArchive = function (program, archive_callback) { // Warn if not building on 64-bit linux var arch = process.platform + '.' + process.arch; - if (arch !== 'linux.x64') { + if (arch !== 'linux.x64' && !program.dockerContainer) { console.warn('Warning!!! You are building on a platform that is not 64-bit Linux (%s). ' + 'If any of your Node dependencies include C-extensions, they may not work as expected in the ' + 'Lambda environment.\n\n', arch); From 248daf28ca2f5f24516664cf631733f2629c1d34 Mon Sep 17 00:00:00 2001 From: Steven White Date: Wed, 8 Feb 2017 17:07:08 -0500 Subject: [PATCH 3/8] Ignore .lambda --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 29ade9b7..cf155a2f 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,4 @@ node_modules .env event.json npm-debug.log* +.lambda From 627dec93daf4c4dccc3193b9c50243fc9524233e Mon Sep 17 00:00:00 2001 From: Steven White Date: Wed, 8 Feb 2017 17:11:40 -0500 Subject: [PATCH 4/8] doc update --- README.md | 2 ++ bin/node-lambda | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6f4d8653..3fd7c865 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,7 @@ $ node-lambda package --help -h, --help output usage information -A, --packageDirectory [build] Local Package Directory + -C, --dockerContainer [] Docker container for npm install -n, --functionName [node-lambda] Lambda FunctionName -H, --handler [index.handler] Lambda Handler {index.handler} -e, --environment [staging] Choose environment {development, staging, production} @@ -127,6 +128,7 @@ $ node-lambda deploy --help -b, --vpcSubnets [] VPC Subnet ID(s, comma separated list) for your Lambda Function, when using this, the below param is also required -g, --vpcSecurityGroups [] VPC Security Group ID(s, comma separated list) for your Lambda Function, when using this, the above param is also required -A, --packageDirectory [] Local package directory + -C, --dockerContainer [] Docker container for npm install -x, --excludeGlobs [] Add a space separated list of file(type)s to ignore (e.g. "*.json .env") -D, --prebuiltDirectory [] Prebuilt directory ``` diff --git a/bin/node-lambda b/bin/node-lambda index 3fc2804b..bec2bd1a 100755 --- a/bin/node-lambda +++ b/bin/node-lambda @@ -60,7 +60,7 @@ program .option('-g, --vpcSecurityGroups [' + AWS_VPC_SECURITY_GROUPS + ']', 'Lambda VPC Security Group', AWS_VPC_SECURITY_GROUPS) .option('-A, --packageDirectory [' + PACKAGE_DIRECTORY + ']', 'Local Package Directory', PACKAGE_DIRECTORY) - .option('-C, --dockerContainer [' + DOCKER_CONTAINER + ']', 'Docker container', DOCKER_CONTAINER) + .option('-C, --dockerContainer [' + DOCKER_CONTAINER + ']', 'Docker container for npm install', DOCKER_CONTAINER) .option('-f, --configFile [' + CONFIG_FILE + ']', 'Path to file holding secret environment variables (e.g. "deploy.env")', CONFIG_FILE) .option('-x, --excludeGlobs [' + EXCLUDE_GLOBS + ']', @@ -75,7 +75,7 @@ program .alias('zip') .description('Create zipped package for Amazon Lambda deployment') .option('-A, --packageDirectory [' + PACKAGE_DIRECTORY + ']', 'Local Package Directory', PACKAGE_DIRECTORY) - .option('-C, --dockerContainer [' + DOCKER_CONTAINER + ']', 'Docker container', DOCKER_CONTAINER) + .option('-C, --dockerContainer [' + DOCKER_CONTAINER + ']', 'Docker container for npm install', DOCKER_CONTAINER) .option('-n, --functionName [' + AWS_FUNCTION_NAME + ']', 'Lambda FunctionName', AWS_FUNCTION_NAME) .option('-H, --handler [' + AWS_HANDLER + ']', 'Lambda Handler {index.handler}', AWS_HANDLER) .option('-e, --environment [' + AWS_ENVIRONMENT + ']', 'Choose environment {dev, staging, production}', From 4e4cd35f48bc68f09a324435e7a8ee4ccd525c23 Mon Sep 17 00:00:00 2001 From: Steven White Date: Wed, 8 Feb 2017 17:14:01 -0500 Subject: [PATCH 5/8] Add note about ignoring .lambda --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3fd7c865..8c7de240 100644 --- a/README.md +++ b/README.md @@ -48,10 +48,10 @@ $ node-lambda setup --help -h, --help output usage information ``` -After running setup, it's a good idea to gitignore the generated `event.json` and `.env` files. +After running setup, it's a good idea to gitignore the generated `event.json` and `.env` files, as well as `.lambda`. ``` -echo -e ".env\ndeploy.env\nevent.json" >> .gitignore +echo -e ".env\ndeploy.env\nevent.json\n.lambda" >> .gitignore ``` #### run From cf595b61250b5d6df22c4a89d34cc5c5ed1f361c Mon Sep 17 00:00:00 2001 From: Steven White Date: Wed, 26 Apr 2017 23:12:31 -0400 Subject: [PATCH 6/8] Remove readme section on tmpdir --- README.md | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/README.md b/README.md index 86df9d0b..f6e0f6db 100644 --- a/README.md +++ b/README.md @@ -166,16 +166,6 @@ The `--prebuiltDirectory` flag is useful for working with Webpack for example. I ## Handling `npm link` and Dependencies With Local Paths Perhaps the easiest way to handle these cases is to bundle the code using Webpack and use the `--prebuiltDirectory` flag to package the output for deployment. -## Running `node-lambda` as an NPM script -Strangely, NPM overwrites the TMPDIR environment variable (and therefore the result of `os.tmpDir()`) to the current working directory. This means when running node-lambda deploy as a NPM script in `package.json`, it fails on the rsync step as the destination directory exists in the folder you're synchronising (causing heaps of file has vanished: type errors). - -You can resolve this by explicitly setting the `TMPDIR` variable as you deploy, something like: -``` -"scripts": { - "deploy-stage": "TMPDIR=/tmp node-lambda deploy" -} -``` - ## Other AWS Lambda Tools Projects + [lambdaws](https://github.com/mentum/lambdaws) From fbbed6fad61dd40e6e18a9e032538b2717864af2 Mon Sep 17 00:00:00 2001 From: Steven White Date: Wed, 26 Apr 2017 23:16:20 -0400 Subject: [PATCH 7/8] Add .lambda to default excludes --- lib/main.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/main.js b/lib/main.js index 4683704f..b1fafcaf 100644 --- a/lib/main.js +++ b/lib/main.js @@ -195,6 +195,7 @@ Lambda.prototype._fileCopy = function (program, src, dest, excludeNodeModules, c '.git*', '*.swp', '.editorconfig', + '.lambda', 'deploy.env', '*.log', '/build/' @@ -234,7 +235,7 @@ Lambda.prototype._fileCopy = function (program, src, dest, excludeNodeModules, c }; Lambda.prototype._rsync = function (program, src, dest, excludeNodeModules, callback) { - var excludes = ['.git*', '*.swp', '.editorconfig', 'deploy.env', '*.log', '/build/'], + var excludes = ['.git*', '*.swp', '.editorconfig', '.lambda', 'deploy.env', '*.log', '/build/'], excludeGlobs = []; if (program.excludeGlobs) { excludeGlobs = program.excludeGlobs.split(' '); From 4454da46221eb137c63620c6a662b83b7cd50adf Mon Sep 17 00:00:00 2001 From: Steven White Date: Thu, 27 Apr 2017 13:03:30 -0400 Subject: [PATCH 8/8] Use correct term, docker image, to prevent confusion --- README.md | 4 ++-- bin/node-lambda | 6 +++--- lib/main.js | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f6e0f6db..a0a307e0 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ $ node-lambda package --help -h, --help output usage information -A, --packageDirectory [build] Local Package Directory - -C, --dockerContainer [] Docker container for npm install + -I, --dockerImage [] Docker image for npm install -n, --functionName [node-lambda] Lambda FunctionName -H, --handler [index.handler] Lambda Handler {index.handler} -e, --environment [staging] Choose environment {development, staging, production} @@ -130,7 +130,7 @@ $ node-lambda deploy --help -Q, --deadLetterConfigTargetArn [] Lambda DLQ resource -T, --tracingConfig [] Lambda tracing settings -A, --packageDirectory [] Local package directory - -C, --dockerContainer [] Docker container for npm install + -I, --dockerImage [] Docker image for npm install -S, --eventSourceFile [event_sources.json] Path to file holding event source mapping variables (e.g. "event_sources.json") -x, --excludeGlobs [] Add a space separated list of file(type)s to ignore (e.g. "*.json .env") -D, --prebuiltDirectory [] Prebuilt directory diff --git a/bin/node-lambda b/bin/node-lambda index 67d65ae9..20b44821 100755 --- a/bin/node-lambda +++ b/bin/node-lambda @@ -70,7 +70,7 @@ var EVENT_FILE = process.env.EVENT_FILE || 'event.json'; var PACKAGE_DIRECTORY = process.env.PACKAGE_DIRECTORY; var CONTEXT_FILE = process.env.CONTEXT_FILE || 'context.json'; var PREBUILT_DIRECTORY = process.env.PREBUILT_DIRECTORY || ''; -var DOCKER_CONTAINER = process.env.DOCKER_CONTAINER || ''; +var DOCKER_IMAGE = process.env.DOCKER_IMAGE || ''; var DEPLOY_ZIPFILE = process.env.DEPLOY_ZIPFILE || ''; var AWS_DLQ_TARGET_ARN = (function() { // You can clear the setting by passing an empty string @@ -108,7 +108,7 @@ program .option('-T, --tracingConfig [' + AWS_TRACING_CONFIG + ']', 'Lambda tracing settings', AWS_TRACING_CONFIG) .option('-A, --packageDirectory [' + PACKAGE_DIRECTORY + ']', 'Local Package Directory', PACKAGE_DIRECTORY) - .option('-C, --dockerContainer [' + DOCKER_CONTAINER + ']', 'Docker container for npm install', DOCKER_CONTAINER) + .option('-I, --dockerImage [' + DOCKER_IMAGE + ']', 'Docker image for npm install', DOCKER_IMAGE) .option('-f, --configFile [' + CONFIG_FILE + ']', 'Path to file holding secret environment variables (e.g. "deploy.env")', CONFIG_FILE) .option('-S, --eventSourceFile [' + EVENT_SOURCE_FILE + ']', @@ -126,7 +126,7 @@ program .alias('zip') .description('Create zipped package for Amazon Lambda deployment') .option('-A, --packageDirectory [' + PACKAGE_DIRECTORY + ']', 'Local Package Directory', PACKAGE_DIRECTORY) - .option('-C, --dockerContainer [' + DOCKER_CONTAINER + ']', 'Docker container for npm install', DOCKER_CONTAINER) + .option('-I, --dockerImage [' + DOCKER_IMAGE + ']', 'Docker image for npm install', DOCKER_IMAGE) .option('-n, --functionName [' + AWS_FUNCTION_NAME + ']', 'Lambda FunctionName', AWS_FUNCTION_NAME) .option('-H, --handler [' + AWS_HANDLER + ']', 'Lambda Handler {index.handler}', AWS_HANDLER) .option('-e, --environment [' + AWS_ENVIRONMENT + ']', 'Choose environment {dev, staging, production}', diff --git a/lib/main.js b/lib/main.js index b1fafcaf..8c14edea 100644 --- a/lib/main.js +++ b/lib/main.js @@ -271,8 +271,8 @@ Lambda.prototype._rsync = function (program, src, dest, excludeNodeModules, call }; Lambda.prototype._npmInstall = function (program, codeDirectory, callback) { - var command = program.dockerContainer ? - 'docker run --rm -v ' + codeDirectory + ':/var/task ' + program.dockerContainer + ' npm -s install --production' : + var command = program.dockerImage ? + 'docker run --rm -v ' + codeDirectory + ':/var/task ' + program.dockerImage + ' npm -s install --production' : 'npm -s install --production --prefix ' + codeDirectory; exec(command, { maxBuffer: maxBufferSize, @@ -457,7 +457,7 @@ Lambda.prototype._buildAndArchive = function (program, archive_callback) { // Warn if not building on 64-bit linux var arch = process.platform + '.' + process.arch; - if (arch !== 'linux.x64' && !program.dockerContainer) { + if (arch !== 'linux.x64' && !program.dockerImage) { console.warn('Warning!!! You are building on a platform that is not 64-bit Linux (%s). ' + 'If any of your Node dependencies include C-extensions, they may not work as expected in the ' + 'Lambda environment.\n\n', arch);