terraform aws 提供程序:在子模块中添加默认标签

发布于 2025-01-11 05:23:17 字数 848 浏览 0 评论 0原文

我在根模块 my_terraform 中使用 terraform aws 提供程序 default_tags 块。该模块有一个名为 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.

有什么办法解决这个问题吗?

I'm using terraform aws provider default_tags block in a root module my_terraform. That module has a submodule called my_submodule, and I would like to have additional default tags in that submodule. I tried this in my_terraform/my_submodule/main.tf:

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

But I get this error:

$ 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.

Is there any way around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

下雨或天晴 2025-01-18 05:23:17

解决方法是为每个所需的标签集创建一个不同的别名提供程序块,并 显式地将其传递到每个子模块。

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
  }
}

A workaround is to create a different aliased provider block for each needed tag set and pass it explicitly to each submodule.

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
  }
}
七色彩虹 2025-01-18 05:23:17

您还可以在模块中的每个资源上手动定义标签,并使用全局变量调用它。

# Cloudwatch.tf

resource "aws_cloudwatch_log_group" "puma" {
  name              = "/${var.team_name}/puma"
  retention_in_days = 30
  tags = var.tags
}
# Variables.tf

variable "tags" {
  description = "Tags to apply to all resources"
  type        = map(string)
}
# parent file calling the module

locals {
  teams = {
    cheeze = {},
    pesto  = {}
  }
}

module "xxxx" {
  source   = "./xxxx/"
  for_each = local.teams

  team_name  = each.key
  # ...

  tags = {
    "Team" = each.key
  }
}

You can also define your tags manually on each resource in the module and call it with a global variable.

# Cloudwatch.tf

resource "aws_cloudwatch_log_group" "puma" {
  name              = "/${var.team_name}/puma"
  retention_in_days = 30
  tags = var.tags
}
# Variables.tf

variable "tags" {
  description = "Tags to apply to all resources"
  type        = map(string)
}
# parent file calling the module

locals {
  teams = {
    cheeze = {},
    pesto  = {}
  }
}

module "xxxx" {
  source   = "./xxxx/"
  for_each = local.teams

  team_name  = each.key
  # ...

  tags = {
    "Team" = each.key
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文