0

Terraform v0.11.11
provider.azurerm v1.21.0

I am done with converting an ARM template into Terraform and started adding new things to TF instead of the ARM. Is it safe to apply the terraform on the existing deployment, since this is all I have got now?

And another question. Since I did not have any persisted state when using ARM templates, do I have to use the state or will Terraform diff the existing resources against the tf templates and perform a "merge"/"update"?

Do I have to import every resource separately into the state file/blob, something like this Error Importing existing resources into Terraform State File ?

Thanks

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
tridy
  • 1,166
  • 1
  • 12
  • 21
  • 1
    Please show the codes what you have done and clearly ask what you need to improve. – BMW Jan 25 '19 at 01:05

1 Answers1

1

Yes, you need to "save\gather" state before applying terraform to existing resources created outside of terraform. About it being safe - after you've added state for those to TF it will be able to tell if its going to make any changes to them.

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • Thanks! One small question though. What are the benefits of keeping the state in the file/blob compared to gathering the state before every TF run? – tridy Jan 25 '19 at 07:23
  • well, i'm not the biggest TF expert out there, but I'd say main value proposition of storing state is locking. so when you start `tf apply` it locks the state. so it somebody else using tf attempts to apply changes to the same resources it wont work because you are already doing that – 4c74356b41 Jan 25 '19 at 07:29
  • What do you mean by gathering state here? If you mean the refresh then that's just detecting drift of known resources, not finding resources not in the state file. – ydaetskcoR Jan 25 '19 at 08:46
  • not sure I understand the question, sorry. I never said it will refresh state\detect state for resources not in the state file – 4c74356b41 Jan 25 '19 at 08:52
  • From what I understand, ARM deployment compares the template with the existing resources and applies the changes without dealing with the state. From what I understand so far, if I want to apply TF to an existing deployment (and I do NOT have a state saved anywhere), I will need to import/gather the state into a file/blob first, and only then apply TF. Do I understand correct? – tridy Jan 25 '19 at 09:16
  • You will need to import any existing resources or Terraform will attempt to create them (you can see this with `terraform plan`). If that would cause the creation to fail (if the resource is uniquely keyed by something configurable such as a name) then Terraform will fail. If it's not keyed by something configurable (such as some random ID) then you will end up with duplicate resources. You should read up on other questions here and the Terraform docs about state before going too much further. – ydaetskcoR Jan 25 '19 at 09:18
  • arm template doesnt really compare the state, it just applies state. for TF you would need to import state for it to show you the diff. I'm not sure, you might be able to force apply it without gathering state, but that leaves you better of with arm templates, tbh – 4c74356b41 Jan 25 '19 at 09:19
  • if arm template does not compare state, how does it know if a resource should be created or edited? It would override/reset all the existing resources otherwise, wouldn't it? For example if I add a policy to a keyvaul, it will not create a new keyvault with this policy but add the policy to the exisitng keyvault. Or am I totally lost? – tridy Jan 25 '19 at 11:06
  • it just applies state in the template and resource provider will apply that config. it will decide what to do on its own. arm templates dont really do anything except talking to resource provider – 4c74356b41 Jan 25 '19 at 11:11
  • alright. here is the situation. 1) I have arm template and deployed resources 2) I have converted arm template to TF. 3) I added some changes to TF. 4) I want to run TF with changes against deployed resources. How do I do that? – tridy Jan 25 '19 at 11:28
  • 1
    import state (existing resources) to tf and run tf plan\apply – 4c74356b41 Jan 25 '19 at 11:33