tim-b / grunt-aws-lambda Goto Github PK
View Code? Open in Web Editor NEWA grunt plugin to assist in developing functions for AWS Lambda.
License: MIT License
A grunt plugin to assist in developing functions for AWS Lambda.
License: MIT License
Strange issue here. I have npm installed globally, but when I followed your setup instructions, it was choking on the lambda_package task with:
Loading "lambda_package.js" tasks...ERROR
>> Error: Cannot find module 'npm'
I added npm to the dev dependencies inside your project, and everything ran fine. Not sure why that is if you didn't encounter the same issue. Again, I have npm installed globally, so it seems like a strange error.
running lambda_package task creates a zip in /dist that is missing the /node_modules folder.
i tried setting the permissions on the node_modules, in case that was the problem. but no, it still persists.
it started happening after i deleted the node_modules folder and npm installed again. i also tried to copy the project into a new folder and the same is still happening.
did anyone encounter this issue before?
Hey there! I just switched to using the config
module which places private env variables in config/default.json. I noticed that the config file and directory is not being included with the .zip file that is uploaded to Lambda. Can someone recommend the right way to ensure that this directory is included? I am guessing it may have to do with .npmignore
Hey Tim, loving this repo... saving me a bunch of time & the documentation is great.
One suggestion. According to this AWS Lambda docs page, a Lambda deployment package only needs to contain a single JS file and the relevant node_modules. But it looks like this repo's lambda_package
method is bundling other stuff from the project folder.
I'm able to bypass most of them with my .npmignore
file, but package.json
is still included. I don't think it's necessary here unless I'm missing something. Lambda seems to ignore these extraneous files, but it might be better to not include them in the first place.
This could be a good candidate for my first pull request. Maybe I can take a crack at it. Just LMK if I'm missing something...
The region for aws sdk is not configurable. The file 'lambda_deploy.js' has a hard coded value of us-west-1 for the region. As a result it is not possible to deploy to any other aws region.
Hi,
I have problems with deployment. The grunt deploy
task is keep getting 404 and shows:
Running "lambda_package:default" (lambda_package) task
[email protected]
Created package at ./dist/css-processor_1-0-0_2016-2-21-22-58-51.zip
Running "lambda_deploy:default" (lambda_deploy) task
Warning: Unable to find lambda function arn:aws:lambda:us-west-2:XXX:function:XX-css-processor, verify the lambda function name and AWS region are correct. Use --force to continue.
Aborted due to warnings.
I'm able to invoke the same function with aws-cli:
aws lambda invoke --function-name XXX-css-processor test
{
"StatusCode": 200
}
I'm running
$ aws --version
aws-cli/1.10.14 Python/2.7.10 Darwin/14.5.0 botocore/1.4.5
Package.json
"devDependencies": {
"grunt": "^0.4.5",
"grunt-aws-lambda": "^0.12.0"
}
Gruntfile.js
var grunt = require('grunt');
grunt.loadNpmTasks('grunt-aws-lambda');
grunt.initConfig({
lambda_invoke: {
default: {
}
},
lambda_deploy: {
options: {
enableVersioning: true
},
default: {
arn: 'arn:aws:lambda:us-west-2:XXX:function:XXX-css-processor'
}
},
lambda_package: {
default: {
}
},
});
grunt.registerTask('deploy', ['lambda_package:default', 'lambda_deploy:default']);
Am I doing something wrong? I have used this library before and never head problems with this. Do you see there something wrong? I'm sure that the Lambda function is working and I'm able to test it.
I'm attempting use grunt-aws-lambda for a project (https://github.com/kevinreedy/chef-asg-cleanup) that uses https://github.com/normanjoyner/chef-api. One of its dependencies is https://github.com/quartzjer/ursa, which has native extensions. When I run an npm install
, the extensions are built! When I run grunt lambda_package
, they are not. Steps to reproduce below:
$ git clone [email protected]:kevinreedy/chef-asg-cleanup.git
$ cd chef-asg-cleanup/
$ npm install
[output snipped]
$ grunt lambda_package
Running "lambda_package:default" (lambda_package) task
[email protected] ../../../tmp/1443742405683.5662/node_modules/chef-asg-cleanup
Created package at ./dist/chef-asg-cleanup_0-1-0_2015-9-1-23-33-25.zip
Done, without errors.
$ cd dist
$ unzip chef-asg-cleanup_0-1-0_2015-9-1-23-33-25.zip
[output snipped]
$ cd ..
root@8dd621056992:/usr/src/app# ls node_modules/chef-api/node_modules/ursa/build
Makefile Release binding.Makefile config.gypi ursaNative.target.mk
root@8dd621056992:/usr/src/app# ls dist/node_modules/chef-api/node_modules/ursa/build
ls: cannot access dist/node_modules/chef-api/node_modules/ursa/build: No such file or directory
I imagine there's an option that is needed to pass into npm.commands.install
(https://github.com/Tim-B/grunt-aws-lambda/blob/master/tasks/lambda_package.js#L59-L66) to ensure that node-gyp
gets called, but it's not obvious from looking at npm's code. Any thoughts to push me in the right direction? Thanks!
The lambda_deploy task fails because it executes a getFunction before uploading the packaged zip file.
Since the UploadFunction method is used to create and update functions, failing on a 404 response from the getFunction call is probably not the best practice.
(I'm new at all this, so it's likely that I am doing something fundamentally wrong.)
My source tree look like this:
/Gruntfile.js
/package.json
/node_modules/..../blah
/function1/index.js
/function1/package.json
/function2/index.js
/function2/package.json
/common/dependency_package/index.js
/common/dependency_package/package.json
dependency_package
has been added to the dependencies
and bundledDependencies
sections of function1/package.json
and function2/package.json
.
I've installed dependency_package
in /
so it is in /node_modules/
When I run the lambda_invoke
task everything works fine.
When I package with lambda_package
, the task executes with no warning or error, but dependency_package
is not included in the zip file. To have it included in the zip file I need to go into /function1/
and /function2/
directories and run npm install
there.
I was hoping that grunt-aws-lambda
would find the package by inspecting up the directory tree like require
does. Failing that I was hoping that the plugin would issue an error if it cannot find bundledDependencies
when building the zip.
When developing functions and invoking them using lambda_invoke, error (e.g. parse errors) stacktraces are getting swallowed up and I'm just getting the error string.
Is there a way to get the full trace? Thanks.
I use [email protected]. Using grunt-aws-lambda gives me:
Loading "lambda_package.js" tasks...ERROR
>> Error: Cannot find module 'npm'
>> at Function.Module._resolveFilename (module.js:336:15)
>> at Function.Module._load (module.js:278:25)
>> at Module.require (module.js:365:17)
>> at require (module.js:384:17)
>> at Object.module.exports (node_modules\grunt-aws-lambda\tasks\lambda_package.js:14:15)
Looks like require('npm')
no longer works by default with npm 3.
npm i [email protected]
fixes it, so you can probably just include npm in the dependencies. No idea how you would elegantly pass the version of the globally installed npm though. A postinstall hook would work, I guess.
Unable to deploy to lambda when I run grunt deploy, getting following error:
Running "lambda_upload" task
Fatal error: Cannot read property 'Configuration' of null
My gruntfile is as follows:
var grunt = require('grunt');
grunt.loadNpmTasks('grunt-aws-lambda');
grunt.initConfig({
lambda_invoke: {
default: {
options: {
file_name: 'index.js'
}
}
},
lambda_deploy: {
default: {
options: {
timeout : 10,
memory: 256
},
arn: 'arn:aws:lambda:us-east-1:XXXXXXXX:function:XXXX'
}
},
lambda_package: {
default: {
}
}
});
grunt.registerTask('deploy', ['lambda_package', 'lambda_deploy']);
I am able to run grunt lambda_package and get no issues. Tried running grunt lambda_deploy on its own, but to no avail.
Anyone else seeing this error on grunt deploy
? It happens intermittently. I am also seeing very long times at Running "lambda_package:default" (lambda_package) task
and very large packages (> 1GB) despite my codebase being quite a bit smaller.
Running "lambda_deploy:default" (lambda_deploy) task
Uploading...
#
# Fatal error in ../deps/v8/src/handles.h, line 48
# CHECK(location_ != NULL) failed
#
==== C stack trace ===============================
1: ??
2: ??
3: ??
4: ??
5: ??
6: ??
7: ??
8: ??
Illegal instruction: 4
Provides a function to invoke lambda with a event.json, but doesn't provide a facility to verify the response from the function. What is the purpose of this lambda_invoke
without verification?
Implement support for latest Lambda Node.js 4.32 release and the callback method functionality that is replacing the context object.
https://aws.amazon.com/blogs/compute/node-js-4-3-2-runtime-now-available-on-lambda/
So I'm trying to deploy using the recommended policy and with hardcoded creds (key, secret and region) and it's failing.
It DOES work if I use my root account creds in ~/.aws/credentials which I have removed
I am hardcoding it for a reason.
Am I the only one getting this issue?
I've just installed Mixpanel (latest cocoapod), and I set up an A/B Experiment to update the text on a button. On launch, the app crashes on the function:
__unused static id transformValue(id value, NSString *toType)
on line 66:
if ([value isKindOfClass:[NSClassFromString(toType) class]]) {
toType is pointing to [NSNull null] rather than an NSString instance.
My base app is Swift. I'm running XCode 7.2.1
Hi all,
I want to use this as part of my Travis CI setup. In practice this means I'd like to provide AWS credentials using my ~/.aws/credentials
file when deploying locally, but using environmental variables when deploying on Travis. Is there any way to do this currently?
Thanks
Are you planning to support the new versioning feature in some way? It looks really useful!
Hi,
Aliases and Versions are not created when executing the 'deploy' command. The Grunt task has the following configuration:
lambda_deploy: {
default: {
'arn':'MyFunction',
options: {
aliases: 'beta',
enableVersioning: true,
timeout:300,
memory: 128,
RoleArn:'SOME_ROLE_ARNA'
}
}
}
Am I missing something?
In my package.json I am using the "files" attribute in an attempt to include a file in a parent directory. It is not being packaged. Files inside the package folder are. Is this an npm issue?
I am working with versioning my lambda functions and I am realizing two things.
Obviously version numbers don't matter, but if lambda is charging me for storage (though the cost is miniscule, a fraction of a fraction of a cent) I think it would be awesome to check for changes before deploying with this tool.
Maybe keep track of the version and alias for a given deploy and then when redeploying to that alias, check that the code is the same as the previously pushed version as well as that the alias is assigned to that version.
Hi!
I want a setup where I have multiple different javascript files in my project. The different files export one handler each. I deploy the same package to different lambda functions which calls the different handlers. Unfortunately it seems that...
lambda_deploy..options.handler
...is ignored.
Example:
I setup my Gruntfile.js like this:
lambda_deploy: {
default: {
arn: '<here goes my default function arn>',
options: {
// Task-specific options go here.
}
},
helloWorld: {
arn: '<here goes my helloWorld function arn>',
options: {
handler: 'helloWorld.handler'
}
}
}
It seems to work if I add this code to lambda_deploy.js:
if (options.handler !== null) {
configParams.Handler = options.handler;
}
Aws is unable to locate node modules but when I zip it up manually it is able to locate them.
Also the package name date is one month behind.
I try to run the task on my iMac the generated zip doesn't contain any sources.
I added this piece of code to see files that are added to the archive. And I see nothing.
zipArchive.on('entry', function(data) {
console.log(data);
});
Next I change the install_location to a directory on the project root, all the files to be compressed are listed correctly. But the archive is corrupt.
var install_location = "./.tmp";
Finally I changed the script that copies the archive to wait for the copy before deleting, and everything works.
output.on('close', function () {
mkdirp('./' + options.dist_folder, function (err) {
fs.createReadStream(install_location + '/' + archive_name + '.zip').pipe(
fs.createWriteStream('./' + options.dist_folder + '/' + archive_name + '.zip')
).on('close', function(){
rimraf(install_location, function () {
grunt.config.set('lambda_deploy.' + task.target + '.package',
'./' + options.dist_folder + '/' + archive_name + '.zip');
grunt.log.writeln('Created package at ' + options.dist_folder + '/' + archive_name + '.zip');
done(true);
});
});
});
});
I'm a beginner with node js and grunt, there is there a problem in my configuration or worries with the task ?
I am looking at the source code for the packaging task. It uses npm.commands.install, but when I read the purpose of bundledDependencies in https://docs.npmjs.com/files/package.json, it seems it is for when we publish a module and not when installing a module.
Hello,
Can you add the ability to exclude files in the lambda_package task? Or modify the include_files property to add only those files specified and not include the entire project folder? thanks.
When the zip archive ends up being > 50MB the upload fails with the following (misleading) error message:
Warning: Package upload failed, check you have lambda:UpdateFunctionCode permissions. Use --force to continue.
There should be a check for the archive size before trying to upload.
In lambda the environment variable LAMBDA_TASK_ROOT is exported to the root of the unpacked archive. But grunt-aws-lambda does not seem to export it.
With the following configuration:
lambda_package: {
default: {
options: {
include_files: ['src/*.js'],
}
}
}
...grunt-aws-lambda
creates a ZIP file with src
at the root, meaning Lambda can't find the files. It looks like someone had a similar issue with archiver - archiverjs/node-archiver#142.
Is there another way I should be specifying or storing my source files? Thanks!!
In the Lambda environment the default region used by the AWS SDK is the region in which the Lambda function is running. In the grunt-aws-lambda environment when running the task locally the region is not set. I have the region specified in ~/.aws/config as us-west-2 but it is not picking it up from there.
Here is the error thrown by the AWS SDK when I try to make a call to CloudWatchLogs api:
{ [ConfigError: Missing region in config]
message: 'Missing region in config',
code: 'ConfigError',
time: Fri Jul 03 2015 21:37:54 GMT-0700 (PDT) }
Could you add the httpOptions in Options?
http://blogs.aws.amazon.com/javascript/post/Tx3CV0OSVN2TBPS/Using-the-AWS-SDK-for-JavaScript-from-Behind-a-Proxy
When I try to deploy lambda function behind proxy server, it's always failed.
Or should I open a new PR for this?
Just add the proxy string for options will influence the dependencies. It will require proxy-agent.
When you use grunt lambda_deploy
it has no checks built in to make sure the given package file exists. The error is generic and makes it seem like the upload itself failed when there was really nothing to upload.
Like I mentioned in #8, if this project is still active I can submit a PR to fix this. Thanks!
With npm 3 on the way, I tried to get grunt-aws-lambda to support it. There are two main issues:
The call to npm.commands.install
in lambda_package.js
should be updated to pass its second argument, the package to install, as an array rather than a string. Seems easy enough, and it worked in my tests.
Unfortunately, because npm 3 flattens dependency structures rather than creating nested node_modules
folders, you end up with something like
temp
|_ node_modules
|_ myLambda
|_ dependency1
|_ dependency1.1
|_ dependency1.2
|_ dependency2
IIRC (I didn't check), with npm 2, you would get the desired effect:
temp
|_ node_modules
|_ myLambda
|_ node_modules
|_ dependency1
|_ node_modules
|_ dependency1.1
|_ dependency1.2
|_ dependency2
I assume that what we're looking for with npm 3 is:
temp
|_ node_modules
|_ myLambda
|_ node_modules
|_ dependency1
|_ dependency1.1
|_ dependency1.2
|_ dependency2
I'm not sure if that can be achieved by passing the appropriate arguments. Alternatively, I guess you could just move all the dependencies into that extra node_modules
manually, but that seems messy.
You can just depend on npm 2 for the time being, but I already wanted to document this issue for when npm 3 becomes mainstream.
Because you support file-name in lambad-invoke, so I can use other js file name, not default index.js. But if I use customized js file name, it doesn't work after deploy, because js file name is changed. To support it you have to support "handler" in lambda-deploy.
When you use grunt lambda_invoke
on even the hello world script, it gives the error:
$ grunt lambda_invoke:HelloWorld
Running "lambda_invoke:HelloWorld" (lambda_invoke) task
Loading function
value1 = First Value
value2 = Second Value
value3 = Third Value
Warning: undefined is not a function Use --force to continue.
Aborted due to warnings.
I found the issue is that context.succeed
function is not defined in the invoke.
Is this project still active? If so I can submit a PR to fix this by fully mocking the context object as documented in the AWS Lambda docs. Thanks!
Are there any options to inject an object into the context called clientContext? The native mobile SDKs pass this in, which has device information, etc, as well as the local AWS Cognito identifier. This is the sort of information that will become more and more common once mobile developers start calling into Lambdas natively.
http://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html#API_Invoke_RequestSyntax
http://docs.aws.amazon.com/mobileanalytics/latest/ug/PutEvents.html
I have a hidden environment file call ".env" which is used to bootstrap environment variables using package "node-env-file", however I notice that the file is not being packaged.
After uninstalling and reinstalling dependencies and grunt-aws-lambda 0.8.0, packaging no longer works for me. Prior to this version it was working even with the default empty lambda_package setting in Gruntfile.js.
Grunt 0.4.5
Grunt-Cli 0.1.13
Grunt-aws-lambda 0.8.0
Opening this issue just as a marker for anyone else who runs in to it...
I just ran grunt deploy
and ended up getting the error:
Package upload failed, check you have lambda:UpdateFunctionCode permissions.
The actual err
returned in the lambda.updateFunctionCode
callback was toString failed
, which happens when a buffer exceeds kMaxLength.
This is probably because the dist folder is not in your .npmignore file. The zip file has exceeded the max size of a buffer and the AWS SDK is choking on it.
Either add dist to .npmignore or clean out your dist folder.
If your package really does exceed the maximum buffer size (which sounds like it's around 2GB right now), then you'll likely need to upload to an S3 bucket first, as AWS.lambda.updateFunctionCode
doesn't seem to support streams.
AWS Lambda has moved its supported version of 'aws-sdk' to 2.2.3, while this project still shows a dependency on version ~2.1.30. It's quite annoying to have to see all the "unmet dependency" output when using npm.
Looking through the documentation, it doesn't look like there is anything between 2.1.35 and 2.2.3 that would cause the functionality to break. Can we please get a dependency version update?
It would be great to know how much memory has been used during the lambda_invokation process.
changing the callback function to something similar would be enought
var context = {
done: function (status, message) {
var success = status === null;
grunt.log.writeln("");
grunt.log.writeln("Message");
grunt.log.writeln("-------");
grunt.log.writeln(message);
grunt.log.writeln("");
grunt.log.writeln("Memory usage");
grunt.log.writeln("-------");
grunt.log.writeln(util.inspect(process.memoryUsage()));
done(success);
}
};
Running "lambda_deploy:prod" (lambda_deploy) task
AWS API request failed with undefined - MissingRequiredParameter: Missing required key 'FunctionName' in params
I've set the function option but I can't seem to avoid this message deploying to Lambda. Have there been API updates?
Here's my full conf.
// Lambda
lambda_invoke: {
default: {
}
},
lambda_deploy: {
default: {
options: {
// aliases: 'prod',
// enableVersioning: true
},
function: '<%= aws.AWSLambdaFunctionName =>',
arn: '<%= aws.AWSLambdaARN %>'
}
// prod: {
// options: {
// aliases: 'prod',
// enableVersioning: true
// },
// arn: '<%= aws.AWSLambdaARN %>'
// }
},
lambda_package: {
default: {
},
prod: {
}
}
});
It's common to call context.succeed
context.error
and context.done
with an object instead of a string.
The lambda invoke task fails to output them, instead just printing the following
Success! Message:
------------------
[object Object]
I think the output objects should be serialized so you get some sane output.
In lambda_package.js
, dist_folder
gets prefixed with './'
.
When I passed in an absolute path, on Windows, the task would never terminate (presumably because of some internal translation going on with './C:/...'
. On *nix, it'll most likely happily create the subdir, but I'm guessing you can just leave out the './'
prefix to end up with support for both absolute and relative paths.
Not a huge issue, but it's not really intuitive right now.
Would be nice if the function was created if not present...
The lambda_package
task ignores any errors from npm.comands.install
, which results in incorrect zip files with no indication that they are not complete.
Hi,
Thanks for this lovely plugin.
I have an error with running lambda_package I debugged and found that
grunt-aws-lambda/tasks/lambda_package.js Line 66:
npm.commands.install(install_location, options.package_folder , function () {
returns the follow error
Fatal error: Argument #2: Expected array but got string
When I change it to
npm.commands.install(install_location, [options.package_folder] , function () {
It works but I get the follow warnings
npm WARN ENOENT ENOENT, open '/private/var/folders/4r/9q6tv2_55257mkv4t9l7d4xw0000gn/T/1444468471486.175/package.json'
npm WARN EPACKAGEJSON /var/folders/4r/9q6tv2_55257mkv4t9l7d4xw0000gn/T/1444468471486.175 No description
npm WARN EPACKAGEJSON /var/folders/4r/9q6tv2_55257mkv4t9l7d4xw0000gn/T/1444468471486.175 No repository field.
npm WARN EPACKAGEJSON /var/folders/4r/9q6tv2_55257mkv4t9l7d4xw0000gn/T/1444468471486.175 No README data
npm WARN EPACKAGEJSON /var/folders/4r/9q6tv2_55257mkv4t9l7d4xw0000gn/T/1444468471486.175 No license field.
Any ideas how to fix it? My package.json file is
{
"name": "lambda-local",
"version": "1.0.0",
"description": "SMS Processor",
"main": "index.js",
"private": "true",
"dependencies": {
"aws-sdk": "^2.2.9",
"mysql": "^2.9.0",
"node-uuid": "^1.4.3",
"npm": "^3.1.2",
"request": "^2.64.0"
},
"devDependencies": {
"grunt": "^0.4.5",
"grunt-pack": "0.1.*",
"grunt-aws-lambda": "^0.8.0"
},
"license": "ISC",
"bundledDependencies": [
"aws-sdk",
"mysql",
"node-uuid",
"request"
]
}
I don't think there is a way yet to do this. I'm generating multiple entries in lambda_invoke to execute different event files. But that doesn't allow for coverage.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.