-
Notifications
You must be signed in to change notification settings - Fork 456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Env TF_VARS are not working and docs are outdated #2550
Comments
Hey thank you for swift response: #.envrc set env var
export TF_VAR_name="my_stack" ❯ pipenv run env | grep -i TF_VAR
TF_VAR_name=my_stack # main.py
#!/usr/bin/env python
from constructs import Construct
from cdktf import App, TerraformStack, TerraformOutput, TerraformVariable
from imports.aws.provider import AwsProvider
from imports.my_stack import MYStack
class MyStack(TerraformStack):
def __init__(self, scope: Construct, id: str):
super().__init__(scope, id)
AwsProvider(self, "aws")
name = TerraformVariable(self, "name", type="string")
localModule = MyStack(self, name=name)
app = App()
MyStack(app, "my_stack")
app.synth() ❯ cdktf deploy
...
my_stack Success! Terraform has validated the lock file and found no need for changes.
my_stack Missing variable: 'main_name_27FB85A4'. You can provide it using the 'TF_VAR_main_name_27FB85A4' environment variable.
0 Stacks deploying 1 Stack done 0 Stacks waiting
Invoking Terraform CLI failed with exit code 1 ❯ cdktf diff
my_stack Initializing modules...
my_stack Initializing the backend...
my_stack Initializing provider plugins...
...
my_stack Success! Terraform has validated the lock file and found no need for changes.
⠏ Processing
[2023-01-27T19:34:26.247] [ERROR] default - ╷
│ Error: No value for required variable
│
│ on cdk.tf.json line 98, in variable:
│ 98: "main_name_27FB85A4": {
│
│ The root module input variable "main_name_27FB85A4" is not set, and has
│ no default value. Use a -var or -var-file command line argument to provide
│ a value for this variable.
my_stack Missing variable: 'main_name_27FB85A4'. You can provide it using the 'TF_VAR_main_name_27FB85A4' environment variable. |
One more thing, I've no idea if expected or separate bug name = TerraformVariable(self, "name", type="string").string_value If don't supply correct value type for every var -
|
Hey @ansgarm please let me know if you need any additional info. |
Hi @reinis-s 👋 The problem seems to be that your variable gets assigned the name However, if the variable is defined in the root of the stack, it shouldn't get that name nowadays. What does your |
@ansgarm variable "name" {
type = string
} cdktf.json {
"language": "python",
"app": "pipenv run python main.py",
"projectId": "e6cdf273-a12e-4391-a940-599259475371",
"sendCrashReports": "false",
"terraformProviders": ["aws@~> 4.0"],
"terraformModules": [
{
"name": "vpc",
"source": "terraform-aws-modules/vpc/aws",
"version": "~> 3.0"
},
{
"name": "my_stack",
"source": "git::https://github.com/MY_REPO//modules/my_stack?ref=master"
}
],
"codeMakerOutput": "imports",
"context": {
"excludeStackIdFromLogicalIds": "true",
"allowSepCharsInLogicalIds": "true"
}
}
|
Hm, that's looking good so far. |
I've already pasted couple comments above? #2550 (comment) |
Hm, I'm only seeing a Maybe pasting the complete file would help – feel free to delete resources that seem superfluous but the general structure is required for me to assess the problem. |
I've updated # main.tf
terraform {
required_version = ">=1.1.7"
}
resource "aws_route53_zone" "zone" {
name = var.name
force_destroy = true
} # variables.tf
variable "name" {
type = string
} # provaider.tf
provider "aws" {
alias = "shared"
region = var.aws_shared_account_region
profile = var.aws_shared_account_profile
}
|
Thanks! This helps a bit, but I'm a bit confused about this part: class MyStack(TerraformStack):
def __init__(self, scope: Construct, id: str):
super().__init__(scope, id)
AwsProvider(self, "aws")
name = TerraformVariable(self, "name", type="string")
localModule = MyStack(self, name=name) # <-- wouldn't this recursively include MyStack within itself? How does your generated |
Ahh sorry my bad, that's typo - I remove company name and accidentally replaced with the same name. from imports.foo_stack import FooStack
class MyStack(TerraformStack):
def __init__(self, scope: Construct, id: str):
super().__init__(scope, id)
AwsProvider(self, "aws")
name = TerraformVariable(self, "name", type="string")
localModule = FooStack(self, name=name) |
Is there a |
No as far as I know - FooStack is just imported terraform module written in Basic setup:
|
Hm, I didn't know it was possible to supply input vars to modules from the outside. You'd need to pass them to your FooStack construct instead. |
At this point, it would really help, if you could publish a minimal example to e.g. a Github repository that I can use to reproduce the issue. |
Hi, I didn't import my stack from HCL but just wrote natively using the java lib. Any improvement on this issue? |
here is my code: fun main() {
val app = App()
TerraformVariable.Builder.create(app, "app_client_secret")
.type("string")
.sensitive(true)
.build()
SomeStack(app)
app.synth()
}
class SomeStack(scope: Construct) : TerraformStack(scope, "ctmx") {
init {
TerraformVariable.Builder.create(this, "stack_client_secret")
.type("string")
.sensitive(true)
.build()
}
} and here is the generated json: {
"//": {
"metadata": {
"backend": "local",
"stackName": "ctmx",
"version": "0.16.0-pre.43"
},
"outputs": {
}
},
"terraform": {
"backend": {
"local": {
"path": "/Users/ke/IdeaProjects/ctmx/ctd-infra/terraform.ctmx.tfstate"
}
}
},
"variable": {
"ctmx_stackclientsecret_274F4C75": {
"sensitive": true,
"type": "string"
}
}
} As you see if I supply the Lastly, in my cdktf.json, I have these settings which don't have any impact: "context": {
"excludeStackIdFromLogicalIds": "true",
"allowSepCharsInLogicalIds": "true"
} |
1 extra info about this. I tried the example from your github repo: terraform-cdk/examples/java/documentation/src/main/java/com/mycompany/app/MainHCL.java Line 26 in e8ad548
the resulting variable in json is this: "variable": {
"mainhcl_petNameLength_9F384CC9": {
"default": 2,
"description": "Pet name length",
"type": "number"
}
} Even the example in TF repo changes the var id while in the documentation it mentions that we can set the value of the variable using environment variable |
One more thing. It is not just about variables, synth changes all the names of all the terraform resources... |
Another finding: this doesn't happen in |
@kemalizing when observing these findings, are you running the application directly or running via If the scope isn't the root stack, then the hash will still be appended. It is possible to use the |
hi @jsteinich Do you know if there is any improvement on the issue that you linked? As mentioned there, having different names doesn't make any sense. Looks like, currently the only possible solution is the one you suggested, using |
Hi there! 👋 We haven't heard from you in 30 days and would like to know if the problem has been resolved or if you still need help. If we don't hear from you before then, I'll auto-close this issue in 30 days. |
I'm closing this issue because we haven't heard back in 60 days. ⌛️ If you still need help, feel free to comment or reopen the issue! |
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. |
Community Note
cdktf & Language Versions
Issue
I'm not sure was there as good reason to disable TF_VAR_* but documentation still says that we can use TF_VAR_.
What's the team current suggestion how dynamically inject config into CDKTF?
References
The text was updated successfully, but these errors were encountered: