diff --git a/README.md b/README.md index cec1a5b..ada1f31 100644 --- a/README.md +++ b/README.md @@ -293,9 +293,57 @@ 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_bucket +Type: `String` + +If the value is defined, the package is deployed from AWS Lambda. +The is recommended for packages bigger than 10 Mo. + +NB: this project is not uploading the package on s3, you can use [grunt-aws-s3](https://github.com/MathieuLoutre/grunt-aws-s3) to do this. + +For example, your Gruntfile.js might contain the following: + +```javascript +grunt.initConfig({ + lambda_deploy: { + env: { + arn: 'arn:aws:lambda:us-east-1:123456781234:function:my-function', + s3_bucket: 'my-lambda-code-bucket', + s3_key_prefix: 'folderName' + } + }, + lambda_package: { + env: { + options: {} + } + }, + aws_s3: { + options: { + }, + env: { + options: { + bucket: 'my-lambda-code-bucket' + }, + files: [ + {action: 'upload', expand: true, src: ['dist/**'], differential: true, dest: 'folderName'} + ] + } + } +}); +grunt.loadNpmTasks('grunt-aws-s3'); +grunt.loadNpmTasks('grunt-aws-lambda'); +grunt.registerTask('deploy', ['lambda_package', 'aws_s3', 'lambda_deploy']); +``` + +##### s3_key_prefix +Type: `String` + +The S3 folder to be used if deploying from AWS S3. The resulting s3 key will be `:///` +If null, the package is uploaded direclty into the root of the s3 Bucket. + ##### package Type: `String` Default value: Package name set by package task of same target - see below. @@ -643,4 +691,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) \ No newline at end of file +* Added local proxy support - [pull request by alekstr](https://github.com/Tim-B/grunt-aws-lambda/pull/66) diff --git a/utils/deploy_task.js b/utils/deploy_task.js index fd6c675..7ca2683 100644 --- a/utils/deploy_task.js +++ b/utils/deploy_task.js @@ -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; @@ -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.'); @@ -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); - }); - }); - }); }); }; };