What is Terraform?
Terraform is a tool developed by HashiCorp for provisioning cloud infrastructure in a declarative method. Terraform use the concept of Infrastructure as a Code (IaC) for provisioning. IaC diverts from the traditional cloud provisioning, in IaC we create code contains all the necessary components and integrations that need for the provisioning and we deploy that code to the cloud provider through Terraform. So Terraform will decide how to provision it to the cloud. We called it a declarative approach. Terraform has extensive support for different cloud providers.
Terraform Cloud is an application developed to help organizations DevOps teams to use Terraform together. This application has content and reliable environment fined tuned for working with Terraform. With this, it eliminates a few challenges faced when working with terraforming on the local environment. Such as storing the state file in a secure shared location, sharing secret data, access controls for approving changes.
Prerequisites for Terraform Cloud
Create an Organization
In the process sign up for the Terraform you have to create an organization to work with this. You can create multiple organizations for the need.
Next step is to create a workspace as below
Next step is to connect to Version Control System (VCS)
As I already connected to GitHub it shows all my repos. From repos we can select the repo which our terraform code is stored.
Next configure the settings for the repo you selected above following settings can configured.
- If repo contains multiple folders we can configure to work on specific path
- Notification informing, when running the execution plan terraform will use this folder location
- If multiple branches existed can give the branch name. Default to master branch
After configuring above settings we are safe to create the workspace.
Next is to define the variables required for the variable.tf file
If we use Azure as our deployment platform we need to pass following variables to the variables.tf file from variables in Terraform workspace
- Client ID
- Tenent ID
- Client Secret
- Subscription ID
After configuring the variables we are ready to go forward with the deployment. But before we move we can add Environment variables for the deployment queue as below. Here I’m adding an ENV to enable the functionality for destroying the deployment.
OK, let’s move with the deployment plan. First, we need to queue a deployment for the workspace as below.
Click Queue Plan then terraform plan will exicuted as below
When the plan is completed will ask to confirm the plan and apply. review the changes in the plan and apply it.
Add any comments and apply
This will start the deployment as below
So if I login to Azure I can see the resources are creating as below. Those resources are deploying through Terraform cloud.
Will wait until the deployment completed as below.
Now you may wonder what happens to the state file of Terraform. Good thing about Terraform cloud is state file be saved in the workspace at itself so we don’t need to do any additional configurations to set it up. State file can find on the workspace state tab as below.