Terraform资源定义内的if语句

huangapple go评论58阅读模式
英文:

Terraform if statement inside of resource definition

问题

我正在使用helm_release Terraform模块将应用程序部署到我的Kubernetes集群。我试图获取存储在AWS SSM中的单个值,并将其传递到helm chart值文件中。这个特定值仅对一个helm chart而言是必需的。我想要的是不要为所有helm chart检索或传递这个值,所以我正在尝试在helm_release资源中设置一个if语句,如果chart_name变量等于我需要设置值的chart,就执行条件检查。例如,我想要像这样做:

resource "helm_release" "example" {
  name  = var.chart_name
  repository = var.repository
  chart      = var.chart_name
  namespace = var.namespace
  create_namespace = var.create_namespace
  verify = false
  timeout = var.timeout
  repository_username = var.repo_username
  repository_password = var.repo_token
  
  # 在这里设置来自SSM的自定义值。这是我想执行条件检查的地方:
  # 例如
  # if var.chart_name = "some-chart":
  set_sensitive = [
    {
      path  = "firstEndpoints"
      value = data.aws_ssm_parameter.first_endpoints
    },
    {
      path  = "secondEndpoints"
      value = data.aws_ssm_parameter.second_endpoints
    },
  ]

  # 所有图表的动态值通过Terragrunt输入传递(这里不是特别相关)
  dynamic "set" {
    for_each = var.dynamic_values
    
    content {
      name = set.value.name
      value = set.value.value
    }
  }
}

长话短说,是否可以在Terraform资源内部定义条件,而不是在整个资源上定义条件?

英文:

I am deploying applications to my kubernetes cluster using the helm_release terraform module. I am attempting to get a single value stored in AWS SSM to be passed into the helm chart values file. This particular value is only required for one helm chart. I would like to not retrieve or pass in this value for all helm charts, so I am trying to set an if statement in the helm_release resource if the chart_name variable is equal to the chart I need to set the value for. For example, I would like to do something like this:

resource "helm_release" "example" {
  name  = var.chart_name
  repository = var.repository
  chart      = var.chart_name
  namespace = var.namespace
  create_namespace = var.create_namespace
  verify = false
  timeout = var.timeout
  repository_username = var.repo_username
  repository_password = var.repo_token
  
  # custom value from SSM would be set here. This is where I would like to perform the conditional check:
  # i.e. 
  # if var.chart_name = "some-chart":
  set_sensitive = [
    {
      path  = "firstEndpoints"
      value = data.aws_ssm_parameter.first_endpoints
    },
    {
      path  = "secondEndpoints"
      value = data.aws_ssm_parameter.second_endpoints
    },
  ]

  # dynamic values for all charts are passed in through terragrunt inputs (not super relevant here)
  dynamic "set" {
    for_each = var.dynamic_values
    
    content {
      name = set.value.name
      value = set.value.value
    }
  }
}

Long story short, is it possible to define conditionals within a terraform resource, rather than defining it on the entire resource?

答案1

得分: 1

你可以在资源参数中使用条件逻辑,使用预期的语法使用三元运算符:

set_sensitive = var.chart_name == "some-chart" ? [
  {
    path  = "firstEndpoints"
    value = data.aws_ssm_parameter.first_endpoints
  },
  {
    path  = "secondEndpoints"
    value = data.aws_ssm_parameter.second_endpoints
  },
] : null

按预期,这将为指定的图表名称分配自定义的list(object)值,否则参数将完全被省略。

英文:

You can specify conditional logic within a resource parameter using a ternary with the expected syntax:

set_sensitive = var.chart_name == "some-chart" ? [
  {
    path  = "firstEndpoints"
    value = data.aws_ssm_parameter.first_endpoints
  },
  {
    path  = "secondEndpoints"
    value = data.aws_ssm_parameter.second_endpoints
  },
] : null

As expected this will assign the custom list(object) value for the specified chart name, and the parameter will be omitted completely otherwise.

huangapple
  • 本文由 发表于 2023年6月13日 03:13:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459674.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定