Terraform-provider-aws: Stop instances

Created on 13 Jun 2017  Â·  38Comments  Â·  Source: hashicorp/terraform-provider-aws

_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?

enhancement servicec2

Most helpful comment

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: https://github.com/artburkart/terraform-provider-aws/commit/03adc67a0c20e61619e00e3041e4e885f87b63be#diff-aa349804f7de2200d450db00820fe64fR274

All 38 comments

_This comment was originally opened by @phinze as https://github.com/hashicorp/terraform/issues/1579#issuecomment-94523309. 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 https://github.com/hashicorp/terraform/issues/1579#issuecomment-94551365. 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 https://github.com/hashicorp/terraform/issues/1579#issuecomment-94551452. 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 https://github.com/hashicorp/terraform/issues/1579#issuecomment-95117648. 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.

_This comment was originally opened by @maartensl as https://github.com/hashicorp/terraform/issues/1579#issuecomment-98125937. 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 https://github.com/hashicorp/terraform/issues/1579#issuecomment-112642406. 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 https://github.com/hashicorp/terraform/issues/1579#issuecomment-118806540. 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 https://github.com/hashicorp/terraform/issues/1579#issuecomment-175475155. 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

_This comment was originally opened by @charity as https://github.com/hashicorp/terraform/issues/1579#issuecomment-237110221. 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 https://github.com/hashicorp/terraform/issues/1579#issuecomment-240884545. 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 https://github.com/hashicorp/terraform/issues/1579#issuecomment-263413711. It was migrated here as part of the provider split. The original comment is below._


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.

_This comment was originally opened by @stumyp as https://github.com/hashicorp/terraform/issues/1579#issuecomment-280030606. 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 https://github.com/hashicorp/terraform/issues/1579#issuecomment-285588403. 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 https://github.com/hashicorp/terraform/issues/1579#issuecomment-286314870. It was migrated here as part of the provider split. The original comment is below._


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?

_This comment was originally opened by @mitchellh as https://github.com/hashicorp/terraform/issues/1579#issuecomment-286320943. 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 https://github.com/hashicorp/terraform/issues/1579#issuecomment-290841453. 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 https://github.com/hashicorp/terraform/issues/1579#issuecomment-306044714. 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

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: https://github.com/artburkart/terraform-provider-aws/commit/03adc67a0c20e61619e00e3041e4e885f87b63be#diff-aa349804f7de2200d450db00820fe64fR274

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

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.

Your link is to the aws_instance data source, but this thread seems to be more about the resource. Are you saying we can now control the state of the aws_instance resource?

@bchallenor you're right, I pasted the wrong link in my first comment. However instance_state is not documented in the resource, whose correct URL is https://www.terraform.io/docs/providers/aws/r/instance.html

The patch by @artburkart on 2017-10-19 which implemented this feature is linked above.

@jeffgage That doesn't seem to have been merged, though. The corresponding PR was closed.

@jeffgage yeah it was never merged and subsequently abandoned in the PR.

Right, 2 months was too long to sit on the PR. If I recall correctly, it already had significant merge conflicts by the time it was reviewed by anyone, and the changes that were requested were generally unrelated to the PR at hand; they were all things that could be added later and not right then and there. As such, it didn't make sense to maintain the PR, since it had so little interest from the powers that be.

My use case is to create EC2 instances in a stopped state.

Terraform v0.11.11
provider.aws: version = "~> 1.59"

Error: aws_instance.blah: "instance_state": this field cannot be set

Guys, Is there any solution to just stop the instance (no termination) just stop the instance in terraform?

The only solution I've had is a local-exec provisioner script that waits a
while and then stops the instance.

On Mon, Mar 25, 2019, 5:39 PM mssaisandeep notifications@github.com wrote:

Guys, Is there any solution to just stop the instance (no termination)
just stop the instance in terraform?

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/terraform-providers/terraform-provider-aws/issues/22#issuecomment-476429583,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AOjVUnhGp5_7P1Buax6hlJKiGg7wSQtmks5vaWw3gaJpZM4N4fBl
.

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 on/off

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"

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

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.

How will you do that? @stumyp

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

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

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

Was this page helpful?
0 / 5 - 0 ratings