【问题标题】:Terraform separate input variables via IF statement according to values of another input variableTerraform 根据另一个输入变量的值通过 IF 语句分离输入变量
【发布时间】:2023-01-27 02:51:13
【问题描述】:

我有两个使用 terraform 管理的 elasticsearch 服务。但是一个版本是 6.8 而另一个是 7.10 。问题是由于我使用的实例大小,我必须描述 ebs_option 输入变量。但是,当我在描述完之后运行 terraform plan 命令时,我得到以下输出:

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # module.aws-opensearch.aws_elasticsearch_domain.elastic-domains[1] will be updated in-place
  ~ resource "aws_elasticsearch_domain" "elastic-domains" {
        id                    = "arn:aws:es:eu-central-1:xxx:domain/new-elastic"
        tags                  = {
            "Environment" = "test"
            "Name"        = "new-elastic"
            "Namespace"   = "test"
        }
        # (9 unchanged attributes hidden)

      ~ ebs_options {
          - iops        = 3000 -> null
            # (4 unchanged attributes hidden)
        }

        # (13 unchanged blocks hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.

即使我应用了它,每次运行 terraform apply 命令时我都会得到相同的输出。当我稍微研究一下时,当 elasticsearch 是 7.10 版时,它使用 gp3 存储。但在 6.8 版本中它使用 gp2。默认情况下,两者之间存在一些差异。 iops 就是其中之一。

我怎样才能克服这个问题?由于我是在单个模块下定义的,所以不能单独给出。

我在下面有地形配置:

主程序

resource "aws_elasticsearch_domain" "elastic-domains" {

  count = length(var.domain_names)

  domain_name           = var.domain_names[count.index].domain_name
  elasticsearch_version = var.domain_names[count.index].elasticsearch_version

  ...

  ebs_options {
    ebs_enabled = true
    volume_size = 50
  }
}

变量.tf

variable domain_names {
    type=list(object({
        domain_name           = string
        elasticsearch_version = number
    }))
}

terraform.tfvars

domain_names = [
    {
        domain_name           = "elastic"
        elasticsearch_version = "6.8"
    },
    {
        domain_name           = "new-elastic"
        elasticsearch_version = "7.10"
    }    
]

【问题讨论】:

    标签: amazon-web-services terraform terraform-provider-aws amazon-opensearch


    【解决方案1】:

    您有条件地将 iops 设置为 null,具体取决于版本。例如。

    ebs_options {
        ebs_enabled = true
        volume_size = 50
        iops = var.domain_names[count.index].elasticsearch_version == "7.10" ? 3000 : null
    }
    

    【讨论】: