-
Notifications
You must be signed in to change notification settings - Fork 93
terraform-validator fails to validate resources if project id is (known after apply) #139
Comments
It looks like this occurs when the project-factory |
Exact same issue here with a new project being assigned a random project ID and the same version of terraform-validator for Linux.
@travmcvey Would you mind detailing your workaround? I am not sure which flag to set to |
@lvaylet - @travmcvey is referring to https://github.com/terraform-google-modules/terraform-google-project-factory. The workaround is to set the "random_project_id" variable to false here. I was able to validate the same behavior on my end. Looks like terraform-validator expects the project_id to be known ahead of time. When assigning a project_id by appending a random hash, the project_id is only known after the apply. |
Thank you @mw-marcingluc. Then I guess I am in a slightly different situation then. I also append a random two-letter string at the end of new projects. However, I use the |
Hey @lvaylet - I think you could resolve this by passing the random ID as a variable, as a work-around. It seems to be an issue when the project-id is not unknown until after the apply. I don't know much more than that. Thank you! |
Hey @travmcvey - do you mean I should try defining a Terraform variable like |
Close! I am assuming you are doing something like this: resource "random_id" "random_project_id_suffix" {
byte_length = 2
}
locals {
base_project_id = var.project_name
new_project_id = format("%s-%s", local.base_project_id, random_id.random_project_id_suffix.hex)
}
resource "google_project" "new_project" {
name = var.name
project_id = local.new_project_id
} This is resulting in a new random id, done at apply. I am suggesting to, get around the validator error, which still needs to be fixed. Add in some logic for the random generator variables.tf: variable "random_generator" {
type = bool
default = true
}
variable "custom_random" {
type = number
default = 00
} Then in locals.tf modify this: locals {
base_project_id = var.project_name
new_project_id = var.random_generator ? format("%s-%s", local.base_project_id, random_id.random_project_id_suffix.hex) : format("%s-%s", local.base_project_id, var.custom_random)
} Then when running validator you would set |
You guessed right. Our code is very similar. Now I see how we can combine the |
You are welcome! Awesome, I am so glad to hear this is sufficient work-around! Let's hope they resolve the underlying issue. Regards, |
It seems that this and #132 related. |
Internal issue: b/211887519 |
validator version: gs://terraform-validator/releases/2020-03-05/terraform-validator-linux-amd64
I am attempting to deploy a single project and run a basic validation to see if only a required set of API's are enabled.
TF plan:
When running validator: ~/terraform-validator-linux-amd64 validate plan.json --policy-path=policy-library
I encounter the following error:
~/terraform-validator-linux-amd64 validate plan.json --policy-path=policy-library Error: converting tfplan to CAI assets: converting resource google_project: converting asset: project: required field is not set
I am able to successfully get validator to work by specify a random project id available in the org:
~/terraform-validator-linux-amd64 validate plan.json --policy-path=policy-library --project valid-project-id-of-random-project
It looks like validator is failing because the state file does not yet have a valid project_id that it can query against. From poking around,I believe the
--ancestry
flag is designed to combat this issue but I have not been able to get this to work. Folders themselves are created outside of this main.tf so those are also not available in the state.$ ~/terraform-validator-linux-amd64 validate plan.json --policy-path=policy-library --ancestry=organization/redactedID/folder/redactedID Error: converting tfplan to CAI assets: converting resource google_project: converting asset: project: required field is not set
$ ~/terraform-validator-linux-amd64 validate plan.json --policy-path=policy-library --ancestry=organization/redactedID/folder/redactedID --offline Error: converting tfplan to CAI assets: converting resource google_project: converting asset: project: required field is not set
The text was updated successfully, but these errors were encountered: