In this article, we will explore what Terraform is, what its workflow looks like and how it can be applied in practice.
Terraform is …
A tool to manage your infrastructure as code, including:
- storage,
- compute instances,
- networking, etc.
The term infrastructure as code means that all the needed resources will be managed through files (i.e. code) instead of manual configurations through an interface. So, infrastructure “becomes” some code and can be versioned, which allows it to be easily re-used, changed and shared.
Specifically, Terraform will generate a plan on how to reach the desired state of the infrastructure for your project and will then execute it. If any changes take place subsequently, incremental plans will be created and applied.
Terraform is awesome also because it is platform-agnostic — which makes it easy to re-use the infrastructure once you switch platforms for any reason.
Terraform Workflow Looks Like …
- Write the infrastructure as code.
- Plan on how to the infrastructure will be applied.
- Apply the infrastructure by provisioning the needed resources.
Configuration files are created using HCL syntax.
Terraform uses a Push approach by interacting directly with the provider of resources. It “communicates” the desired infrastructure to the latter.
Example Usage with Azure …
Any configuration file in Terraform has to specify the name of the provider (Azure in our example below) and make a resource declaration, with the name of your resource group and its location:
provider "azurerm" {
version = "some_version"
}resource "azurerm_resource_group" "mygroup" {
name = "<some_name>",
location = "<some_location>"
}
Say, if you want to deploy some Microsoft resource named X, you can write something like the following:
resource "azurerm_X_account" {
solution_name = "X",
location = azurerm_resource_group.mygroup.location,
resource_group_name = azurerm_resource_group.mygroup.name,
... plan {
publisher = "Microsoft",
product = "X"
}
}
To download all components needed to apply the code, we run:
terraform init
A new .terraform
directory will appear, where all the required components will be stored.
To create an execution plan (i.e. to determine how to reach the desired state), you can run the following command:
terraform plan -out <terraform_plan>.tfplan
You can review the changes to be made with the -out
optional parameter.
Finally, to apply the reviewed configurations, we run:
terraform apply <terraform_plan>.tfplan
You can reference the following links for more information on how to get started with Terraform: