hashicorp/terraform-cdk

Add support for Terraform Variables

skorfmann opened this issue ยท 15 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

It would be good to support Terraform variables as first class construct. This will be helpful for hybrid approaches and for creating a Terraform module from constructs.

I'm not sure i understand correctly, but does this issue means that we can't use terraform variable right now using CDK ?

I'm not sure i understand correctly, but does this issue means that we can't use terraform variable right now using CDK ?

There's no explicit construct to support variables at the moment. Variables still can be supported via escape hatches. See this for a full example.

Here an excerpt:

stack.addOverride('variable', {
  tags: {
    description: "Tags for the instance",
    type: "map(string)"
  },
  instance_type: {
    description: "Instance type",
    type: "string"
  }
})

@skorfmann Thanks for the quick answer, i believe adding this into the readme would be helpful to people looking to migrate to CDK :)

@skorfmann Thanks for the quick answer, i believe adding this into the readme would be helpful to people looking to migrate to CDK :)

No, worries. Adding this to the docs would make sense, yes ๐Ÿ‘

Out of curiosity, could you elaborate on your use-case for variables?

Sure, here is some options (in TS) on the infrastructure i'm trying to migrate:

type InfrastructureToolingOptions = {
  environment: string
  gcp_credentials: string
  sva_dns: string
  project: string
  region: string

  gke: {
    pool_min: number
    pool_max: number
    pool_type: number
    version: string,
    nodes_cidr: string
    pods_cidr: string
    master_cidr: string
  }
}

There obviously some default that i'm setting in the code directly, but i like to be able to override them on the UI (and allows for non-ops to edit them too). For some variables which are secret like gcp_credentials or sva_dns they are all set inside TF Cloud as "secret"

Thanks, that's quite interesting. I had primarily modules as a use-case for this in mind. Terraform Cloud looks certainly like another very compelling use-case for this /cc @anubhavmishra

@vmarchaud how do you approach the CDK in Terraform Cloud? Commit the synthesized JSON and use this a basis for the Terraform Cloud?

Thanks, that's quite interesting. I had primarily modules as a use-case for this in mind.

Well after seeing your examples to use the variables, i believe making a module would be the easiest way to use them.

how do you approach the CDK in Terraform Cloud? Commit the synthesized JSON and use this a basis for the Terraform Cloud?

Well that's a unknown for me, from what i read i can use the RemoteState and the local apply, it should be able to read the variables on Terraform Cloud and apply them locally ?

EDIT: Worth to mention that this project (~30 resources) is quite small so its pretty much a playground to try make it works. I would like to migrate my main infrastructure (with ~200 resources) later on though

Personally I'm still split between converting variables to code/configuration files and keeping them around. We are using Terraform Atlantis, so I could add in a step to CDK synth, but that requires adding in the build dependencies.

Having variable support would be a nice stepping stone to convert existing infrastructure though.

EDIT: Worth to mention that this project (~30 resources) is quite small so its pretty much a playground to try make it works. I would like to migrate my main infrastructure (with ~200 resources) later on though

Yes, please be also aware that this project is still considered alpha with frequent (breaking) changes.

Well that's a unknown for me, from what i read i can use the RemoteState and the local apply, it should be able to read the variables on Terraform Cloud and apply them locally ?

I guess so, haven't used Terraform Cloud that much yet. The remote state is being worked on at the moment - see #241.

I thought you had Terraform Cloud hooked up to Github somehow, and it'd execute a deploy on your behalf.

Personally I'm still split between converting variables to code/configuration files and keeping them around. We are using Terraform Atlantis, so I could add in a step to CDK synth, but that requires adding in the build dependencies.

Not sure if I understand this correctly. If you wouldn't add a build step for the CDK, how would you ship it via Atlantis then?

Not sure if I understand this correctly. If you wouldn't add a build step for the CDK, how would you ship it via Atlantis then?

Just a question of whether Atlantis executes the step or something else does beforehand and passes along the synthesized JSON.

Hi Folks,
When do you expect this functionality to be available via npm?

When do you expect this functionality to be available via npm?

If you're using ts/js it should already be available using the @next version.

This was released in 0.0.18

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.