What is Terraform?
Terraform is a simple and powerful open-source infrastructure management tool developed by Hashicorp which uses a simple language called HashiCorp Configuration Language (HCL). Terraform lets us build, change and organize the infrastructure efficiently. It organizes infrastructure in configuration files that describes the topology of cloud resources such as Virtual machines, storage accounts and networking interfaces.
Why use Terraform?
Terraform has several benefits. Some of them are listed below :
Syntax
Since it uses Hashicorp Configuration Language (HCL) , the syntax is way simpler to understand. HCL is designed to be human-readable as well as machine-friendly language.
Multi-provider
Terraform lets us use the same language to create deployments for Azure, MySQL, AWS or on-premises data centres.
Easy Deployment
Deploys the same template multiple times to create identical development, test, and production environments.
Dry Runs
Before creating the actual environment, Terraform allows us to view the plan in order to avoid mistakes while deploying the infrastructure.
Modules
Modules help us to re-use the code. Module is just another template that is packaged up and then can be called from the main template. The command ‘Terraform get’ fetches the ready modules from the repository.
Data Sources
Data sources are configuration objects in Terraform that allow you to collect data from outside of Terraform. There are a wide range of data source available within each provider, for example in the Azure provider we can use data sources to pull in information about existing resources such as DNS Zones, RBAC Roles, Disk Images etc.
If none of these built-in providers meets your needs there is also the external data source that allows you to call a script and read data from that, so long as it is returned as JSON.
State
Terraform template it creates a state file – “terraform.tfstate”. This state file is used to store the state of the infrastructure that it is deployed, separate to that infrastructure. C
How to Install and Configure Terraform
For installing Terraform in Azure, there are 2 quick-start options –
For an Azure user, there’s no need to install Terraform separately. Terraform is installed by default in the Azure cloud shell.
User can visit the https://shell.azure.com/ and sign-in through your account.
Cloud shell can either be run stand-alone or it can be used as an integrated command-line terminal from the Azure portal.
Another option is by installing Terraform on a supported system –
- Find appropriate distribution package from
Terraform distribution package for your system and download it. Terraform is then distributed as a single .zip file. - After downloading the package, unzip it into the desired directory.
- This step is optional but is recommended, we need to modify the path and insert the directory that contains the Terraform binary.
Verifying the Installation
After installing Terraform, open command prompt and type the command terraform. Execute this command and you will see a similar output –
C:\Windows\system32>terraform
Usage: terraform [-version] [-help] [args]
The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.
Common commands:
apply Builds or changes infrastructure
console Interactive console for Terraform interpolations
destroy Destroy Terraform-managed infrastructure
env Workspace management
fmt Rewrites config files to canonical format
get Download and install modules for the configuration
graph Create a visual graph of Terraform resources
import Import existing infrastructure into Terraform
init Initialize a Terraform working directory
output Read an output from a state file
plan Generate and show an execution plan
providers Prints a tree of the providers used in the configuration
refresh Update local state file against real resources
show Inspect Terraform state or plan
taint Manually mark a resource for recreation
untaint Manually unmark a resource as tainted
validate Validates the Terraform files
version Prints the Terraform version
workspace Workspace management
All other commands:
0.12upgrade Rewrites pre-0.12 module source code for v0.12
debug Debug output management (experimental)
force-unlock Manually unlock the terraform state
push Obsolete command for Terraform Enterprise legacy (v1)
state Advanced state management
Workflow of Terraform
The terraform workflow is as follows –
-terraform init : This command looks through all *.tf files present in the current working directory and then automatically downloads the providers that are required.
-terraform plan : After running terraform init, run terraform plan to plan the resources you wish to create and to preview the changes before applying. This step is a dry run that shows which actions will be performed. This helps to manually verify the changes before they are made.
-terraform apply : Run this command to deploy the resources that were planned during the dry run. After executing this command, you will be prompted for confirmation that you want to apply those changes.
-terraform destroy : To clean up the resources that were created in the previous step, run terraform destroy. The command lets you know what are you about to remove and will prompt for confirmation.
Microsoft offers quickstart templates for Terraform on Azure. The following link provides some examples for quickstart templates.
https://github.com/terraform-providers/terraform-provider-azurerm/tree/master/examples
“If you want to find out more about other Microsoft products, Let’s CONNECT!.”