Terraform:How to create new instance without destroying it.

1,011 views
Skip to first unread message

Vivek Deo

unread,
May 13, 2016, 3:34:09 PM5/13/16
to Terraform
As of now I have created an instance using example.tf. If I make some changes in example.tf  and try to bring new instance, it first destroys it. Work  around I used is to run the same file form another directory and it does not destroy it. But I am sure there must be a an elegant way to do this. 

Thanks,
Vivek

Martin Atkins

unread,
May 15, 2016, 4:57:29 PM5/15/16
to Terraform
Hi Vivek,

Terraform's model is that each "resource" block causes one (or more, if "count" is specified) resource to exist in the target system, and so if you make a change that requires a new object to be created it will destroy the old one as you saw, to preserve the idea that there is only one.

If you want your old instance and your new instance to exist together for the long term, the appropriate way to express that is to create a separate "resource" block representing your second instance and leave the original configuration in place, thus telling Terraform that you want both instances to exist. You can then remove either instance from the configuration at a later date when you're ready for it to be destroyed.

If instead you just want both instances to exist briefly while switching from one to the other, e.g. in order to prevent downtime, the create_before_destroy lifecycle flag can achieve this. When this flag is set, Terraform will still try to ensure that only one instance exists when it completes, but it will retain the old instance until the new instance is created, and only then destroy the old instance.

Andrew Langhorn

unread,
May 15, 2016, 5:21:16 PM5/15/16
to terrafo...@googlegroups.com
Another alternative is to use a blue-green approach, and create all of your instances inside autoscaling groups. Each time you want to update things, you bake a new AMI using a tool such as Packer, and update the value in the ASG configuration in Terraform, up the number of instances in your ASG, then deploy that, and then roll it back down again. That will do you zero-downtime deployments, since the oldest instance will be terminated first by the ASG.

Of course, it's more complex than the ideas offered above, but in larger infrastructure, I'm finding it works well.
Reply all
Reply to author
Forward
0 new messages