(Netlify Newbie) Current State of Forms, Functions, Env Contexts, & Dev - April 2020

Hi Netlify Folks! :wave:t2:


I recently decided to jump real deep into Netlify from scratch and have had a blast doing it, but want to clarify and culminate some findings into a single place for confirmation for both myself and others, and for clarity on the current state of things (April 29, 2020) to make sure my headā€™s in the right place. Iā€™ve scoured tons of docs, threads, and community posts here so Iā€™ll try to reference as many as I can but this is a new community account and I may be limited on links :grin:

For starters: Netlify is awesome. What an incredible platform and system. I canā€™t wait to see how it grows and hope I can help in any way possible. The dev process and framework are so phenomenal. Thanks to the whole team for all the efforts and progress made. I enjoy using this platform. It brings me joy. That is not something that happens unintentionally, so thank you.

Early apologies for the length of this post. Iā€™ve dug in pretty deep and want to make sure Iā€™m getting things right based on my observations.

Environment

First and foremost, I want to clarify the role and correct injection of environment variables. I have some ā€œprodā€ tier environment variables defined in the Netlify UI for my site, but I would like some non-prod tier env vars injected locally and/or in branch deploys and/or in preview deploys ā€“ across both site assets / files build-time and function runtimes. Docs: ā€˜Build Environment Variablesā€™ and Docs: ā€˜File-based Configurationā€™ sub-section: ā€˜Deploy Contextsā€™ both have a lot of syntax to imply that everything will be distributed correctly if you use the UI env vars and/or TOML env vars (and thatā€™s true for my prod build using my prod env vars) but this community thread with @perry and @jaredh159 suggests that environment variables defined in the TOML do not get injected into functions in particular. Is that correct?

Proposing an example: I have a page called buy.html which I inject an env var of STRIPE_PUBLIC_KEY into and a process.js Function that I pull STRIPE_SECRET_KEY in to make the transaction.

So if I put my Live (ā€˜prodā€™) API key values into the Netlify UI, everything works great on my production build. It pulls in both live keys correctly and runs things fine :+1:t2:, but if I also have Test (ā€˜not-prodā€™) API key values defined in my netlify.toml file for the build context of ā€¦anything not Production, those vars only get injected into the site static build and not the Function runtime? Is that (still) correct?

Is the current ā€˜best-optionā€™ to follow @foolā€™s response in that thread and hook into $CONTEXT?

Also thank you for the context (no pun intended) in your response, @fool!

AWS only accepts function deployments with up to 4k worth of environment variables, when appended in a string like VAR1=val1,VAR2=val2 , and base64 encoded - more will cause the deploy to fail!

Lame :stuck_out_tongue_winking_eye: ^ darn you, AWS!

Lastly, and also really importantly, does hooking into the $CONTEXT work when running the local dev functions server? Hoping to maintain the parity of dev workflow where I can run my full site env locally using .env variable values then push it up and have it run perfect on ā€˜prodā€™ values when deployed ā€“ OR have it run in a similarly valid ā€˜testā€™ state when itā€™s a branch deploy or deploy preview and then swap over and run fine on ā€˜prodā€™ values when promoted to my production deploy.

I also realize that the thread I linked to just kicked back into active discussion a few hours ago, so if others are reading this, please check there first


Dev (CLI)

Outside of whatā€™s mentioned above, and recognizing that dev is still in beta, is it still currently the case that event-based function hooks are not built into dev? I traced down and found the original PR where functions:invoke was added to the (then) nelify-dev-plugin before it got merged to the CLI repo, and Iā€™ve used this locally to kick off a submission-created.js function, but admittedly it was tricky to figure out exactly what the shape/nesting of my --payload ./foo.json data had to be to match production and itā€™s a bit manual for my taste, but it does workā€¦ well on 2.46.0. Iā€™ve had a few issues related to the framework development on 2.47.0 and 2.48.0 but thatā€™s neither here nor there.

