Is there any way to destroy specific instances

1,796 views
Skip to first unread message

Dahs81

unread,
Dec 10, 2015, 3:28:47 PM12/10/15
to Terraform
Here's my issue.  I would like to be able to destroy specific instances within my aws environment, but I haven't found a good way to do this yet.

I know that you can scale a project up/down using the count variable, like so:

module "hosts" {
  servers = 3
}

Then scale using the server variable.  For instance scale down to 2 server by setting server = 2.
The problem is that I sometimes need to remove the second server from my infrastructure, but scaling down using the server variable seems to remove the last server.

The only thing I can think to do is to have multiple instances of the same module and then remove the one that I want manually, like this:

module "host_01" {
  servers = 1
  initial = 1
}

module "host_02" {
  servers = 1
  initial = 2
}

module "host_03" {
  servers = 1
  initial =3
}

Then, if I need to remove host_02, I can do so.  This feels a little bit hacky though.

Is there any way to remove specific assets from my infrastructure that I don't know about.

What I really would like to do is something like this:

terraform destroy module.host_02.aws_instance.host
This would remove that resource from the .tfstate file and when I terraform apply, it destroys just that resource.

OR

terraform plan -remove=module.host_02.aws_instance.host
This would allow me to use the scale down option mentioned at the top, but would remove a specific aws instance instead of the last instance.

The specific use case that I have is sometimes AWS expires an instance and I need to migrate my data and remove it from my infrastructure.  If I do this through the aws console, then the .tfstate gets messed up.

Does anyone have a solution to this issue or have any best practices for this use case.

Ryan King

unread,
Dec 10, 2015, 3:34:28 PM12/10/15
to terrafo...@googlegroups.com
Why not use an auto-scaling group and set a static size?

-ryan

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/907ccce4-8a07-4e90-97e1-c5b19af657aa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dahs81

unread,
Dec 10, 2015, 3:57:27 PM12/10/15
to Terraform
I'm storing different data on each server.  I need to be able to migrate data from a specific instance and then remove that instance.  If I were running a service that would probably work, but that's not the use case I need.  I need the ability to scale up, migrate my data from an old server, then tear down that specific old server.  Is that possible with auto-scaling groups?  What I really want is to be able to do that using terraform.

Ross Duggan

unread,
Dec 12, 2015, 6:10:13 AM12/12/15
to Terraform
I think that if you're storing different data on each server, having them managed as separate top level resources makes sense, even it if appears verbose.

By the way, I think in the specific case you're talking about, stopping and starting the instance is enough to migrate it to new hardware. You can do that out of band from Terraform, and then the next `apply` will pull in any changed details (like IP addresses).

George Hartzell

unread,
Dec 12, 2015, 12:43:35 PM12/12/15
to terrafo...@googlegroups.com

I've been wondering about a similar problem case in the openstack
world.

I'm wondering what to do if I have a bunch of identical compute
instances up and running and one goes away (e.g. the machine it's
running on crashes and isn't restarted).

I'd like to remove it from Terraform's world view and have Terraform
replace it, but I'm not sure how that dance would go.

At this point this is an 'imagine the problem scenarios' issue, I
haven't hit it yet and I'm not sure how to create a test case give my
openstack access. Still, it's worrisome.

Has anyone dealt with this problem?

g.
> >>> <https://groups.google.com/d/msgid/terraform-tool/907ccce4-8a07-4e90-97e1-c5b19af657aa%40googlegroups.com?utm_medium=email&utm_source=footer>
> >>> .
> >>> For more options, visit https://groups.google.com/d/optout.
> >>>
> >>
> >>
>
> --
> This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
>
> GitHub Issues: https://github.com/hashicorp/terraform/issues
> IRC: #terraform-tool on Freenode
> ---
> You received this message because you are subscribed to the Google Groups "Terraform" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/9db7240b-4fd5-494c-99c8-02a3c2b2d77a%40googlegroups.com.

Michael Daffin

unread,
Dec 13, 2015, 4:24:38 AM12/13/15
to terrafo...@googlegroups.com

When a node is destroyed outside of Terraform, it will detect it on its next run and update its state file. This will cause Terraform to recreate the node if you run terraform apply.


George Hartzell

unread,
Dec 13, 2015, 2:09:05 PM12/13/15
to terrafo...@googlegroups.com

"... destroyed outside of Terraform", in an openstack environment,
would mean that I used the GUI or command line tool or ... to
delete/destroy the machine, correct?

At that point it would realize that it should have N of that item,
only had N-1, and create a replacement.

That sounds like a reasonable match to my scenario.

Thanks!

g.
> > https://groups.google.com/d/msgid/terraform-tool/22124.23745.324005.69224%40alacrity.local
> > .
> > For more options, visit https://groups.google.com/d/optout.
> >
>
> --
> This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
>
> GitHub Issues: https://github.com/hashicorp/terraform/issues
> IRC: #terraform-tool on Freenode
> ---
> You received this message because you are subscribed to the Google Groups "Terraform" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/CAHp%3D8aWGT%3DZMYuZrM72eVMhNv7EW3y_nszgNF65q-y96P_EtUg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages