You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+44-17Lines changed: 44 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -80,7 +80,7 @@ If you are using `pre-commit-terraform` already or want to support its developme
80
80
*[`TFLint`](https://github.com/terraform-linters/tflint) required for `terraform_tflint` hook.
81
81
*[`TFSec`](https://github.com/liamg/tfsec) required for `terraform_tfsec` hook.
82
82
*[`infracost`](https://github.com/infracost/infracost) required for `infracost_breakdown` hook.
83
-
*[`jq`](https://github.com/stedolan/jq) required for `infracost_breakdown` hook.
83
+
*[`jq`](https://github.com/stedolan/jq) required for `terraform_validate` with `--retry-once-with-cleanup` flag, and for `infracost_breakdown` hook.
84
84
*[`tfupdate`](https://github.com/minamijoyo/tfupdate) required for `tfupdate` hook.
85
85
*[`hcledit`](https://github.com/minamijoyo/hcledit) required for `terraform_wrapper_module_for_each` hook.
86
86
@@ -238,7 +238,7 @@ pre-commit run -a
238
238
239
239
Or, using Docker ([available tags](https://github.com/antonbabenko/pre-commit-terraform/pkgs/container/pre-commit-terraform/versions)):
240
240
241
-
**NOTE:** This command uses your user id and group id for the docker container to use to access the local files. If the files are owned by another user, update the `USERID` environment variable. See [File Permissions section](#docker-usage-file-permissions) for more information.
241
+
> Note: This command uses your user id and group id for the docker container to use to access the local files. If the files are owned by another user, update the `USERID` environment variable. See [File Permissions section](#docker-usage-file-permissions) for more information.
242
242
243
243
```bash
244
244
TAG=latest
@@ -258,7 +258,7 @@ There are several [pre-commit](https://pre-commit.com/) hooks to keep Terraform
258
258
259
259
<!-- markdownlint-disable no-inline-html -->
260
260
| Hook name | Description | Dependencies<br><sup>[Install instructions here](#1-install-dependencies)</sup> |
|`checkov` and `terraform_checkov`|[checkov](https://github.com/bridgecrewio/checkov) static analysis of terraform templates to spot potential security issues. [Hook notes](#checkov-deprecated-and-terraform_checkov)|`checkov`<br>Ubuntu deps: `python3`, `python3-pip`|
263
263
|`infracost_breakdown`| Check how much your infra costs with [infracost](https://github.com/infracost/infracost). [Hook notes](#infracost_breakdown)|`infracost`, `jq`, [Infracost API key](https://www.infracost.io/docs/#2-get-api-key)|
264
264
|`terraform_docs`| Inserts input and output documentation into `README.md`. Recommended. [Hook notes](#terraform_docs)|`terraform-docs`|
@@ -268,11 +268,11 @@ There are several [pre-commit](https://pre-commit.com/) hooks to keep Terraform
|`terraform_tflint`| Validates all Terraform configuration files with [TFLint](https://github.com/terraform-linters/tflint). [Available TFLint rules](https://github.com/terraform-linters/tflint/tree/master/docs/rules#rules). [Hook notes](#terraform_tflint). |`tflint`|
270
270
|`terraform_tfsec`|[TFSec](https://github.com/aquasecurity/tfsec) static analysis of terraform templates to spot potential security issues. [Hook notes](#terraform_tfsec)|`tfsec`|
271
-
|`terraform_validate`| Validates all Terraform configuration files. [Hook notes](#terraform_validate)|- |
271
+
|`terraform_validate`| Validates all Terraform configuration files. [Hook notes](#terraform_validate)|`jq`, only for `--retry-once-with-cleanup` flag|
272
272
|`terragrunt_fmt`| Reformat all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) to a canonical format. |`terragrunt`|
273
273
|`terragrunt_validate`| Validates all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) |`terragrunt`|
274
274
|`terraform_wrapper_module_for_each`| Generates Terraform wrappers with `for_each` in module. [Hook notes](#terraform_wrapper_module_for_each)|`hcledit`|
275
-
|`terrascan`|[terrascan](https://github.com/tenable/terrascan) Detect compliance and security violations. [Hook notes](#terrascan)|`terrascan`|
275
+
|`terrascan`|[terrascan](https://github.com/tenable/terrascan) Detect compliance and security violations. [Hook notes](#terrascan)|`terrascan`|
276
276
|`tfupdate`|[tfupdate](https://github.com/minamijoyo/tfupdate) Update version constraints of Terraform core, providers, and modules. [Hook notes](#tfupdate)|`tfupdate`|
277
277
<!-- markdownlint-enable no-inline-html -->
278
278
@@ -284,8 +284,9 @@ Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blo
284
284
285
285
> All, except deprecated hooks: `checkov`, `terraform_docs_replace`
286
286
287
-
You can use environment variables for the `--args` section.
288
-
Note: You _must_ use the `${ENV_VAR}` definition, `$ENV_VAR` will not expand.
287
+
You can use environment variables for the `--args` section.
288
+
289
+
> **Warning**: You _must_ use the `${ENV_VAR}` definition, `$ENV_VAR` will not expand.
289
290
290
291
Config example:
291
292
@@ -486,7 +487,7 @@ Unlike most other hooks, this hook triggers once if there are any changed files
486
487
- --args=--config=.terraform-docs.yml
487
488
```
488
489
489
-
Note: Avoid use `recursive.enabled: true` in config file, that can cause unexpected behavior.
490
+
> **Warning**: Avoid use `recursive.enabled: true` in config file, that can cause unexpected behavior.
490
491
491
492
5. If you need some exotic settings, it can be done too. I.e. this one generates HCL files:
492
493
@@ -540,7 +541,7 @@ To replicate functionality in `terraform_docs` hook:
540
541
541
542
1. The hook requires Terraform 0.14 or later.
542
543
2. The hook invokes two operations that can be really slow:
543
-
* `terraform init` (in case `.terraform` directory is not initialised)
544
+
* `terraform init` (in case `.terraform` directory is not initialized)
544
545
* `terraform providers lock`
545
546
546
547
Both operations require downloading data from remote Terraform registries, and not all of that downloaded data or meta-data is currently being cached by Terraform.
@@ -675,7 +676,31 @@ To replicate functionality in `terraform_docs` hook:
675
676
- --tf-init-args=-lockfile=readonly
676
677
```
677
678
678
-
3. It may happen that Terraform working directory (`.terraform`) already exists but not in the best condition (eg, not initialized modules, wrong version of Terraform, etc.). To solve this problem, you can find and delete all `.terraform` directories in your repository:
679
+
3. It may happen that Terraform working directory (`.terraform`) already exists but not in the best condition (eg, not initialized modules, wrong version of Terraform, etc.). To solve this problem, you can delete broken `.terraform` directories in your repository:
680
+
681
+
**Option 1**
682
+
683
+
```yaml
684
+
- id: terraform_validate
685
+
args:
686
+
- --hook-config=--retry-once-with-cleanup=true # Boolean. true or false
687
+
```
688
+
689
+
> Note: The flag requires additional dependency to be installed: `jq`.
690
+
691
+
If `--retry-once-with-cleanup=true`, then in each failed directory the cached modules and providers from the `.terraform` directory will be deleted, before retrying once more. To avoid unnecessary deletion of this directory, the cleanup and retry will only happen if Terraform produces any of the following error messages:
692
+
693
+
* "Missing or corrupted provider plugins"
694
+
* "Module source has changed"
695
+
* "Module version requirements have changed"
696
+
* "Module not installed"
697
+
* "Could not load plugin"
698
+
699
+
**Warning:** When using `--retry-once-with-cleanup=true`, problematic `.terraform/modules/` and `.terraform/providers/` directories will be recursively deleted without prompting for consent. Other files and directories will not be affected, such as the `.terraform/environment` file.
700
+
701
+
**Option 2**
702
+
703
+
An alternative solution is to find and delete all `.terraform` directories in your repository:
679
704
680
705
```bash
681
706
echo "
@@ -689,22 +714,23 @@ To replicate functionality in `terraform_docs` hook:
689
714
690
715
`terraform_validate`hook will try to reinitialize them before running the `terraform validate` command.
691
716
692
-
**Warning:** If you use Terraform workspaces, DO NOT use this workaround ([details](https://github.com/antonbabenko/pre-commit-terraform/issues/203#issuecomment-918791847)). Wait to [`force-init`](https://github.com/antonbabenko/pre-commit-terraform/issues/224) option implementation.
717
+
**Warning:** If you use Terraform workspaces, DO NOT use this option ([details](https://github.com/antonbabenko/pre-commit-terraform/issues/203#issuecomment-918791847)). Consider the first option, or wait for [`force-init`](https://github.com/antonbabenko/pre-commit-terraform/issues/224) option implementation.
693
718
694
719
4. `terraform_validate` in a repo with Terraform module, written using Terraform 0.15+ and which uses provider `configuration_aliases` ([Provider Aliases Within Modules](https://www.terraform.io/language/modules/develop/providers#provider-aliases-within-modules)), errors out.
695
720
696
721
When running the hook against Terraform code where you have provider `configuration_aliases` defined in a `required_providers` configuration block, terraform will throw an error like:
697
-
>
698
-
>
722
+
699
723
> Error: Provider configuration not present
700
-
> To work with <resource> its original provider configuration at provider["registry.terraform.io/hashicorp/aws"].<provider_alias> is required, but it has been removed. This occurs when a provider configuration is removed while
701
-
> objects created by that provider still exist in the state. Re-add the provider configuration to destroy <resource>, after which you can remove the provider configuration again.
724
+
> To work with `<resource>` its original provider configuration at provider `["registry.terraform.io/hashicorp/aws"].<provider_alias>` is required, but it has been removed. This occurs when a provider configuration is removed while
725
+
> objects created by that provider still exist in the state. Re-add the provider configuration to destroy `<resource>`, after which you can remove the provider configuration again.
702
726
703
727
This is a [known issue](https://github.com/hashicorp/terraform/issues/28490) with Terraform and how providers are initialized in Terraform 0.15 and later. To work around this you can add an `exclude` parameter to the configuration of `terraform_validate` hook like this:
728
+
704
729
```yaml
705
730
- id: terraform_validate
706
731
exclude: '^[^/]+$'
707
732
```
733
+
708
734
This will exclude the root directory from being processed by this hook. Then add a subdirectory like "examples" or "tests" and put an example implementation in place that defines the providers with the proper aliases, and this will give you validation of your module through the example. If instead you are using this with multiple modules in one repository you'll want to set the path prefix in the regular expression, such as `exclude: modules/offendingmodule/[^/]+$`.
709
735
710
736
Alternately, you can use [terraform-config-inspect](https://github.com/hashicorp/terraform-config-inspect) and use a variant of [this script](https://github.com/bendrucker/terraform-configuration-aliases-action/blob/main/providers.sh) to generate a providers file at runtime:
@@ -722,6 +748,7 @@ To replicate functionality in `terraform_docs` hook:
722
748
```
723
749
724
750
Save it as `.generate-providers.sh` in the root of your repository and add a `pre-commit` hook to run it before all other hooks, like so:
751
+
725
752
```yaml
726
753
- repos:
727
754
- repo: local
@@ -738,7 +765,7 @@ To replicate functionality in `terraform_docs` hook:
738
765
[...]
739
766
```
740
767
741
-
**Note:** The latter method will leave an "aliased-providers.tf.json" file in your repo. You will either want to automate a way to clean this up or add it to your `.gitignore` or both.
768
+
> Note: The latter method will leave an "aliased-providers.tf.json" file in your repo. You will either want to automate a way to clean this up or add it to your `.gitignore` or both.
742
769
743
770
### terraform_wrapper_module_for_each
744
771
@@ -784,7 +811,7 @@ If the generated name is incorrect, set them by providing the `module-repo-short
784
811
785
812
See the `terrascan run -h` command line help for available options.
786
813
787
-
2. Use the `--args=--verbose` parameter to see the rule ID in the scaning output. Usuful to skip validations.
814
+
2. Use the `--args=--verbose` parameter to see the rule ID in the scanning output. Useful to skip validations.
788
815
3. Use `--skip-rules="ruleID1,ruleID2"` parameter to skip one or more rules globally while scanning (e.g.: `--args=--skip-rules="ruleID1,ruleID2"`).
789
816
4. Use the syntax `#ts:skip=RuleID optional_comment` inside a resource to skip the rule for that resource.
0 commit comments