terraform-cdk icon indicating copy to clipboard operation
terraform-cdk copied to clipboard

Support for CLI variables

Open cmclaughlin opened this issue 5 years ago • 8 comments

Community Note

  • Please vote on this issue by adding a šŸ‘ reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

Add CLI args: -var foo=bar and -var-file=foo to be passed to Terraform.

For commands that wrap Terraform, such as cdktf deploy, it would be nice to be able to pass in variables at runtime.

For instance, you might want to deploy a new container to Fargate/ECS/EKS and pass in something like app_version=1.0.

cmclaughlin avatar Aug 26 '20 05:08 cmclaughlin

While it's good to have native support to generate native Terraform variables, I see the use-case for these more in the direction of generating hybrid modules and perhaps when using Terraform CDK as some sort of template generator. However, both use-cases are not in the focus at the moment.

Terraform variables are not a primary objective for Terraform CDK workflows. If you're using the cdktf cli to orchestrate your Terraform workflows, there are plenty of other ways to configure your stacks dynamically already:

  • ENV
  • Custom configuration files
  • Remote calls (e.g. APIs)

I do agree that it'd be good to have a way to provide custom arguments to the cdktf cli. However, I would think more of something like Context to address this need.

For instance, you might want to deploy a new container to Fargate/ECS/EKS and pass in something like app_version=1.0.

How about APP_VERSION=1.0 cdktf deploy?

skorfmann avatar Aug 26 '20 08:08 skorfmann

I agree that for fresh projects Terraform variables don't really seem like the best use of cdktf; however, I think they could be useful in migrating existing Terraform workflows into cdktf. This would allow easy separation of hcl -> typescript/python/etc and the configuration values used. Maybe it's a mute point since running cdktf will already be a change, but worth considering.

jsteinich avatar Aug 26 '20 13:08 jsteinich

Yeah, I’m using ENV vars and Yaml input files now. That is good enough for me.

cmclaughlin avatar Aug 26 '20 14:08 cmclaughlin

@cmclaughlin how did you get this to work? do you have a sample code I can use? I am having similar requirement

saikirangit avatar Apr 30 '21 18:04 saikirangit

I ended up using environment variables.

cmclaughlin avatar Apr 30 '21 18:04 cmclaughlin

I think it is already possible to set Terraform variables via environment variables (TF_VAR_app_version=0.1 cdktf deploy). A quick win here would be to document this in our docs – however, we should also figure out what we want to recommend in which case (e.g. environment variables accessed in synth vs. Terraform variables).

ansgarm avatar Feb 15 '22 14:02 ansgarm

Is there any intention of adding this to the vars files? Just trying to plan how we should approach this and also if it would be welcomed if we did a PR to add it?

slmingol avatar May 12 '22 13:05 slmingol

Is there any intention of adding this to the vars files? Just trying to plan how we should approach this and also if it would be welcomed if we did a PR to add it?

This comes up often enough (recently https://github.com/hashicorp/terraform-cdk/issues/734), that it seems worthwhile to come up with a proposal and evaluate more fully. Even though falling back to direct Terraform execution is always a possibility, it is a disruption.

jsteinich avatar May 17 '22 01:05 jsteinich

I'm going to lock this issue because it has been closed for 30 days. This helps our maintainers find and focus on the active issues. If you've found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

github-actions[bot] avatar Feb 10 '23 01:02 github-actions[bot]