Skip to content Skip to footer

Terraform AWS Reboots

From time to time you need to reboot aws instances as part of provisioning etc.

The typical way is to place it in user data or in more complex setups using null resource with remote exec provisioner

For the latter, the reboot (or sudo reboot) will fail with code 1, due to terraform receiving a discconect as its running.

The work around is to just use shutdown -r as below 

 

				
					resource "null_resource" "reboot" {
  provisioner "remote-exec" {
    connection {
      type        = "ssh"
      user        = "root"
      host        = var.server_ip
      private_key = var.root_private_key
    }
    inline = [
      "shutdown -r 1",
    ]
  }
  depends_on = [null_resource.cloud-init-wait]
}
				
			

Leave a comment