Stop/Start EC2 same instance with terraform?

7,019 views
Skip to first unread message

manfred.s...@gmail.com

unread,
Oct 7, 2018, 5:39:06 PM10/7/18
to Terraform
Is it possible to stop and restart the same EC2 instance without destroying the machine?

It would help in reducing costs and deployment times.

It seems currently always a new instance is started.

David Adams

unread,
Oct 7, 2018, 5:59:54 PM10/7/18
to terrafo...@googlegroups.com
Not with Terraform, but it's pretty easy to stop and start an instance with the CLI tools if you have them installed:

    $ aws ec2 stop-instances --region us-east-2 --instance-ids i-0123456789abcdef

    $ aws ec2 start-instances --region us-east-2 --instance-ids i-0123456789abcdef

If you need to fetch the instance ID quickly, you can define a TF output and get at it that way:

    $ terraform output id
    i-0123456789abcdef

Or if its buried in a module somewhere you can get it from the state:

    $ terraform state show module.service.aws_instance.host |awk '$1=="id"{print $3}'
    i-0123456789abcdef

Combine things and do:

    $ aws ec2 stop-instances --region us-east-2 --instance-ids $( terraform output id )

Enjoy.
--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/2e7b3689-13ea-42d8-bfd1-f14fccc94b70%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages