Skip to content

Error when using dynamic keys in for_each statements #839

Open
terraform-docs/terraform-config-inspect
#3
@the-real-mathew-moon

Description

@the-real-mathew-moon

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions