Search code examples
typescriptterraformterraform-cdk

How do you handle cdktf input


I know TerraformVariable class, which can handle TF_VAR_ environment variables. The CDKTF documentation suggest to use language API if possible

If you plan to use CDKTF to manage your infrastructure, we recommend using your language's APIs to consume the data you would normally pass through Terraform variables. You can read from disk (synchronously) or from the environment variables, just as you would in any normal program

I assume in typescript that means passing in variables using process.env. The issue I am facing right now is that in case, I want to be strict and make some of the variables compulsory, when running multiple stacks, the required variables in one, become required even in another.

Is there some trick, cool pattern I could use to get around it? I have even considered sniffing a stack name and validating input conditionally, however, I did not find anyway even of doing that

Thank you


Solution

  • If you want to have these types of checks you either need to pass the variables all the time, split your stacks up into different CDKTF applications or (and this would be what I would recommend) use the TerraformVariable Construct and let Terraform handle the validation at execution time (versus at synth time like you are describing now).

    You could theoretically set an environment variable indicating the stack you are about to deploy, but this can get very hacky very quickly.