Skip to content
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

Closed
hashibot opened this issue Jun 13, 2017 · 47 comments · Fixed by #28639
Closed

Stop instances #22

hashibot opened this issue Jun 13, 2017 · 47 comments · Fixed by #28639
Assignees
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/ec2 Issues and PRs that pertain to the ec2 service.
Milestone

Comments

@hashibot
Copy link

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?

@hashibot hashibot added the enhancement Requests to existing resources that expand the functionality or scope. label Jun 13, 2017
@hashibot
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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 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.

@hashibot
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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.

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

@hashibot
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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?

@hashibot
Copy link
Author

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.

@hashibot
Copy link
Author

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.

@hashibot
Copy link
Author

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)?
@phinze @mitchellh

@jeffgage
Copy link

jeffgage commented Aug 21, 2018

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

@johnjelinek
Copy link

@jeffgage: so should this be closed or remain open until documentation is provided?

@jeffgage
Copy link

Hi @johnjelinek,
My opinion is that the issue is not resolved until that argument has documentation so that users may be aware of the solution.

@chrisportela
Copy link
Contributor

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.

@stumyp
Copy link

stumyp commented Jul 23, 2019

Local-exec seems to be the only possibilty so far, can control the state of the instance with more advanced logic than simple on/off

@tmccombs
Copy link
Contributor

tmccombs commented Nov 4, 2019

Another possible solution is to use user_data to poweroff the instance once it is set up, assuming instance_initiated_shutdown_behavior is set to "stop"

@tvkrishnaAWS
Copy link

Is it possible to stop the EC2 instance using terraform. As terraform evolving, i want to know, how they addressed it.

@kalote
Copy link

kalote commented Oct 20, 2020

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.
The thing is that this scenario will happen multiple times a year, and without this state setting on the aws_instance, i don't know how to properly manage it.

Any news about stopping / starting instances with terraform? Any known workaround? Thanks

@stumyp
Copy link

stumyp commented Oct 20, 2020

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 local-exec to stop them on provision time.

@kalote
Copy link

kalote commented Oct 20, 2020

How will you do that? @stumyp

@stumyp
Copy link

stumyp commented Oct 20, 2020

@kalote
The documentation is pretty simple for it:

https://www.terraform.io/docs/provisioners/local-exec.html

resource "aws_instance" "web" {
  # ...

  provisioner "local-exec" {
    command = "echo ${aws_instance.web.private_ip} >> private_ips.txt"
  }
}

replace command with aws ec2 stop-instances --instance-ids ${aws_instance.web.id}

@tmccombs
Copy link
Contributor

@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 assume_role option, it can be difficult to create a simple aws command with the proper credentials.

@stumyp
Copy link

stumyp commented 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 resource aws_instance_state instead of expanding existing aws_instance resource.

@cgmckeever
Copy link

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

@backhausmartin
Copy link

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
Copy link

bulebuk commented 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
Copy link

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?

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/instance#instance_state

@stumyp
Copy link

stumyp commented Oct 5, 2021

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/instance#instance_state

This is data source, not a resource.

@eathtespagheti
Copy link

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
Copy link
Collaborator

brittandeyoung commented Dec 30, 2022

I wonder if this would best be solved by an additional resource:

aws_instance_state

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
https://docs.aws.amazon.com/sdk-for-go/api/service/ec2/#EC2.StartInstances

Valid inputs for state would be running or stopped.

If this would resolve this issue I would be glad to start work on the additional resource and open up a PR.

@github-actions
Copy link

github-actions bot commented 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
Copy link

github-actions bot commented 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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/ec2 Issues and PRs that pertain to the ec2 service.
Projects
None yet
Development

Successfully merging a pull request may close this issue.