替代条件陈述“计数”在Terraform

发布于 2025-02-12 02:30:59 字数 331 浏览 1 评论 0原文

我有一个只想在生产环境中构建的AWS资源。通常,我会使用类似的东西:

count = var.environment == "production" ? 1 : 0

但是,根据Terraform文档,在模块中用于Terraform低于13的版本中时不支持计数。语言/meta-arguments/count“ rel =“ nofollow noreferrer”>在Terraform 0.13中添加了对计数的模块支持,并且以前的版本只能将其用于资源。”)

我正在使用Terraform 12,并且资源为12来自模块,所以我想要一个只有在生产中构建资源的条件语句

I have a aws resource that i only want to be built in the production environment. Usually i would use something like this:

count = var.environment == "production" ? 1 : 0

However, as per the terraform documentation, count is not supported when used in a module for versions of terraform lower than 13. ("Module support for count was added in Terraform 0.13, and previous versions can only use it with resources.")

I am using terraform 12 and the resource is coming from a module and so I would like a different conditional statement that will only build the resource in production but not sure what else there is other than the count function

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

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

发布评论

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

评论(1

不一样的天空 2025-02-19 02:30:59

这是我评论的更多详细信息:

这是我的测试模块“策略”,代码类似于:

variable "foo" {
  type = number
}

resource "aws_iam_policy_attachment" "policy_abc" {
  count = var.foo
  ...
}

resource "aws_iam_policy_attachment" "policy_def" {
  count = var.foo
  ...
}

调用模块

module "bar" {
  source = "../modules/policies"

  foo = var.environment == "production" ? 1 : 0
  ...
}

Here is more details on my comments:

Here is my test module "policies", code is something like:

variable "foo" {
  type = number
}

resource "aws_iam_policy_attachment" "policy_abc" {
  count = var.foo
  ...
}

resource "aws_iam_policy_attachment" "policy_def" {
  count = var.foo
  ...
}

calling the module

module "bar" {
  source = "../modules/policies"

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