title: How to do a graceful shutdown of a AWS ASG with terraform?
body: <p><strong>Problem</strong></p>
  
  <p>On a <code>terraform destroy</code>, all ASG resources are terminated and some services (<code>spark streaming</code> in my case) may still have data to process. </p>
  
  <p>To be sure my app shuts down gracefully, I connect to each instances of my ASG to perform a <code>systemctl stop service</code> and I would like to automate this process with Terraform</p>
  
  <p><strong>Leads</strong></p>
  
  <p>I know <code>when="destroy</code> keyword and <code>remote-exec</code> provisioner, but I'm not sure what the recommended way to gracefully shutdown instances in an ASG.</p>
  
  <pre><code>resource "aws_instance" "app" {
    # ...
  
    provisioner "remote-exec" {
      when    = "destroy"
      inline = [ "systemctl stop service" ]
    }
  }
  </code></pre>
  
  <p>source: </p>
  
  <ul>
  <li><a href="https://www.terraform.io/docs/provisioners/index.html#destroy-time-provisioners" rel="nofollow noreferrer">https://www.terraform.io/docs/provisioners/index.html#destroy-time-provisioners</a></li>
  <li><a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/terminating-instances.html#what-happens-terminate" rel="nofollow noreferrer">https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/terminating-instances.html#what-happens-terminate</a></li>
  </ul>