Search code examples
google-cloud-platformterraform-provider-awsterraform-provider-gcp

Terraform manually change autoscaling group/instance group manually outside terraform


I have infrastructure created with Terraform. There will be a few autoscaling groups (AWS) or instance groups (GCP) depending on cloud provider. It doesn't manage instances directly.

I want write an AWS or gcloud command to reduce autoscaling to min 0, max 0, remove health checks, basically everything else tied to the instance groups. Then wait for the group to delete all instances. Then I manually change everything back to the original setting.

  • Will this corrupt terraform state?
  • After doing this, is this considered a drift?
  • Can terraform handle this or will it create a zombie resource?
  • Is this an anti-pattern?

Solution

  • You can do this indeed. However, IaC tools focus on the "desired state". By making a change to scaled in/drain resources, you can do that outside Terraform. Then, after identifying that your resources were drained, let Terraform return the infrastructure to the desired state.

    Something similar, but that still uses IaC is defining terraform local states as local variables. Then, switching back and forth the state.

    Example:

    # define the states
    locals {
       instance_state { 
          draining {
             min_instances = 0
             max_instances = 0
             health_check = false
          }
    
          black_friday {
             min_instances = 20
             max_instances = 100
             health_check = true
          }
    
          default {
             min_instances = 5
             max_instances = 20
             health_check = true
          }
       }
    }
    
    # You set the state here...
    locals {
       desired_state = local.instance_state.draining
    }
    

    Then, in your resource use the desired_state:

    # No change needed here since it points to the desired_state
    resource "type_some_resource" "resource_name" {
       min_instances = local.desired_state.min_instances
       max_instances = local.desired_state.max_instances
       health_check  = local.desired_state.health_check
    }