Skip to content

Commit

Permalink
Use AWS API 2015-03-31:
Browse files Browse the repository at this point in the history
- Check if the function exists, if it does update else create new
- This API provides compatibility with VpcConfig
  • Loading branch information
DeviaVir committed Mar 25, 2016
1 parent 5ea4782 commit a8e27ca
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ $ node-lambda deploy --help
-t, --timeout [3] Lambda Timeout
-d, --description [missing] Lambda Description
-u, --runtime [nodejs] Lambda Runtime
-p, --publish [false] This boolean parameter can be used to request AWS Lambda to create the Lambda function and publish a version as an atomic operation
-v, --version [custom-version] Lambda Version
-f, --configFile [] Path to file holding secret environment variables (e.g. "deploy.env")`
-b, --vpcSubnets [] VPC Subnet(s) you want your Lambda Function to deploy in to (comma separated list), when using this, the below param is also required
-g, --vpcSecurityGroups [] VPC Security Group(s) you want your Lambda Function to deploy in to (comma separated list), when using this, the above param is also required
-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
```

## Custom Environment Variables
Expand Down
4 changes: 2 additions & 2 deletions bin/node-lambda
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ var AWS_SESSION_TOKEN = process.env.AWS_SESSION_TOKEN || '';
var AWS_REGION = process.env.AWS_REGION || 'us-east-1,us-west-2,eu-west-1';
var AWS_FUNCTION_NAME = process.env.AWS_FUNCTION_NAME || packageJson.name;
var AWS_HANDLER = process.env.AWS_HANDLER || 'index.handler';
var AWS_MODE = 'event';
var AWS_ROLE = process.env.AWS_ROLE_ARN || process.env.AWS_ROLE || 'missing';
var AWS_MEMORY_SIZE = process.env.AWS_MEMORY_SIZE || 128;
var AWS_TIMEOUT = process.env.AWS_TIMEOUT || 60;
var AWS_DESCRIPTION = process.env.AWS_DESCRIPTION || '';
var AWS_RUNTIME = process.env.AWS_RUNTIME || 'nodejs';
var AWS_PUBLISH = process.env.AWS_PUBLIS || false;
var AWS_FUNCTION_VERSION = process.env.AWS_FUNCTION_VERSION || '';
var AWS_VPC_SUBNETS = process.env.AWS_VPC_SUBNETS || '';
var AWS_VPC_SECURITY_GROUPS = process.env.AWS_VPC_SECURITY_GROUPS || '';
Expand All @@ -38,12 +38,12 @@ program
.option('-r, --region [' + AWS_REGION + ']', 'AWS Region', AWS_REGION)
.option('-n, --functionName [' + AWS_FUNCTION_NAME + ']', 'Lambda FunctionName', AWS_FUNCTION_NAME)
.option('-h, --handler [' + AWS_HANDLER + ']', 'Lambda Handler {index.handler}', AWS_HANDLER)
.option('-c, --mode [' + AWS_MODE + ']', 'Lambda Mode', AWS_MODE)
.option('-o, --role [' + AWS_ROLE + ']', 'Amazon Role ARN', AWS_ROLE)
.option('-m, --memorySize [' + AWS_MEMORY_SIZE + ']', 'Lambda Memory Size', AWS_MEMORY_SIZE)
.option('-t, --timeout [' + AWS_TIMEOUT + ']', 'Lambda Timeout', AWS_TIMEOUT)
.option('-d, --description [' + AWS_DESCRIPTION + ']', 'Lambda Description', AWS_DESCRIPTION)
.option('-u, --runtime [' + AWS_RUNTIME + ']', 'Lambda Runtime', AWS_RUNTIME)
.option('-p, --publish [' + AWS_PUBLISH + ']', 'Lambda Publish', AWS_PUBLISH)
.option('-v, --version [' + AWS_FUNCTION_VERSION + ']', 'Lambda Function Version', AWS_FUNCTION_VERSION)
.option('-b, --vpcSubnets [' + AWS_VPC_SUBNETS + ']', 'Lambda Function VPC Subnets', AWS_VPC_SUBNETS)
.option('-g, --vpcSecurityGroups [' + AWS_VPC_SECURITY_GROUPS + ']', 'Lambda VPC Security Group',
Expand Down
2 changes: 2 additions & 0 deletions lib/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ AWS_MEMORY_SIZE=128
AWS_TIMEOUT=3
AWS_DESCRIPTION=
AWS_RUNTIME=nodejs
AWS_VPC_SUBNETS=
AWS_VPC_SECURITY_GROUPS=
52 changes: 45 additions & 7 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,17 @@ Lambda.prototype._runHandler = function (handler, event) {
Lambda.prototype._params = function (program, buffer) {
var params = {
FunctionName: program.functionName + (program.environment ? '-' + program.environment : ''),
FunctionZip: buffer,
Code: {
ZipFile: buffer
},
Handler: program.handler,
Mode: program.mode,
Role: program.role,
Runtime: program.runtime,
Description: program.description,
MemorySize: program.memorySize,
Timeout: program.timeout
Timeout: program.timeout,
Publish: program.publish,
VpcConfig: {}
};
if (program.version) {
params.FunctionName += ('-' + program.version);
Expand Down Expand Up @@ -191,6 +194,36 @@ Lambda.prototype._setEnvironmentVars = function (program, codeDirectory) {
fs.writeFileSync(handlerFileName, prefix + contents.toString());
};

Lambda.prototype._uploadExisting = function(lambda, params, cb) {
return lambda.updateFunctionCode({
'FunctionName': params.FunctionName,
'ZipFile': params.Code.ZipFile,
'Publish': params.publish
}, function(err, data) {
if(err) {
return cb(err, data);
}

return lambda.updateFunctionConfiguration({
'FunctionName': params.FunctionName,
'Description': params.Description,
'Handler': params.Handler,
'MemorySize': params.MemorySize,
'Role': params.Role,
'Timeout': params.Timeout,
'VpcConfig': params.VpcConfig
}, function(err, data) {
return cb(err, data);
});
});
};

Lambda.prototype._uploadNew = function(lambda, params, cb) {
return lambda.createFunction(params, function(err, data) {
return cb(err, data);
});
};

Lambda.prototype.deploy = function (program) {
this._createSampleFile('.env');

Expand Down Expand Up @@ -256,13 +289,18 @@ Lambda.prototype.deploy = function (program) {
aws.config.update(aws_security);

var lambda = new aws.Lambda({
apiVersion: '2014-11-11'
apiVersion: '2015-03-31'
});

lambda.uploadFunction(params, function (err, data) {
cb(err, data);
});
return lambda.getFunction({
'FunctionName': params.FunctionName
}, function(err) {
if(err) {
return _this._uploadNew(lambda, params, cb);
}

return _this._uploadExisting(lambda, params, cb);
});
}, function (err, results) {
if (err) {
console.error(err);
Expand Down
7 changes: 5 additions & 2 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var originalProgram = {
sessionToken: 'token',
functionName: 'node-lambda',
handler: 'index.handler',
mode: 'event',
role: 'some:arn:aws:iam::role',
memorySize: 128,
timeout: 3,
Expand Down Expand Up @@ -59,12 +58,16 @@ describe('node-lambda', function () {
program.vpcSecurityGroups = 'sg-00000000,sg-00000001,sg-00000002';
var params = lambda._params(program);
assert.equal(params.VpcConfig.SubnetIds[0], program.vpcSubnets.split(',')[0]);
assert.equal(params.VpcConfig.SubnetIds[1], program.vpcSubnets.split(',')[1]);
assert.equal(params.VpcConfig.SubnetIds[2], program.vpcSubnets.split(',')[2]);
assert.equal(params.VpcConfig.SecurityGroupIds[0], program.vpcSecurityGroups.split(',')[0]);
assert.equal(params.VpcConfig.SecurityGroupIds[1], program.vpcSecurityGroups.split(',')[1]);
assert.equal(params.VpcConfig.SecurityGroupIds[2], program.vpcSecurityGroups.split(',')[2]);
});

it('does not append VpcConfig when params are not set', function() {
var params = lambda._params(program);
assert.equal('VpcConfig' in params, false);
assert.equal(Object.keys(params.VpcConfig).length, 0);
});
});

Expand Down

0 comments on commit a8e27ca

Please sign in to comment.