Skip to content

Deploy lambda function from S3 #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,16 @@ Default value: None - Required (if you havn't specified an ARN)

##### Proxy
On Linux based hosts you can set proxy server for deploy task by specifying standard environment variable - https_proxy.
E.g:
E.g:
env https_proxy=http://localhost:8080 grunt deploy

##### s3_key and s3_bucket

* s3_key : the S3Key to be used if deploying from AWS S3
* s3_bucket : the S3Bucket to be used if deploying from AWS S3

Those are an alternative to package options

##### package
Type: `String`
Default value: Package name set by package task of same target - see below.
Expand Down Expand Up @@ -643,4 +650,4 @@ Adding more warnings for various failure cases

* Added support for Node 4.3 runtime callback function - [pull request by bobhigs](https://github.com/Tim-B/grunt-aws-lambda/pull/76)
* Added VPC support - [pull request by beeva-arturomartinez](https://github.com/Tim-B/grunt-aws-lambda/pull/71)
* Added local proxy support - [pull request by alekstr](https://github.com/Tim-B/grunt-aws-lambda/pull/66)
* Added local proxy support - [pull request by alekstr](https://github.com/Tim-B/grunt-aws-lambda/pull/66)
67 changes: 41 additions & 26 deletions utils/deploy_task.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ deployTask.getHandler = function (grunt) {
subnetIds: null,
securityGroupIds: null
});

if (options.profile !== null) {
var credentials = new AWS.SharedIniFileCredentials({profile: options.profile});
AWS.config.credentials = credentials;
Expand Down Expand Up @@ -83,6 +83,8 @@ deployTask.getHandler = function (grunt) {
var package_version = grunt.config.get('lambda_deploy.' + this.target + '.version');
var package_name = grunt.config.get('lambda_deploy.' + this.target + '.package_name');
var archive_name = grunt.config.get('lambda_deploy.' + this.target + '.archive_name');
var s3_key_prefix = grunt.config.get('lambda_deploy.' + this.target + '.s3_key_prefix');
var s3_bucket = grunt.config.get('lambda_deploy.' + this.target + '.s3_bucket');

if (deploy_arn === null && deploy_function === null) {
grunt.fail.warn('You must specify either an arn or a function name.');
Expand Down Expand Up @@ -248,36 +250,49 @@ deployTask.getHandler = function (grunt) {
}
};

grunt.log.writeln('Uploading...');
fs.readFile(deploy_package, function (err, data) {
var codeParams = {
FunctionName: deploy_function,
};

var updateFunctionCodeCb = function (err, data) {
if (err) {
grunt.fail.warn('Could not read package file (' + deploy_package + '), verify the lambda package ' +
'location is correct, and that you have already created the package using lambda_package.');
grunt.fail.warn('Package upload failed, check you have lambda:UpdateFunctionCode permissions and that your package is not too big to upload.');
}

var codeParams = {
FunctionName: deploy_function,
ZipFile: data
};
grunt.log.writeln('Package deployed.');

lambda.updateFunctionCode(codeParams, function (err, data) {
if (err) {
grunt.fail.warn('Package upload failed, check you have lambda:UpdateFunctionCode permissions and that your package is not too big to upload.');
}
updateConfig(deploy_function, configParams)
.then(function () {return createVersion(deploy_function);})
.then(function () {return setAliases(deploy_function);})
.then(function () {return setPackageVersionAlias(deploy_function);})
.then(function () {
done(true);
}).catch(function (err) {
grunt.fail.warn('Uncaught exception: ' + err.message);
});
};

if(s3_bucket){
codeParams.S3Key = (s3_key_prefix ? path.join(s3_key_prefix,deploy_package) : deploy_package);
codeParams.S3Bucket = s3_bucket;
lambda.updateFunctionCode(codeParams, updateFunctionCodeCb);

} else if(deploy_package){
grunt.log.writeln('Uploading...');
fs.readFile(deploy_package, function (err, data) {
if (err) {
grunt.fail.warn('Could not read package file (' + deploy_package + '), verify the lambda package ' +
'location is correct, and that you have already created the package using lambda_package.');
}

codeParams.ZipFile = data;

lambda.updateFunctionCode(codeParams, updateFunctionCodeCb);
});
} else {
grunt.fail.warn('At least package must be defined or S3_key and s3_bucket must be defined');
}

grunt.log.writeln('Package deployed.');

updateConfig(deploy_function, configParams)
.then(function () {return createVersion(deploy_function);})
.then(function () {return setAliases(deploy_function);})
.then(function () {return setPackageVersionAlias(deploy_function);})
.then(function () {
done(true);
}).catch(function (err) {
grunt.fail.warn('Uncaught exception: ' + err.message);
});
});
});
});
};
};
Expand Down