Skip to content

Stop instances #22

Closed
Closed

Description

@hashibot

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?

Activity

  1. added
    enhancementRequests to existing resources that expand the functionality or scope.
    on Jun 13, 2017
  2. hashibot commented on Jun 13, 2017

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

  3. hashibot commented on Jun 13, 2017

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

  4. hashibot commented on Jun 13, 2017

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

  5. hashibot commented on Jun 13, 2017

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

  6. hashibot commented on Jun 13, 2017

    @hashibot
    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

  7. hashibot commented on Jun 13, 2017

    @hashibot
    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

  8. hashibot commented on Jun 13, 2017

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

  9. hashibot commented on Jun 13, 2017

    @hashibot
    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
    
  10. hashibot commented on Jun 13, 2017

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

  11. hashibot commented on Jun 13, 2017

    @hashibot
    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).

  12. hashibot commented on Jun 13, 2017

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

  13. hashibot commented on Jun 13, 2017

    @hashibot
    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

  14. hashibot commented on Jun 13, 2017

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

  15. hashibot commented on Jun 13, 2017

    @hashibot
    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?

  16. 30 remaining items

  17. stumyp commented on Oct 20, 2020

    @stumyp
    Collaborator

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

  18. cgmckeever commented on Dec 30, 2020

    @cgmckeever

    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

  19. backhausmartin commented on Feb 16, 2021

    @backhausmartin

    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

  20. bulebuk commented on Jul 8, 2021

    @bulebuk

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

  21. josh-m-sharpe commented on Oct 5, 2021

    @josh-m-sharpe

    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

  22. stumyp commented on Oct 5, 2021

    @stumyp
    Collaborator
  23. eathtespagheti commented on Aug 4, 2022

    @eathtespagheti

    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

  24. self-assigned this
    on Dec 6, 2022
  25. brittandeyoung commented on Dec 30, 2022

    @brittandeyoung
    Contributor

    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.

  26. added this to the v4.49.0 milestone on Jan 4, 2023
  27. github-actions commented on Jan 5, 2023

    @github-actions
    Contributor

    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!

  28. github-actions commented on Feb 5, 2023

    @github-actions
    Contributor

    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.

  29. locked as resolved and limited conversation to collaborators on Feb 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementRequests to existing resources that expand the functionality or scope.service/ec2Issues and PRs that pertain to the ec2 service.

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions