One state file for incremental infrastructure provisioning

Hi All,

Am working on a scenario where I have an incremental infrastructure provisioning ( I build VM’s on Azure every week) and I want to maintain one state file for all these builds.

Am storing state file in Azure Storage account and configured backend as required. The problem am having is when I build my 1st VM a state file is created and when I try to build 2nd VM referring (backend) the same state file with some configuration changes in my file, (I know this is actual terraform behaviour ) terraform plan states old VM will be replaced.

I have tried parameters, Lifecycle blocks, create before destroy as well but no luck. I see only one option to add multiple resource blocks to my

Is there any way to achieve creating Multiple VM’s referring (Once a week) one state file having one resource block in my (Parameterized) ?

I’m not quite clear what you are trying to do?

Are you adding a new VM each week that is the same or in some what different to previous ones?

Are the previous ones still running or have they been somehow stopped/removed (via Terraform or elsewhere)?

Hi Stuart,

Yes am adding a new VM each week having almost similar configuration, but with changes in name and resource group. VM’s which are created previously are always running.

If I don’t add a new resource block (For creating VM 2 ) in my file, Ideally terraform replaces/deletes VM 1 and creates VM2. This happens as am referring same state file when VM1 was created.

I want to know whether is it possible to achieve this without altering previously created resources and using same state file and one resource block ?

Why would VM1 be being changed?

Am using the same resource block ( which I have used for creating First VM 1 and updating it to create VM2 in a different resource group and same state file. In this case terraform ideally deletes VM1 and creates VM2.

My question is how do we create VM2 without altering VM1 using the same resource block in file

So you are wanting to end up with two VMs, which are identical other than the resource group?

It sounds like your goal is to use Terraform only for the “create” step, and then for Terraform to be uninvolved in the ongoing management of the objects. That would be a nonstandard use of Terraform, but would technically work because Terraform would effectively forget all of the objects in the state, and thus never destroy them or otherwise interact with them again.

If so, that isn’t really what Terraform is intended to do, but you can get that effect by discarding the state file each time you’re satisfied that you’ll no longer need to do any Terraform actions for that particular object.

If you want Terraform to manage an object on an ongoing basis then there is no alternative to keeping it present in the configuration, because Terraform is a desired state system and the object being in the configuration is how you declare the desired state that the object should exist in the remote system.