The docs say that I can access environment variables like this:
database_name = "${?DB_NAME}"
Where DB_NAME
is an environment variable. Do I have to put the question mark ?
or is that there to prevent a crash if it doesn't exist?
Also, are we allowed to reference a variable in a file before it is declared?
foo = "hello-${bar}"
bar = "baz"
1- ?
means optional. If the environment variable does not exist at runtime, the whole line will be ignored, as if it didn't exists in the first place. So use it when you want optional overriding.
See Optional Env Vars section of the docs for more info.
Note that ?
works for all substitutions and not just env vars.
2- Yes, you can reference a variable which is defined later in the config file (forward referencing). As long as the value exists and there is no cycle (circular dependency), the substitution will be done successfully.
Think of it like this: first the whole file will be parsed at runtime and the value of the literals will be bound to their vars, and then the substitutions will take place, so the order doesn't really matter.