Skip to content
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

TerraformVariable: unclear how to use validation conditions in python #3571

Open
1 task
MartinLoeper opened this issue Mar 30, 2024 · 1 comment
Open
1 task
Labels
documentation Improvements or additions to documentation new Un-triaged issue

Comments

@MartinLoeper
Copy link

Description

I am a fairly new cdktf user and try to add a validation condition on the env variable s.t. it can only be set to "staging" or "production". However, no matter what I try, cdktf refuses to evaluate my expression.

I searched the docs and could not find any information on how to specify the condition property.

What I expected to work:

env = TerraformVariable(self, "env", type="string")

env.add_validation(
    condition=f"contains(['production', 'staging'], {env.string_value})",
    error_message="ERROR: Valid types are production and staging"
)

However, it failed with:

 Error: Invalid variable validation result
   │ 
   │   on cdk.tf.json line 81, in variable.env.validation[0]:
   │   81:           "condition": "contains(['production', 'staging'], ${var.env})",
   │     ├────────────────
   │     │ var.env is "staging"
   │ 
   │ Invalid validation condition result value: a bool is required.

I also tried to reference the input variable as follows:

env.add_validation(
    condition=f"contains(['production', 'staging'], var.env)",
    error_message="ERROR: Valid types are production and staging"
)

... which failed with:

│ Error: Invalid validation expression
   │ 
   │   on cdk.tf.json line 81, in variable.env.validation[0]:
   │   81:           "condition": "contains(['production', 'staging'], var.env)",
   │ 
   │ The condition expression must refer to at least one object from elsewhere in
   │ the configuration, or else its result would not be checking anything.

│ Error: Invalid variable validation condition
   │ 
   │   on cdk.tf.json line 81, in variable.env.validation[0]:
   │   81:           "condition": "contains(['production', 'staging'], var.env)",
   │ 
   │ The condition for variable "env" must refer to var.env in order to test
   │ incoming values.

Does not make sense to me, as I included var.env into the condition and it looks similar as examples in the non-cdk docs.

Could someone please shed some light on that topic?

Links

Help Wanted

  • I'm interested in contributing a fix myself

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
@MartinLoeper MartinLoeper added documentation Improvements or additions to documentation new Un-triaged issue labels Mar 30, 2024
@nbaju1
Copy link

nbaju1 commented May 27, 2024

If you check your resulting configuration, you see that the condition parameter is synthesized as a string, not a hcl boolean.

Either add expression syntax to the condition string:

env = TerraformVariable(self, "env", type="string")

env.add_validation(
    condition=f"${{contains(['production', 'staging'], {env.string_value})}}",
    error_message="ERROR: Valid types are production and staging"
)

or use the CDKTF equivalent of the contains function.

from cdktf import TerraformVariable, FnGenerated
env = TerraformVariable(self, "env", type="string")

env.add_validation(
    condition=FnGenerated.contains(["production", "staging"], env.string_value),
    error_message="ERROR: Valid types are production and staging"
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation new Un-triaged issue
Projects
None yet
Development

No branches or pull requests

2 participants