Terraforming vSphere

Applying Terraform Defintions in vSphere Environment – Part III

In the previous parts of this series, we set up Terraform and created a basic definition to create a virtual datacenter. In part two, we initialized the Terraform folder and produced a plan of the changes our definition will make. Now, it’s time to apply the change and see our new datacenter take shape in the vSphere inventory.

Applying the Definition

To apply our definition, we use the terraform apply command. This command takes the name of the plan file as an argument, which is named based on the resource being created. In our case, the plan file is named newDC.plan. Here’s the command:

terraform apply newDC.plan

When we execute this command, Terraform will process the plan and create the new datacenter in the vSphere inventory. We can see the progress of Terraform as it processes the plan, and once the command is complete, we can see that our new datacenter has been created.

Destroying the Resource

Now that we have applied our definition and created our new datacenter, we no longer need it. To clean up and avoid having an outdated state file, we will use Terraform to destroy the resource. To do this, we execute the following command:

terraform destroy -var “datacenter=our_new_datacenter”

When we run this command, Terraform will print out to the console what resources will be affected by the command. In our case, only the datacenter resource will be affected. To confirm the destruction, we type “yes” and press Enter.

Once the command is complete, we can see that the datacenter resource has been destroyed in the vSphere inventory.

Terraform State File

As mentioned earlier, Terraform cannot work without the state files. The state file allows Terraform to track the infrastructure it is affecting, recording the infrastructure components, their settings (configuration), and dependencies between each other. Without storing a state, Terraform would not know what to do or when to do it.

In this series of posts, we have been using the state file locally, but for testing and learning purposes, it is perfectly fine. In a future post, I will discuss how to properly manage your state files.

Conclusion

In this third part of our series on getting started with Terraform in a vSphere environment, we have applied our definition and created our new datacenter in the vSphere inventory. We have also destroyed the resource using Terraform, ensuring that our state file remains up-to-date.

Terraform is a powerful tool for managing your infrastructure as code, and with these posts, you should now have a good understanding of how to get started with Terraform in a vSphere environment. In the next post, we will discuss more advanced topics such as using modules and count-based resources.

Until then, happy automating!

Leave a Reply