Skip to content

Commit d8e309d

Browse files
mbainteryermulnik
andcommitted
make improvements from PR feedback
Co-authored-by: George L. Yermulnik <yz@yz.kiev.ua>
1 parent 4a7a7a0 commit d8e309d

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

README.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -551,21 +551,21 @@ Example:
551551

552552
**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.
553553

554-
5. `terraform_validate` in a 0.15+ module with configuration_aliases errors out
554+
5. `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.
555555

556-
When running the hook where you have configuration_aliases defined as required in the terraform block, terraform will throw an error like:
556+
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:
557557
>
558558
>
559559
> Error: Provider configuration not present
560560
> 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
561561
> 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.
562562

563-
This is a [known issue](https://github.com/hashicorp/terraform/issues/28490) with Terraform and how providers are initialized in 0.15 and later. To work around this for now you can add an exclude to the `terraform_validate` hook like this:
563+
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:
564564
```yaml
565565
- id: terraform_validate
566-
exclude: [^/].+$
566+
exclude: [^/]+$
567567
```
568-
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.
568+
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/[^/]+$`.
569569

570570
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:
571571

@@ -581,20 +581,24 @@ Example:
581581
' | tee aliased-providers.tf.json
582582
```
583583

584-
Save this as .generate-providers.sh in the root of your repository and place it in the and add a hook to run that first, like so:
584+
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:
585585
```yaml
586-
- repo: local
587-
hooks:
588-
- id: generate-terraform-providers
589-
name: generate-terraform-providers
590-
require_serial: true
591-
entry: .generate-providers.sh
592-
language: script
593-
files: (\.tf|\.tfvars)$
594-
pass_filenames: false
586+
- repos:
587+
- repo: local
588+
hooks:
589+
- id: generate-terraform-providers
590+
name: generate-terraform-providers
591+
require_serial: true
592+
entry: .generate-providers.sh
593+
language: script
594+
files: \.tf(vars)?$
595+
pass_filenames: false
596+
597+
- repo: https://github.com/pre-commit/pre-commit-hooks
598+
[...]
595599
```
596600

597-
**Note:** This latter method will leave an "aliased-providers.tf.json" file in your path. You will either want to automate a way to clean this up or add it to your gitignore or both.
601+
**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.
598602

599603
### terrascan
600604

0 commit comments

Comments
 (0)