SAFEHR-data / FlowEHR

FlowEHR is a safe, secure & cloud-native development & deployment platform for digital healthcare research & innovation.
https://flowehr.io
Apache License 2.0
17 stars 12 forks source link

TFLint and TF validate pre-commit fails when terragrunt init hasn't been ran locally #275

Closed jjgriff93 closed 1 year ago

jjgriff93 commented 1 year ago

Describe the bug Because the terragrunt-generated files (providers.tf, terraform.tf etc.) are gitignored, if deployments haven't been ran locally before committing terraform file changes, the pre-commit terraform checks might fail and give output like this:

> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
[INFO] Checking merge-conflict files only.
Flake8...............................................(no files to check)Skipped
Fix End of Files.........................................................Passed
black................................................(no files to check)Skipped
Terraform fmt............................................................Passed
Terraform validate.......................................................Failed
- hook id: terraform_validate
- exit code: 1

'terraform init' failed, 'terraform validate' skipped: infrastructure/transform

Initializing provider plugins...
- Reusing previous version of hashicorp/time from the dependency lock file
- Reusing previous version of hashicorp/null from the dependency lock file
- Reusing previous version of hashicorp/random from the dependency lock file
- Reusing previous version of hashicorp/azurerm from the dependency lock file
- Reusing previous version of hashicorp/azuread from the dependency lock file
- Finding latest version of hashicorp/databricks...
- Using previously-installed hashicorp/azurerm v3.47.0
- Using previously-installed hashicorp/azuread v2.35.0
- Using previously-installed hashicorp/time v0.9.1
- Using previously-installed hashicorp/null v3.2.1
- Using previously-installed hashicorp/random v3.4.3
╷
│ Error: Failed to query available provider packages
│ 
│ Could not retrieve the list of available versions for provider
│ hashicorp/databricks: provider registry registry.terraform.io does not have
│ a provider named registry.terraform.io/hashicorp/databricks
│ 
│ All modules should specify their required_providers so that external
│ consumers will get the correct providers when using a module. To see which
│ modules are currently depending on hashicorp/databricks, run the following
│ command:
│     terraform providers
╵


Terragrunt fmt.......................................(no files to check)Skipped
Terraform validate with tflint...........................................Failed
- hook id: terraform_tflint
- exit code: 2

Command 'tflint --init' successfully done:
Plugin `terraform` is already installed
Plugin `azurerm` is already installed

TFLint in infrastructure/transform/:
7 issue(s) found:

Warning: terraform "required_version" attribute is required (terraform_required_version)

  on  line 0:
   (source code not available)

Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.2.2/docs/rules/terraform_required_version.md

Warning: Missing version constraint for provider "time" in "required_providers" (terraform_required_providers)

  on databricks.tf line 38:
  38: resource "time_sleep" "wait_for_databricks_network" {

Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.2.2/docs/rules/terraform_required_providers.md

Warning: Missing version constraint for provider "databricks" in "required_providers" (terraform_required_providers)

  on secrets.tf line 68:
  68: resource "databricks_secret" "flowehr_databricks_sql_database" {

Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.2.2/docs/rules/terraform_required_providers.md

shellcheck...........................................(no files to check)Skipped
yamllint.............................................(no files to check)Skipped
Ensure copyright and license headers are present.........................Passed

We ideally need to be able to tell the pre-commit hooks to run a terragrunt init before the terraform checks. We do this currently in the pr checks workflow but that only prevents this in GH, not locally.

jjgriff93 commented 1 year ago

For others hitting this issue, the workaround is to run make tf-reinit before committing

jjgriff93 commented 1 year ago

Fixed in #310