Use Existing Terraform workspace functionality

Community Alums
Not applicable

Hello Experts,

I have observed below while doing POC on CPG using terraform connector:

Expectations of the “use existing workspace”

  1. Ability to deploy multiple stacks from the same catalog item into a single TFE workspace.

 

Challenge

  1. In testing we have found that the second catalog item to deploy into an existing workspace, terminates and destroys the first catalog item's provisioned resource. The third, destroys the second, the end result only one stack can be deployed to a workspace at any one point in time. Thus a 1-1 relationship between stack and workspace.

As we know Terraform Enterprise is licensed per workspace so the incentive / requirement is to reduce the number of workspaces we require. But above scenario doesn't help . Is this really how the ServiceNow  design is or i am missing something? 

Appreciate any help around this.

Best Regards,

Vishwajeet Anand

 

1 ACCEPTED SOLUTION

Ram Devanathan1
ServiceNow Employee
ServiceNow Employee

Deletion of resources/over-write is controlled by Terraform. if the state file is retained and you are applying the new configuration on top (while also having existing config) then the old resources would not go.

 

if you are using entirely new config and applying it, Terraform being declarative will keep only the resources defined in current template.

 

e.g. i have a terraform config to deploy VM with 1 disk, network card, and i run this through CPG catalog request - this creates the resources and saves the state file with the resource (VM, 1 disk, network card). now the devops team modifies the terraform config and *adds* 1 more disk to the config. then you can update the catalog item via IaC discovery and change task. with new catalog item, you can choose existing workspace and now it will deploy the config, but it will not remove existing resources as these are already in state file.

 

btw - you can try the same steps outside of cpg workflow - you will get same result.

 

Ram

View solution in original post

6 REPLIES 6

Ram Devanathan1
ServiceNow Employee
ServiceNow Employee

Deletion of resources/over-write is controlled by Terraform. if the state file is retained and you are applying the new configuration on top (while also having existing config) then the old resources would not go.

 

if you are using entirely new config and applying it, Terraform being declarative will keep only the resources defined in current template.

 

e.g. i have a terraform config to deploy VM with 1 disk, network card, and i run this through CPG catalog request - this creates the resources and saves the state file with the resource (VM, 1 disk, network card). now the devops team modifies the terraform config and *adds* 1 more disk to the config. then you can update the catalog item via IaC discovery and change task. with new catalog item, you can choose existing workspace and now it will deploy the config, but it will not remove existing resources as these are already in state file.

 

btw - you can try the same steps outside of cpg workflow - you will get same result.

 

Ram

Community Alums
Not applicable

Thankyou for the response Ram. I will test out the scenario outside CPG workflow. But If there is no config change for the particular catalog item/template and and the user selects the existing workspace, it is going to delete the previous resource? What's the recommended best practice to allow the user to use existing workspace and not loose previous resource? Is there a way to where tf file can be retained for each deployment?

Terraform is declarative - you want to set the state of where you want to be. so your code/config should specify the desired state and running apply with bring the resources to that level. 

 

so maintain existing resources in code while applying that way they don't get lost.

 

some interesting reading here - https://qomarullah.medium.com/terraform-declarative-infrastructure-provisioning-636755aa424e

Community Alums
Not applicable

Thankyou Ram for the explanation . Appreciate your response.