is there any way to do build-time env vars instead...
# help
s
is there any way to do build-time env vars instead of runtime? it seems like anything in .env.* has to be passed into Lambda functions using the
environment
property. but this can get pretty tedious. is there some way for env vars to get evaluated and baked into the code?
Vue does this. so if I reference
process.env.VUE_APP_HOWDY
in the code, upon build, it’s actually replaced with whatever is in
.env.[stage]
a
Use dotenv to process the files and pass them all into the Lambda
environment
.
s
yeah.. that’s what I’m doing now. it’s a bit tedious to have to chain everything through like that.
so .env.* has TEST=1 for example. then for EVERY Lambda function that needs that var, I have to do:
Copy code
environment: {
  TEST: process.env.TEST
}
a
Oh, I figured dotenv would have a load function that would just turn everything in those files into one object that you pass to
environment
. Looks like it's just the parser, not the file loading. Weird.
s
I mean, I could set a bunch of these in the default Lambda props.. but it’s kinda overkill to have a whole bunch of env vars in functions where they’re not needed
the dangerous thing is, when your code is highly modular and you have Lambda functions calling all sorts of other functions, it’s hard to remember which of those functions will be referencing env vars
this has gotten me in trouble more than a few times because some Lambda func called a func which called another func which needed
process.env.SOME_API_KEY
and I didn’t pass that into the Lambda func’s environment
r
So I struggled with this as well. I did end up going with the default function env's. It can get a bit much but truth is I am not really looking at that after the fact. Its not like I am digging into my lambda ENV table on AWS often 🙂 However you may be able to pull this off with an esbuild plugin. I know there are a few ways to do this. Would have to do some searching but you should be able to get esbuild to transform for you. It even has the built in define config (https://esbuild.github.io/api/#define). Obviously downside to this is you will need to recompile code if you change an ENV. Truth is what you are looking for is less ENV and more variable substitution.