Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Error when using dynamic keys in for_each statements #839

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

Open
the-real-mathew-moon opened this issue Mar 6, 2025 · 4 comments Β· May be fixed by terraform-docs/terraform-config-inspect#3
Open

Comments

@the-real-mathew-moon
Copy link

Describe the bug

When a resource, datasource or module's provider uses a variable as the key to the a provider generated with a for_each statement the following error is returned:

>  terraform-docs markdown document .                                                                                                                            
Error: Invalid provider reference: Provider argument requires a provider name followed by an optional alias, like "aws.foo".

How can we reproduce it?

Use a for_each statement to generate a provider, then reference the provider using something dynamic, such as a variable, local, etc.

terraform {
    required_providers {
        aws = {
            source = "hashicorp/aws"
        }
    }
}

locals {
    provider_names = ["foo"]
}

provider "aws" {
    for_each = toset(local.provider_names)
    alias = "my_aws_providers"
}

data "aws_caller_identity" "current" {
    provider = aws.my_aws_providers[local.provider_names[0]]
}

Environment information

Terraform Docs version:

> terraform-docs --version
OpenTofu v1.9.0
on darwin_arm64
+ provider registry.opentofu.org/hashicorp/aws v5.89.0

OS: MacOS Sonoma 14.7 on Apple M3 Pro

@pascal-hofmann
Copy link
Contributor

pascal-hofmann commented Mar 7, 2025

Hi @the-real-mathew-moon,
for_each in provider blocks is a feature unique to opentofu and currently not supported by terraform-docs.

PRs for this feature are welcome.

@the-real-mathew-moon
Copy link
Author

the-real-mathew-moon commented Mar 11, 2025

I would respectfully argue that it is indeed a bug since terraform-docs actually does support generating docs with for_each in a provider. It only chokes when the the resource or module's provider configuration contains a key that is a dynamic reference. EG: The following is supported

data "aws_caller_identity" "current" {
    provider = aws.my_aws_providers["foo"]
}

But this fails:

data "aws_caller_identity" "current" {
    provider = aws.my_aws_providers[local.provider_names[0]]
}

Both utilize a provider that was generated with a for_each the only difference is that the latter uses a dynamic reference for the key on the provider object.

@crash-bandi
Copy link

I ran into this issue as well and I found the issue causing this. It is an issue with the imported terraform-config-inspect code. As far as I can tell, there isn't much that can be done in the terraform-docs code base, at least not in a way that is appropriate. terraform-config-inspect does not appear to accept external pull requests, but I put one out there just incase I can get this issue fixed.

@SizZiKe
Copy link

SizZiKe commented May 1, 2025

@the-real-mathew-moon can you please take a look at @crash-bandi 's PR, or help prioritize this? Both opentofu and terraform support for_each providers as of 1.9. The new feature is breaking all of our tfdocs pre-commit hooks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants