mongodb / terraform-provider-mongodbatlas

Terraform MongoDB Atlas Provider: Deploy, update, and manage MongoDB Atlas infrastructure as code through HashiCorp Terraform
https://registry.terraform.io/providers/mongodb/mongodbatlas
Mozilla Public License 2.0
242 stars 168 forks source link

Cannot ignore changes for replication_specs when autoscaling is enabled #888

Closed ilya-scale closed 1 year ago

ilya-scale commented 1 year ago

Terraform CLI and Terraform MongoDB Atlas Provider Version

Terraform v1.3.3
on linux_amd64
+ provider registry.terraform.io/mongodb/mongodbatlas v1.4.3

Terraform Configuration File

resource "mongodbatlas_advanced_cluster" "common" {
  project_id             = mongodbatlas_project.myproject.id
  name                   = "common"
  cluster_type           = "REPLICASET"
  mongo_db_major_version = "6.0"
  version_release_system = "LTS"
  backup_enabled         = var.mongodb_atlas.common_cluster.backup_enabled
  pit_enabled            = var.mongodb_atlas.common_cluster.point_in_time_backup_enabled

  replication_specs {
    region_configs {
      provider_name = "AZURE"
      region_name   = "EUROPE_NORTH"
      priority      = 7

      electable_specs {
        instance_size = var.mongodb_atlas.common_cluster.instance_size
        node_count    = 3
      }

      auto_scaling {
        disk_gb_enabled            = true
        compute_enabled            = true
        compute_scale_down_enabled = true
        compute_min_instance_size  = var.mongodb_atlas.common_cluster.instance_size
        compute_max_instance_size  = var.mongodb_atlas.common_cluster.max_instance_size
      }
    }
  }

  lifecycle {
    ignore_changes = [
      replication_specs # I would like to have a more fine grained control only for instance_size
    ]
    prevent_destroy = true
  }
}

Steps to Reproduce

  1. Create a cluster with terraform that supports autoscaling
  2. Add some load to the cluster so that it can be autoscaled
  3. Wait for the autoscaling to kick in
  4. Run terraform

Expected Behavior

I expect that there is a possibility to ignore instance_size

Actual Behavior

It is not possible to do since terraform does not support referencing elements of a set. This is what I get if I try to reference region_configs:

Block type "replication_specs" is represented by a set of objects, and set elements do not have addressable keys. To find elements matching specific criteria, use a "for" expression with an "if" clause.

Debug Output

Crash Output

Additional Context

References

An issue in terraform core that says pretty much that it is not possible to reference sets and providers should use e.g. a list: https://github.com/hashicorp/terraform/issues/26359

martinstibbe commented 1 year ago

@ilya-scale We will have to explore options to restructure the datatype to a list Internal ticket INTMDB-464

ilya-scale commented 1 year ago

Sounds great! Maybe there are better options though that will allow to use autoscaling without an ignore, i.e. not specifying the instance_size directly (at least to have it as an option), but always using the compute_min_instance_size config from autoscaling e.g. as a starting point

ivan-sukhomlyn commented 1 year ago

@martinstibbe thanks for taking it into a backlog. I'm waiting for such a possibility as well. Seems like it will require changing of attributes structure to list or map instead of a set - https://github.com/hashicorp/terraform/issues/26359#issuecomment-1064652196.

Error example,

╷
│ Error: Cannot index a set value
│ 
│   on mongodb_atlas_cluster.tf line 70, in resource "mongodbatlas_advanced_cluster" "product_api":
│   70:       replication_specs.region_configs.electable_specs.instance_size
│ 
│ Block type "replication_specs" is represented by a set of objects, and set elements do not have addressable keys. To find elements matching specific criteria, use a
│ "for" expression with an "if" clause.
leo-ferlin-sutton commented 1 year ago

Just wanted to add a +1. We are very interested in this as well.

Zuhairahmed commented 1 year ago

@ilya-scale @leo-ferlin-sutton @ivan-sukhomlyn brief update, we have allocated this fix to our next v1.8.0 release which should be out next week. will keep you posted when it has been published to the Terraform Registry.

icco commented 1 year ago

Similar issue but ours is slightly more complex in that our replication_specs and region_configs are dynamic. Just wanted to flag the use case so it gets tested with your 1.0 fix.

  replication_specs {
    dynamic "region_configs" {
      for_each = var.regions
      content {
        region_name   = replace(upper(region_configs.value["region_name"]), "-", "_")
        provider_name = "AWS"
        priority      = region_configs.value["electable_nodes"] > 0 ? region_configs.value["priority"] : 0

        electable_specs {
          node_count    = region_configs.value["electable_nodes"]
          instance_size = var.provider_default_instance_size
        }

        read_only_specs {
          node_count    = region_configs.value["read_only_nodes"]
          instance_size = var.provider_default_instance_size
        }

        auto_scaling {
          disk_gb_enabled            = true
          compute_enabled            = true
          compute_scale_down_enabled = true
          compute_min_instance_size  = var.provider_auto_scaling_compute_min_instance_size
          compute_max_instance_size  = var.provider_auto_scaling_compute_max_instance_size
        }
      }
    }
icco commented 1 year ago

Worth also noting that your docs about auto_scaling recommend a solution that does not work: https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/advanced_cluster#auto_scaling

Zuhairahmed commented 1 year ago

@ilya-scale @leo-ferlin-sutton @ivan-sukhomlyn @icco we just released v1.8.0, feel free to give it try! issue should have been resolved.

ivan-sukhomlyn commented 1 year ago

I can confirm that the issue has been fixed with the v1.8.0 version.

Config example,

  lifecycle {
    # as storage and compute autoscaling is enabled
    ignore_changes = [
      disk_size_gb,
      replication_specs[0].region_configs[0].electable_specs[0].instance_size
    ]
  }
$ terraform version
+ provider registry.terraform.io/mongodb/mongodbatlas v1.8.0

Thanks @Zuhairahmed @evertsd 👍

Zuhairahmed commented 1 year ago

Wonderful to hear! Thanks for the confirmation @ivan-sukhomlyn

ilya-scale commented 1 year ago

It seems to work fine for me as well, thanks for the fix!

leo-ferlin-sutton commented 1 year ago

I agree. Thanks a lot!