Unable to set environment variable while deploying lamda functions using netlify-cli manual deploy

I am trying to deploy lambda functions manually using netlify-cli’s deploy command,
I am using netlify-lambda build to build the functions and netlify deploy --prod for deploying lambda functions, although my functions are deployed successfully but they are not able to access environment variable’s which were present in my system while deploying.

Hi @karan, where are you setting these environment variables? Can you please provide a link to your Netlify site or the site/app id?

Hi @karan - I know this is a while ago now, but could you share the command you were using to successfully deploy functions using netlify-cli?

I have tried everything I can think of and I can’t get the functions to deploy.

In theory no “command” is needed - netlify-lambda was the old way :slight_smile:

Here’s a demo showing all the ways you can deploy functions; the other two (zip-it-and-ship-it, or including package.json in the repo) are my suggestions:

I don’t understand what you are saying here. Surely there is a netlify-cli command that must be run to deploy the code?

Thanks for the example repo, I can see now the various ways to bundle the code, in each case the output ends up in the functions folder. Right?

Is the path to the functions folder the value used for the netlify-cli —functions parameter?

I never use the CLI and I deploy functions every day. Netlify builds my site for me when I commit to git, and does NOT run netlify-lambda during build.

How I do this:

  1. I set a functions directory in the UI or netlify.toml,
  2. and have my ready-to-deploy-no-more-building-needed functions in that directory (either as zipfiles or as “unbundled”).
  3. I test on deploy previews before merging to make sure my code works before it gets into production (rather than testing locally).

If you are running locally and deploying via CLI instead, this describes not using netlify lambda to build the functions: https://docs.netlify.com/cli/get-started/#unbundled-javascript-function-deploys

Ok I see what you mean now, you have it setup so Netlify does your build. I’m doing my build outside of Netlify, so I do need to run the CLI to upload the files.

Can all 3 function packaging methods (bundled, unbundled and zip-it-and-ship-it) be deployed via the CLI?

As far as I know, but as I said, I don’t use it. Let me know how your testing goes!

I got this working, seems like there was an issue having the functions folder in a hidden directory, once I updated to output the functions to dist/functions and site to dist/site the CLI ran without any issues.

I’m wondering wether at some stage in the future to start using the unbundled method of packaging. Although it works now with Netlify lambda bundling, the stack traces when something breaks are a bit horrible.

Thanks for your help!

@mjgs, I don’t think yo need to bundle your functions. You can view the doc here https://github.com/netlify/cli/blob/master/docs/commands/deploy.md#unbundled-nodejs-functions-that-have-dependencies-outside-or-inside-of-the-functions-folder. You just need to have your function inside the folder you set in your netlify.toml file. Let me know if that helps.

I can see from the link you posted that node_modules dependencies don’t have to be in the functions folder. But what about regular dependencies (i.e project modules you’ve written yourself) not in node_modules, can they also be outside the functions folder?

I ask because the lambda bundler just wraps everything up, but if I needed to restructure all my code to do the unbundled packaging, it’s quite a big job.

I recommend that regular dependencies be inside your specific functions folder. Unlike the netlify-lambda bundler, the zip-it-and-ship-it feature will need to know about your dependency to include it but then the issue of the path that you function has for that dependency will no longer be correct because zisi has included the file inside the function folder. That why I recommend that you just keep non-npm dependencies inside your function folder like so:


You can then require the mydependencyfunction.js and the path to that file should still be the same after deploy. There are some quirks to this solution as well but it should get you most of the way to where you want.

@dennis thanks that’s useful information. Is there a suggested way to use regular dependencies that are shared between functions or is duplicating the shared module the only way? How about symlinks, how are they handled?

I doubt symlinks will work. The real issue is that when you require a file, you need to specify a path. This is fine for node_modules since the absolute/relative path is omitted. But for regular dependencies, the packaging process will change the location of the file and while the actually file will be included in the function, the require path is no longer correct. Maybe you can wrap the import in an if conditional to account for the changing paths? :thinking:

I’m not entirely sure I understand what you are saying.

However your comment about paths made me think about another solution I had been contemplating.

I was thinking that at build time I could copy the shared utilities folder into the function directory. But as you say the require paths used locally would be different to build time.

Is this what you had in mind too? (Copying shared utilities into the functions dir)

1 Like

Also quick related question - is there a way to make the call stacks more usable for runtime errors that happen in the bundled code? Like source maps or similar?

Debugging the bundled code is killing me right now.

Yup, that’s what I had in mind. Copy the file at build time would probably be the best option for shared files.

Unfortunately, I don’t know if netlify-lambda is able to do that. There is an issue on the repo that seems related: https://github.com/netlify/netlify-lambda/issues/262. Perhaps you can add you voice/comments there.