You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How can I specify the state of my instances to be stopped?
Reacted by Jared Ledvina, Miguel Bernadin, Thomas Obenaus, Silvio Moioli, Ivan Skriabin, sharath-yml, Shiraaz Moollatjie, Jakub, Nils Hjelte, levequej and 74 more
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.
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.
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
Reacted by GamgeeNL, Oliver L Schoenborn, John Celoria, Nathan Schepers, Paragon1970, Scott Stensland, rovigit and Peter Reji
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.
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 👍 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.
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.
@tmccombs I agree, this might be less usable with assume_role. My guess is that we'll need to be asking for a new resource aws_instance_state instead of expanding existing aws_instance resource.
Reacted by Johann BICH, Richard Watson, João Rafael, Stanislav Baranov and Yu Kato
I got this working with assume_role by specifying the AWS_PROFILE=xxxx ... this works for me as the terraform.workspace is named the same as my aws profiles. But the provisioner trick works nicely
I would also like to see this feature implemented.
I plan to provide interactive GPU workstation and image rendering instances to users and they should be able to start and stop the instance on demand (without the instance being destroyed every time), like during the night, like in an on-premise office.
But I understand that, with Terraform saving the state in a .tfstate file or in Terraform Cloud there will be a quick mismatch between this initial "everything is running" state and the state after a week when some machines are on and some are off... Doesn't look trivial to me to implement.
As I also plan to use Ansible, maybe Ansible is a good partner tool for controlling infrastructure that is already running:
I came across this looking for a way to stop aws rds cluster instances.... but looking at this ticket and the docs for aws_instance I think this is all implemented? no?
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.
Activity
hashibot commentedon Jun 13, 2017
This comment was originally opened by @phinze as hashicorp/terraform#1579 (comment). It was migrated here as part of the provider split. The original comment is below.
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.
hashibot commentedon Jun 13, 2017
This comment was originally opened by @knuckolls as hashicorp/terraform#1579 (comment). It was migrated here as part of the provider split. The original comment is below.
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.
hashibot commentedon Jun 13, 2017
This comment was originally opened by @knuckolls as hashicorp/terraform#1579 (comment). It was migrated here as part of the provider split. The original comment is below.
Heh, I should refresh old tabs before I reply.
hashibot commentedon Jun 13, 2017
This comment was originally opened by @mitchellh as hashicorp/terraform#1579 (comment). It was migrated here as part of the provider split. The original comment is below.
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
terminatedtostopped. 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.
hashibot commentedon Jun 13, 2017
This comment was originally opened by @maartensl as hashicorp/terraform#1579 (comment). It was migrated here as part of the provider split. The original comment is below.
+1 for stop / start and other state specifications in TF
hashibot commentedon Jun 13, 2017
This comment was originally opened by @sebastiang as hashicorp/terraform#1579 (comment). It was migrated here as part of the provider split. The original comment is below.
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
hashibot commentedon Jun 13, 2017
This comment was originally opened by @mtekel as hashicorp/terraform#1579 (comment). It was migrated here as part of the provider split. The original comment is below.
Terraform should also check if the state is the same as desired. Currently when I stop VM terraform doesn't change the state.
hashibot commentedon Jun 13, 2017
This comment was originally opened by @jevonearth as hashicorp/terraform#1579 (comment). It was migrated here as part of the provider split. The original comment is below.
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.
hashibot commentedon Jun 13, 2017
This comment was originally opened by @charity as hashicorp/terraform#1579 (comment). It was migrated here as part of the provider split. The original comment is below.
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.
hashibot commentedon Jun 13, 2017
This comment was originally opened by @spkane as hashicorp/terraform#1579 (comment). It was migrated here as part of the provider split. The original comment is below.
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).
hashibot commentedon Jun 13, 2017
This comment was originally opened by @jmehnle as hashicorp/terraform#1579 (comment). It was migrated here as part of the provider split. The original comment is below.
Folks, for Cthulu's sake! Please hit the 👍 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.
hashibot commentedon Jun 13, 2017
This comment was originally opened by @stumyp as hashicorp/terraform#1579 (comment). It was migrated here as part of the provider split. The original comment is below.
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
hashibot commentedon Jun 13, 2017
This comment was originally opened by @in4mer as hashicorp/terraform#1579 (comment). It was migrated here as part of the provider split. The original comment is below.
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.
hashibot commentedon Jun 13, 2017
This comment was originally opened by @spkane as hashicorp/terraform#1579 (comment). It was migrated here as part of the provider split. The original comment is below.
I noticed that a recent commit (hashicorp/terraform#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?
30 remaining items
stumyp commentedon Oct 20, 2020
@tmccombs I agree, this might be less usable with
assume_role. My guess is that we'll need to be asking for a new resourceaws_instance_stateinstead of expanding existingaws_instanceresource.cgmckeever commentedon Dec 30, 2020
I got this working with
assume_roleby specifying theAWS_PROFILE=xxxx... this works for me as theterraform.workspaceis named the same as my aws profiles. But theprovisionertrick works nicelybackhausmartin commentedon Feb 16, 2021
I would also like to see this feature implemented.
I plan to provide interactive GPU workstation and image rendering instances to users and they should be able to start and stop the instance on demand (without the instance being destroyed every time), like during the night, like in an on-premise office.
But I understand that, with Terraform saving the state in a .tfstate file or in Terraform Cloud there will be a quick mismatch between this initial "everything is running" state and the state after a week when some machines are on and some are off... Doesn't look trivial to me to implement.
As I also plan to use Ansible, maybe Ansible is a good partner tool for controlling infrastructure that is already running:
https://docs.ansible.com/ansible/latest/collections/amazon/aws/ec2_module.html#ansible-collections-amazon-aws-ec2-module
Newer module:
https://docs.ansible.com/ansible/latest/collections/community/aws/ec2_instance_module.html
The "state" can be set to stopped.
I also found this video: https://www.hashicorp.com/resources/ansible-terraform-better-together
bulebuk commentedon Jul 8, 2021
@chrisportela That's essentially what is possible with the OpenStack provider. https://registry.terraform.io/providers/terraform-provider-openstack/openstack/latest/docs/resources/compute_instance_v2#power_state
I've used this flag handle blue/green with rollback, power up/down test environments, and to selectively disable systems in ugly enviornments.
josh-m-sharpe commentedon Oct 5, 2021
I came across this looking for a way to stop aws rds cluster instances.... but looking at this ticket and the docs for
aws_instanceI think this is all implemented? no?https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/instance#instance_state
stumyp commentedon Oct 5, 2021
This is data source, not a resource.
eathtespagheti commentedon Aug 4, 2022
Great feature, I would love to have it, unfortunately I can't use the local exec workaround since I'm working on a multi account setup
brittandeyoung commentedon Dec 30, 2022
I wonder if this would best be solved by an additional resource:
aws_instance_stateThe configuration would look like so:
This would make use of the following APIs to manage the state of the Instance:
https://docs.aws.amazon.com/sdk-for-go/api/service/ec2/#EC2.StopInstances
https://docs.aws.amazon.com/sdk-for-go/api/service/ec2/#EC2.StartInstances
Valid inputs for
statewould berunningorstopped.If this would resolve this issue I would be glad to start work on the additional resource and open up a PR.
github-actions commentedon Jan 5, 2023
This functionality has been released in v4.49.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.
For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!
github-actions commentedon Feb 5, 2023
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.