Home > Back-end >  Terraform separate input variables via IF statement according to values of another input variable
Terraform separate input variables via IF statement according to values of another input variable

Time:01-27

I have two elasticsearch services managed with terraform. But one version is 6.8 while the other is 7.10 . The problem is that I had to describe the ebs_option input variable because of the instance size that I am using. However, when I run the terraform plan command after describing this, I get the following output:

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.

Even though I apply it, I get the same output every time I run the terraform apply command. When I researched this a bit, when elasticsearch is version 7.10, it uses gp3 storage. But in version 6.8 it uses gp2. There are some differences between the two that come by default. iops is one of them.

How can I overcome this problem? Since I defined it under a single module, I cannot give it separately.

I have terraform configuration below:

main.tf

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

variables.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"
    }    
]

CodePudding user response:

You conditionally set the iops to null depending on the version. E.g.

ebs_options {
    ebs_enabled = true
    volume_size = 50
    iops = startswith(var.domain_names[count.index].elasticsearch_version, "7") ? 3000 : null
}
  • Related