Netlify lambda function available in Unique Deploy URL but not in Live URL

I am trying to get a function to run in my environment:

I followed instructions here to set up netlify-lambda locally in conjunction with netlify dev.

The good stuff: when running netlify dev, I can access the function under http://localhost:8888/.netlify/functions/hello. When I netlify deploy, I can access the function with /.netlify/functions/hello and also when I deploy to production with netlify deploy -p, I can access the function under (this is the Unique Deploy URL).

The not so good part: The function is NOT accessible under the Live URL

Is it just me or is that very strange? Especially that it’s on the Unique Deploy URL but not on the Live URL…

The deploy info:
Deploying to live site URL…
:heavy_check_mark: Finished hashing 24 files and 1 functions
:heavy_check_mark: CDN requesting 1 files and 0 functions
:heavy_check_mark: Finished uploading 1 assets
:heavy_check_mark: Deploy is live!
-> it even mentions the function there…

My repo is here: - it includes package.json, netlify.toml and the hello world function I’m trying to get to work…

I’d appreciate any help :slight_smile: Thank you!

1 Like

Yoooooo, I could be completely wrong, but is it possible you’re gitignoring the unbuilt functions directory? If you were, then when netlify pulls the repo to build it for a deploy, there wouldn’t be any functions in that build. I’m pretty sure that’s what’s happening.

Edit: Just to add some further info, the reason I suspect the dir is missing when netlify is deploying is because I couldn’t see it in the git repo you shared. I think you accidentally gitignored the unbuilt functions dir, and didn’t gitignore the built functions dir. Hope that makes senes.

Hi @nonissue, first of all thanks a lot for your reply!

I gitignored the functions_build directory now and removed the functions directory from the gitignore file. After redeploying, I see the same exact behavior:

Really weird in my opinion…

Also, on that note: I don’t really want to expose neither functions nor functions_build on my repository. The reason is that, after getting the hello world function to work, I want to build a fetch() function and store my API key inside the function.

It seems that no matter if the functions or functions_build directories are gitignored or not, the functions run perfectly with netlify dev, netlify deploy, and even in the unique deploy url when netlify deploy -p but just not on the live url. That is what seems so strange?

Hmmm weird. I was pretty certain that was it. I can try pulling it tonight or tomorrow and see what problems I have.

Also, a lot of people frown on keeping any keys in code, even if it isn’t committed. MetLife has environmental variables in the site config dashboard, and I think it would be better to store them in there. If you have issues with that let me know.

I’ll try to have another look tonight or tomorrow when I get home!

Ok! I appreciate your help.

Yeah, my first action was also to try and use environmental variables, however, I’ve run into issues with that also. The first one was that I was unable to access them on netlify live deploys. Also, it seems like locally, I’d need to use dotenv to set them up the same way, unless I’d be using netlify dev as exclusive development environment.
Either way, I found functions a more elegant way of solving my problem, because my goal is to have a fetch function for an external API, and I’d actually like to call that function in multiple places throughout my app.

However, I’d very much be open to any solution, I just want to do it the way it is supposed to be done (best practice), no matter how hard it is.
I can imagine the most elegant way would probably be to set up a Netlify (lambda) function that uses an environmental variable that is the API key that I want to hide.

Anyway, first things first; it seems I can’t even get a hello world example going on the live environment right now, so I definitely want to solve that problem first.

In a later step, I’ll implement a fetch() function and I will try again with environmental variables once that works.

Hmm, so I pulled your repo, ran yarn install, ran netlify build and then netlify deploy + netlify deploy --prod. I was able to get a response from the hello function locally, on the deploy url, and on the prod url.

Here’s the unique deploy url:

Here’s the prod deploy url:

I tested the function using postman (get request) to:

And I got a response from both.

Maybe I’m missing something, but I’m around this evening / tomorrow afternoon, so if I can do anything else to help, let me know.

In your response, I noticed that you ran “netlify build”, which builds and zips the function hello world. I never actually did that before, because I read that this is one option to build functions, the other one being the use of the netlify-lambda package (the approach I tried)…
So I made a new folder, cloned my repository, ran “netlify build”, “netlify deploy” (connected a new netlify site) and “netlify deploy -p” and sure enough, on the live url, I was able to access the function!

So I went back to my initial project folder and ran “netlify build” and “netlify deploy -p”, but nothing changed, the function is still not available on the live url of my initial project.

So I created another, fresh folder, copied the repository, ran “npm install” to install netlify-lambda and its dependencies, and then I ran “netlify dev” to test the local access to the function (worked), “netlify deploy” (connected a new netlify site), and then I ran “netlify deploy -p” and the function works on the live url.

So I don’t know what’s wrong with my initial folder, I don’t see any hidden files or anything that isn’t in the other folders. It’s a complete mystery to me at this point why it didn’t work initially.
It seems to work both with “netlify build” or alternatively using netlify-lambda as package to build functions locally.
The last attempt (working) is here:

Thanks a lot for your help @nonissue I really appreciate it.

Next up: figuring out environmental variables :smiley: If you happen to know a repo where there is a function or any piece of code that uses a netlify environmental variable successfully, that would be very interesting to see. Whenever I tried accessing a variable that I created in the Netlify UI, I got something like “process is not defined” (trying to access the variable using “process.env.API_KEY”). I also tried accessing the variable using $process.env.API_KEY, env.API_KEY, just $API_KEY, and other methods.

So strange, glad you got it working! I’ll try and skim over it tomorrow to see if I can see anything obvious that would have caused the problem.

As for env variables, a personal project of mine pulls them from the netlify env variables set in the site settings under build and deploy

It’s pretty ugly and not at all finished, so I definitely wouldn’t use it as a template for any best practices, but you can see how I use the netlify env variable in this function for example:

I hope that helps! When I have time tomorrow I’ll try adding an env variable to the clone of your repo i made on netlify and see if I can get it working.

1 Like

Thank you for the sample, this is exactly what I was looking for.

Interestingly, simply using process.env.VARIABLE_NAME was my first try, but didn’t work then, as stated in my last message. When I try the same now, it works perfectly fine.

It seems like somehow, my initial project folder or some file(s) got corrupted, because it works with the same repository and settings in a new folder.

Thanks a lot for your help @nonissue I really appreciate it :slight_smile:

1 Like