-
Notifications
You must be signed in to change notification settings - Fork 9.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stop instances #22
Comments
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. |
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. |
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. |
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 We need to do it, just explaining the situation. |
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 |
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 |
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. |
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.
|
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. |
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). |
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. |
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 |
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. |
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? |
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. @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. |
This comment was originally opened by @reubenavery as hashicorp/terraform#1579 (comment). It was migrated here as part of the provider split. The original comment is below. +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. |
This comment was originally opened by @kollender as hashicorp/terraform#1579 (comment). It was migrated here as part of the provider split. The original comment is below. So, can i stop instance via TF or should i do it manually via the AWS console (GUI)? |
Hi, just a reminder that the 'instance_state' argument which solves this problem is still undocumented on https://www.terraform.io/docs/providers/aws/d/instance.html Users: see the example in the patch: artburkart@03adc67#diff-aa349804f7de2200d450db00820fe64fR274 |
@jeffgage: so should this be closed or remain open until documentation is provided? |
Hi @johnjelinek, |
It was mentioned that it was difficult to handle all the state transitions, but couldn't a boolean be added to make the instance "stopped", which is blocked for instance-store types, and terminate would be handled via destroy? I'm wondering why a simpler solution wasn't suggested and maybe I'm missing something. |
Local-exec seems to be the only possibilty so far, can control the state of the instance with more advanced logic than simple |
Another possible solution is to use |
Is it possible to stop the EC2 instance using terraform. As terraform evolving, i want to know, how they addressed it. |
I am in a use-case where i need to create 10-15 EC2 instances for an event. After being configured / setup, those instances needs to be stopped until the event start. All the instances needs to be running for the event, and after that, they can be destroyed. Any news about stopping / starting instances with terraform? Any known workaround? Thanks |
I do not think this is possible at this moment. AWS terraform provder only can do what AWS API can. and RunInstances API call does not have this setting. Just use |
How will you do that? @stumyp |
@kalote https://www.terraform.io/docs/provisioners/local-exec.html
replace |
@stumyp that can actually be pretty difficult to do in practice if you have a non-trivial setup for your aws provider, because the command doesn't have access to the aws credentials from the provider. In particular, if you use the |
@tmccombs I agree, this might be less usable with |
I got this working with |
I would also like to see this feature implemented. 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: Newer module: The "state" can be set to stopped. I also found this video: https://www.hashicorp.com/resources/ansible-terraform-better-together |
@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. |
I came across this looking for a way to stop aws rds cluster instances.... but looking at this ticket and the docs for |
This is data source, not a resource. |
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 |
I wonder if this would best be solved by an additional resource:
The configuration would look like so: data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_instance" "test" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
tags = {
Name = "HelloWorld"
}
}
resource "aws_instance_state" "test" {
instance_id = aws_instance.test.id
state = "stopped"
Force = true # if forcing the stop is required. Default is false
} 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 Valid inputs for If this would resolve this issue I would be glad to start work on the additional resource and open up a PR. |
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! |
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. |
This issue was originally opened by @johnjelinek as hashicorp/terraform#1579. It was migrated here as part of the provider split. The original body of the issue is below.
How can I specify the state of my instances to be
stopped
?The text was updated successfully, but these errors were encountered: