Community: functions debugging
Netlify Functions are a huge part of what makes JAMstack sites so powerful - now you can run dynamic code at browse time via AWS Lambdas without actually needing a AWS account. Things that you have previously needed a server-side backend to accomplish, such as processing a form or managing an Identity signup, or triggering a series of events can now be handled without needing to run a server on your own - hopefully this is both much cheaper and muceasier to maintain.
That said, Functions are new territory for many, and it can feel overwhelming at first to get started. This post is a work in progress, and we’ll be continuously editing it and making it more useful for you to debug your sites and apps that use Netlify Functions.
Firstly, the official Netlify docs site on Functions should be your starting place - it contains tons of information to get you up and running the right way. We feel very sure that this massive collection of some of the most popular applications for Netlify Functions will also be really helpful You might get a lot out of this video as well:
Secondly, a huge part of Functions is testing, and we have an NPM package,
netlify-lambda, which allows you to test node functions locally.
If, for some reason, you can’t test your functions with our npm package, we recommend you try and pull out the custom code and test it separately. Because functions run on Node.js or Go, you can run unit tests etc like you would with any other Node.js/Go code.
Thirdly, A few more bits and pieces:
- You must specify a functions directory, this can be done from the site config or in a
netlify.tomlLearn more about configuring that file.
- functions need to be bundled OR zipped with dependencies. Bundling can be done with
netlify-lambda(mentioned above). For zipping, we suggest this tool by Netlifyer David Wells.
- Functions must return a JSON string in the payload - even if it’s an error you’re returning, please make sure the content-type is
application/json, and you may want to JSON.Stringify() any text.
- Functions timeout after 10 seconds, and limited to 1024mb (1 gb) of memory by default. They are deployed to AWS Lambda in their
us-east-1region. Some modules, for example, Headless Chrome, require the whole 1gb of RAM! There are custom settings available (up to 26 seconds runtime and 3Gb memory) which you’ll need to contact our helpdesk to get help configuring.
Question not answered? Don’t fret - pop a comment below and we will be happy to help you out.