For others in the future: if you want to use the payload functionality (which you do), this is the exact shape your json file needs to have. Command: ntl functions:invoke submission-created --payload "./some-data.json" file contents:

{
  "payload": {
    "number": 4,
    "title": "Some Value",
    "email": "test@example.com",
    "body": null,
    "data": {
      "idempotency-token": "bc7d6880-8809-11ea-8af5-18d798294576",
      "Business Name": "Example Biz",
      "Contact Name": "Example Person",
      "Contact Signature": "Example Person",
      "user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36",
      "referrer": "https://example.com/contact"
    },
}

And, a related question - since thereā€™s no way to bind to local event triggers (since thereā€™s no local forms anyway :stuck_out_tongue_winking_eye:) but you can use local functions when called explicitly, is there any actual benefit to using event-driven Functions rather than just using a ā€˜normalā€™ function and injecting a fetch() into the form submission event then running the normal .submit() for tracking in the Netlify UI / post-submission hooks (Zapier, etc.)?
I only ask because that workflow would allow the local function to run without a manual invoke.

btw, --live is absolutely brilliant. Saves me some Ngrok time and setup :pray:t2: thank you thank you thank you.

Environment workflow

Iā€™m a bit confused about deploys when it comes to their capabilities as reflected in the Netlify Web UI. The web UI feels like it only shows information and data for whatā€™s currently live in the production deploy. Forms and Functions are primary examples of this - if I create a new Form in a branch then that branch gets deployed, there is no Form listed in the web UIā€¦ which makes sense if the web UI is really only for viewing production ā€˜stuffā€™ - but is my new form actually built and setup on Netlifyā€™s back end? Is there a way to see those submissions in a web UI at all in a branch or preview deploy?
Question applies to functions too - beyond the environment variables stuff, if I create a new function in a branch or PR, then deploy that branch or preview, that function isnā€™t listed in the web ui for that Site. Did it get spun up somewhere? Is it running? I saw a forum post somewhere that I canā€™t seem to trace down again that said ā€œyesā€ to those things and that the logs for all of those functions would ultimately culminate into the log viewer for the live function (currently in ā€˜productionā€™) ā€“ which makes sense if Iā€™m making diffs to an existing function, but how would I see the logs of a brand new function in a branch deploy if itā€™s not listed in the site web UI?

Apologies again for the massive post. Appreciate you all and hope I can contribute answers to others with the results of all the digging Iā€™ve done and the answers yā€™all have posted previously :smiley:

ā€“
Jon

CC @perry not for priority just for visibility for a backlog :sweat_smile: :pray:t2:

hi jonsully and welcome! i know its only been a short time but weā€™ve enjoyed having you here these last few days.

We havenā€™t had the capacity to read through your thoughtful post in depth, but we will and weā€™re thankful you took the time to write it :pray:

We do really love feedback as long as it is constructive, and we are all (as a team) excited to be working on supporting a product suite that people care about!

More soon.

1 Like

Thanks @perry :slight_smile: any thoughts here? I think all of these topics still apply.

Iā€™ll try to answer some of your questions. :slight_smile:
For Env Vars, yes, toml-defined env vars do not currently make it into lambda functions. A work around would be to modify your function directly based on context. Something like whatā€™s mentioned here: File-based configuration | Netlify Docs.

For any cli issues, I think filing an issue here: Issues Ā· netlify/cli Ā· GitHub would probably get you the best answer.

As far as event-triggered functions, I would say ease of use and the payload would be the main reason I would use them. But as you said, chaining a function invocation after a form submit works too! You do lose the specific payload (like the submission data) but you can probably do some api call ( Netlify API documentation) to retrieve that data. It really depends on what you want to do.

Let me know if that helps and thanks for being awesome! :slight_smile:

For those coming back around to see this, I ended up digging into multi-environment ENV var support pretty deep. There are two good ways to get environment-specific ENV vars running on Netlify. Rock on!

ā€“
Jon

1 Like