Terraform AWS提供程序:在子模块中添加默认标记

6

我正在使用Terraform AWS提供者中的default_tags块在根模块my_terraform中。该模块具有名为my_submodule的子模块,并且我想在该子模块中有其他默认标记。我尝试在my_terraform/my_submodule/main.tf中执行此操作:

provider "aws" {
  default_tags {
    tags = {
      "extra_tag" = "something"
    }
  }
}

但是我收到了这个错误:

$ terraform init
Initializing modules...
- my_terraform.my_submodule in my_terraform/my_submodule
There are some problems with the configuration, described below.

The Terraform configuration must be valid before initialization so that
Terraform can determine which modules and providers need to be installed.
╷
│ Error: Module module.my_submodule contains provider configuration
│ 
│ Providers cannot be configured within modules using count, for_each or depends_on.

有没有什么办法可以解决这个问题?

我认为错误信息有误导性。实际上,该错误是针对已声明模块中的提供程序配置抛出的。这种模式绝对是不被鼓励的,但也可能是在那个版本中的错误。 - Matt Schuchard
1个回答

3
一种解决方法是为每个所需的标签集创建不同的别名提供程序块,并将其明确地传递给每个子模块。
provider "aws" {
  default_tags {
    tags = {
      default_tag = "asdf"
    }
  }
}
provider "aws" {
  alias  = "foo"
  default_tags {
    tags = {
      default_tag = "asdf"
      extra_tag   = "foo"
    }
  }
}
provider "aws" {
  alias  = "bar"
  default_tags {
    tags = {
      default_tag = "asdf"
      extra_tag   = "bar"
    }
  }
}

module "foo" {
  source = "something"
  providers = {
    aws = aws.foo
  }
}
module "bar" {
  source = "something"
  providers = {
    aws = aws.bar
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接