Terraform: Stop instances

Created on 17 Apr 2015  ·  30Comments  ·  Source: hashicorp/terraform

How can I specify the state of my instances to be stopped?

enhancement provideaws

Most helpful comment

Good question - this is not something that can be done in Terraform today, but it's definitely a feature we've been talking about. Tagged.

All 30 comments

Good question - this is not something that can be done in Terraform today, but it's definitely a feature we've been talking about. Tagged.

This is an excellent question and I don't think it's addressed. I assume you mean ec2 instances. There should likely be a stop_on_destroy = true flag added to that resource. It would require additional logic in the destroy function of that resource. I didn't see anything that suggested that Terraform currently supports it, but it should.

One complexity is how the create function would work in conjunction with the statefile. Should a new instance be created? Should the stopped instance be started? How would Terraform know to terminate and re-create versus stopping and starting. Food for thought.

Heh, I should refresh old tabs before I reply.

I think this should be added. Its tricky though since Terraform now needs to know how to do a state transition from almost anything. i.e. it needs to know how to get from terminated to stopped. I'm not saying thats hard, so much as we need to build that knowledge into TF itself.

We need to do it, just explaining the situation.

+1 for stop / start and other state specifications in TF

For temporary environments like dev boxes or custom staging environments, would be lovely to just stop all the instances overnight and start them again when we get to work to save costs but keep some ephemeral state. +1

Terraform should also check if the state is the same as desired. Currently when I stop VM terraform doesn't change the state.

I'm also looking forward to this.

My situation, and current work around is;

I have a c4.2xlarge instance that I use as a operating system builder. I only need this to run occasionally. I don't like to rebuild this from scratch every time, as it takes a lot longer.

For now, I just manually shutdown the instance via the AWS web interface. This causes errors in terraform because the IP address of my machine is lost, and terraform tries to update the related route53 record with an empty string.

This is likely a corner case that will need to be addressed when you add the ability to stop instances from terraform.

1 error(s) occurred:

* aws_route53_record.packages_builder: InvalidChangeBatch: Invalid Resource Record: FATAL problem: ARRDATAIllegalIPv4Address encountered at
        status code: 400, request id: b221dee7-c4ca-11e5-9bcd-b5f8c0509fb2

I have a bunch of environments, some of which have state. as a broke ass startup i would like to be able to apply "stop" to an entire tf environment, so i don't have to recreate them every time i want to run something on the cluster.

To expand on this a bit:

It would also be nice if terraform could identify the instance state, notify you during a plan of that state (like an instance that is stopped), and then transition the state of those instances (that are not is the correct state).

Folks, for Cthulu's sake! Please hit the :+1: reaction symbol beneath @phinze's first response at the top of this issue instead of posting "+1" a gazillion times. All the people subscribed to this issue really don't need to by notified by email every time someone else agrees this is a good idea.

You can use user-data/cloud-init to make instance stop right after provision.

https://cloudinit.readthedocs.io/en/latest/topics/examples.html#reboot-poweroff-when-finished

This issue is almost two years old. Time to get some movement here; this is a fundamental limitation of state management! It's been mentioned in linked-but-closed-issues, but being able to 1) change user-data, and 2) resize instances (slightly more complicated) instead of recreating would be huge. There are so many use cases for this functionality, we need it.

I noticed that a recent commit (https://github.com/hashicorp/terraform/pull/11998) which "allows aws_instances to be resized rather than forcing a new instance" seems to have some of the seeds of code to address this (https://github.com/hashicorp/terraform/pull/11998/files).

Is there a plan to make this more generally supported?

@spkane I do believe that's the goal! :)

For @in4mer, we've been working on both those cases for the past couple weeks (in between other issues as well) and we're getting close.

+1 for this at me. Badly needed, especially for a usecase such as a C/I pipeline where I'm testing creating essentially a whole set of instances to match a production environment. if i could subsequently run the same catalog but instead pass aws_instance an argument e.g. "ensure=${var.instance_state}" where that could be running, stopped, present, absent (hat tip to Puppet) -- well, that would be awesome and a huge cost saver.

It would require some feature build-out in provisioners however, adding something akin to the ignore_changes arg in lifecycle blocks in order to ignore the offline servers.

There are other ways to do this of course but it seems to me most perfectly suited for Terraform.

So, can i stop instance via TF or should i do it manually via the AWS console (GUI)?
@phinze @mitchellh

@kollender - I don't think you can currently stop the instance with TF, so you'd need to do it through the AWS Console.

A few months back, I took a stab at implementing this feature request, but quickly came to understand what Mitchell meant when he said getting from terminated to stopped could be interesting. As I'm not a core contributor to the project, it was difficult to make any reasonable decisions about how to inject the logic into the code base.

That said, implementing the workflow from stopped => running and running => stopped was very easy. I think it required a change of a single string. I think terminated => stopped was the only tricky one and terminated => running was different because terminated was essentially a destroy, except the state stuck around. I was unsure what the proper action was there either.

Why is this closed? Until there is no resolution either reopen or explain why won't resolve.

The issue has been moved to terraform-providers/terraform-provider-aws#22 due to the upcoming changes in Terraform 0.10.x as noted by the bot's references above.

With Google Cloud and AWS you don't pay for instances that are stopped. 👍 adding a new provider option stopped.

Would be a nice feature for other providers as well.

+1 for this feature

If I dug up my old code for implementing just the stopped/running states, would that constitute a valuable PR?

+1 for this feature too

I can't believe this isn't implemented. It exists for the vagrant aws plugin.

For what it's worth, my PR is here: https://github.com/terraform-providers/terraform-provider-aws/pull/1980
Kinda just hanging out ;)

LGTM

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings