aws-lambda
Advanced tools
Comparing version 1.0.0 to 1.0.1
151
lib/main.js
@@ -10,6 +10,66 @@ "use strict"; | ||
var Lambda = function( ) { | ||
var Lambda = function( o ) { | ||
this.settings = o || {}; | ||
return this; | ||
} | ||
Lambda.prototype._parse_config_file = function( program ) { | ||
if (program.substr(-7) === '.lambda') | ||
program = program.substr(0,program.length - 7) | ||
var config_file = program + '.lambda' | ||
if ( !fs.existsSync( config_file ) ) { | ||
console.log('Lambda config not found (' + program + '.lambda )') | ||
process.exit(-1) | ||
} | ||
var $config; | ||
try { | ||
//var $config = JSON.parse(fs.readFileSync( config_file, "utf8")) | ||
$config = yaml.safeLoad(fs.readFileSync( config_file, "utf8"), { | ||
schema: buildYamlSchema(), | ||
onWarning: function(warning) { | ||
console.error(warning); | ||
}, | ||
json: true, | ||
}) | ||
} catch (e) { | ||
console.log('Invalid config file (' + program + '.lambda )', e ) | ||
if ( _this.settings.exitOnError !== false ) | ||
process.exit(-1) | ||
return; | ||
} | ||
// console.log( JSON.stringify($config, null, "\t")) | ||
if ((typeof $config.AWS_KEY === "object") && $config.AWS_KEY.hasOwnProperty('Ref') && (typeof $config.AWS_KEY.Ref === "string") && ($config.AWS_KEY.Ref.indexOf('env.') === 0)) | ||
$config.AWS_KEY = process.env[ $config.AWS_KEY.Ref.split('env.')[1] ] | ||
if ((typeof $config.AWS_SECRET === "object") && $config.AWS_SECRET.hasOwnProperty('Ref') && (typeof $config.AWS_SECRET.Ref === "string") && ($config.AWS_SECRET.Ref.indexOf('env.') === 0)) | ||
$config.AWS_SECRET = process.env[ $config.AWS_SECRET.Ref.split('env.')[1] ] | ||
if ((typeof $config.Role === "object") && $config.Role.hasOwnProperty('Ref') && (typeof $config.Role.Ref === "string") && ($config.Role.Ref.indexOf('env.') === 0)) | ||
$config.Role = process.env[ $config.Role.Ref.split('env.')[1] ] | ||
if (!$config.FunctionName) | ||
$config.FunctionName = program.split('/').slice(-1)[0] | ||
var $configPath = program.split('/').slice(0,-1).join('/') | ||
//console.log( $configPath ) | ||
if (($config.PATH.substr(0,1) === '/') || ($config.PATH.substr(0,2) == '~/')) { | ||
var $fullFunctionPath = ($config.PATH).replace(/\/\.\//g, '\/') | ||
} else { | ||
var $fullFunctionPath = (process.cwd() + '/' + $configPath + '/' + $config.PATH).replace(/\/\.\//g, '\/') | ||
} | ||
if ($fullFunctionPath.substr(-1) !== '/') | ||
$fullFunctionPath+='/' | ||
//console.log($fullFunctionPath) | ||
$config.PATH = $fullFunctionPath; | ||
return $config; | ||
} | ||
Lambda.prototype.deploy = function( program ) { | ||
@@ -38,3 +98,5 @@ var aws = require( "aws-sdk" ) | ||
console.log('Invalid config file (' + program + '.lambda )', e ) | ||
process.exit(-1) | ||
if ( _this.settings.exitOnError !== false ) | ||
process.exit(-1) | ||
return; | ||
} | ||
@@ -52,2 +114,4 @@ // console.log( JSON.stringify($config, null, "\t")) | ||
if (!$config.FunctionName) | ||
$config.FunctionName = program.split('/').slice(-1)[0] | ||
@@ -62,4 +126,2 @@ aws.config.update({ | ||
if (!$config.FunctionName) | ||
$config.FunctionName = program.split('/').slice(-1)[0] | ||
@@ -131,2 +193,5 @@ var _this = this | ||
if (typeof $config.Tags !== "object") | ||
$config.Tags = {} | ||
var paramsV2 = { | ||
@@ -144,2 +209,3 @@ Code: { ZipFile: buffer, }, | ||
Layers: $config.Layers || [], | ||
Tags: $config.Tags, | ||
//VpcConfig: {} | ||
@@ -153,7 +219,11 @@ }; | ||
if ( err && err.code !== 'ResourceConflictException') { | ||
console.log("upload error:", err ) | ||
process.exit(-1) | ||
console.log("ERROR:", err ) | ||
if ( _this.settings.exitOnError !== false ) | ||
process.exit(-1); | ||
return | ||
} | ||
if ( err && err.code === 'ResourceConflictException') { | ||
// console.log("function exists, should update config and code") | ||
@@ -170,8 +240,14 @@ var paramsV2 = { | ||
Layers: $config.Layers || [], | ||
//Tags: $config.Tags, | ||
//VpcConfig: {} | ||
}; | ||
// interesting: tags not supported by updateFunctionConfiguration but it will remve tags | ||
lambdaV2.updateFunctionConfiguration(paramsV2, function(err, data) { | ||
if ( err) { | ||
console.log("upload error:", err ) | ||
process.exit(-1) | ||
console.log("ERROR:", err ) | ||
if ( _this.settings.exitOnError !== false ) | ||
process.exit(-1) | ||
return; | ||
} | ||
@@ -186,8 +262,30 @@ | ||
}; | ||
lambdaV2.updateFunctionCode(paramsV2, function(err, data) { | ||
lambdaV2.updateFunctionCode(paramsV2, function(err, data2) { | ||
if ( err) { | ||
console.log("upload error:", err ) | ||
process.exit(-1) | ||
console.log("ERROR:", err ) | ||
if ( _this.settings.exitOnError !== false ) | ||
process.exit(-1) | ||
return; | ||
} | ||
// lambdaV2.listTags({ Resource: data.FunctionArn }, function(err, data) { | ||
// console.log( err, data ) | ||
// }); | ||
// lambdaV2.listTags({ Resource: data.FunctionArn }, function(err, data) { | ||
// console.log( err, data ) | ||
// }); | ||
if (Object.keys($config.Tags).length) { | ||
lambdaV2.tagResource({ Resource: data.FunctionArn, Tags: $config.Tags }, function(err, data) { | ||
if (err) console.log("WARNING: tagResource failed on function ", $config.FunctionName ) | ||
console.log( "Deployed!" ); | ||
}); | ||
return; | ||
} | ||
// @todo: listTags, diff with $config.Tags, untagResource not in $config.Tags, tagResource with $config.Tags | ||
console.log( "Deployed!" ); | ||
return; | ||
}); | ||
@@ -271,3 +369,3 @@ | ||
if ( err && err.code === 'ResourceNotFoundException') | ||
console.log("delete warning: function not found") | ||
console.log("WARNING: deleteFunction ",$config.FunctionName,": ResourceNotFoundException " ) | ||
else | ||
@@ -349,3 +447,32 @@ console.log( "Deleted!" ); | ||
Lambda.prototype.start = function( program ) { | ||
console.log("start", program ) | ||
var fs = require('fs'); | ||
var l = new Lambda( { exitOnError: false } ) | ||
console.log("---|", new Date().toISOString() ) | ||
try { | ||
l.deploy( program ) | ||
} catch (e) { console.log(e) } | ||
setTimeout(function() { | ||
fs.watch(program, function(event, filename) { | ||
console.log("---|", new Date().toISOString() ) | ||
try { | ||
l.deploy( program ) | ||
} catch (e) {} | ||
}); | ||
var config = l._parse_config_file( program ) | ||
// console.log( config ) | ||
fs.watch( config.PATH, function(event, filename) { | ||
console.log("---|", new Date().toISOString(), event, filename ) | ||
try { | ||
l.deploy( program ) | ||
} catch (e) {} | ||
}); | ||
}, 100) | ||
} | ||
module.exports = new Lambda( ); |
{ | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"license": "MIT", | ||
@@ -17,3 +17,3 @@ "author": { | ||
], | ||
"homepage": "https://github.com/awspilot/cli-lambda-deploy", | ||
"homepage": "https://awspilot.dev", | ||
"bugs": "https://github.com/awspilot/cli-lambda-deploy/issues", | ||
@@ -20,0 +20,0 @@ "repository": { |
@@ -20,27 +20,18 @@ | ||
## Config file | ||
## Usage | ||
* PATH must point to your code folder and is relative to the config file | ||
* PATH can be relative or absolute | ||
* If not set, Runtime defaults to **nodejs10.x** | ||
* If not set, FunctionName defaults to the name of the config file ("my-function" in this case) | ||
* You can use **Ref** to reference environment variables in the form of env.YOUR_ENVIRONMENT_NAME | ||
* `lambda deploy <file.lambda>` credentials needs permissions to **CreateFunction**, **UpdateFunctionConfiguration** and **UpdateFunctionCode** | ||
* `lambda delete <file.lambda>` credentials needs permissions to **DeleteFunction** | ||
* `lambda invoke <file.lambda>` credentials needs permissions to **InvokeFunction** | ||
``` | ||
// if installed globally then | ||
lambda deploy /path/to/my-function.lambda | ||
// if 'npm installed' without the -g then you must use the full path | ||
node_modules/.bin/lambda /path/to/my-function.lambda | ||
## Sample JSON config | ||
``` | ||
## Sample .lambda file | ||
* PATH must point to your code folder and is relative to the .lambda file | ||
* PATH can be relative or absolute | ||
* If not set, Runtime defaults to **nodejs10.x** | ||
* If not set, FunctionName defaults to the name of the config file without extension ("my-function" in this case) | ||
* You can use **Ref** to reference environment variables in the form of env.YOUR_ENVIRONMENT_NAME | ||
* `lambda deploy <file.lambda>` credentials needs permissions to **CreateFunction**, **UpdateFunctionConfiguration** and **UpdateFunctionCode** | ||
* `lambda delete <file.lambda>` credentials needs permissions to **DeleteFunction** | ||
* `lambda invoke <file.lambda>` credentials needs permissions to **InvokeFunction** | ||
``` | ||
// Sample contents of my-function.lambda | ||
{ | ||
@@ -66,2 +57,6 @@ "PATH": "./test-function", | ||
], | ||
"Tags": { | ||
"k1": "v1", | ||
"k2": "v2" | ||
}, | ||
"Description": "" | ||
@@ -71,3 +66,4 @@ } | ||
You can also use yaml content, | ||
## Sample YAML config | ||
``` | ||
@@ -92,3 +88,30 @@ # unlike json, comments are allowed in yaml, yey! | ||
- "arn:aws:lambda:eu-central-1:452980636694:layer:awspilot-dynamodb-2_0_0-beta:1" | ||
Tags: | ||
k1: v1 | ||
k2: v2 | ||
Description: "" | ||
``` | ||
## Deploy from Local to AWS Lambda | ||
``` | ||
// if installed globally then | ||
$ lambda deploy /path/to/my-function.lambda | ||
$ lambda deploy ../configs/my-function.lambda | ||
// if 'npm installed' without the -g then you must use the full path | ||
$ node_modules/.bin/lambda /path/to/my-function.lambda | ||
// you can also add it in your scripts section of your package.json scripts: { "deploy-func1": "lambda deploy ../config/func1.lambda" } | ||
$ npm run deploy-func1 | ||
``` | ||
## Watch config file | ||
aws-lambda can also watch the config file and the code folder specified in the config.PATH for changes and re-reploy on change | ||
``` | ||
$ lambda start ../configs/my-function.lambda | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
22302
426
114
1
14