Managing Infrastructure With Terraform
Terraform is another amazing tool from the great people over at HashiCorp (The people who brought us Vagrant). It allows you to manage your infrastructure as code on multiple providers.
The Terraform website describes it best:
Terraform enables you to safely and predictably create, change, and improve infrastructure. It is an open source tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
Terraform has the concept of providers. A provider can be seen as a module in Terraform that is responsible for understanding the API and provide resources of a target platform.
The documentation contains a full list of providers.
To understand how you use a provider it will be easiest to refer to an example:
The above configuration tells Terraform to use the http provider, loaded as a data source and tagged in the component web.
Terraform uses the HashiCorp Configuration Lanuage. For more information about the syntax please visit the repo on GitHub.
Your First Server as Code
I will be using Microsoft Azure for the following examples. To authenticate Terraform on Azure there are 2 options available. They are:
I recommend using the CLI option if you are following along with this article on your localhost.
The documentation for the Azure Resource Manager provider can be found at: https://www.terraform.io/docs/providers/azurerm/authenticating_via_azure_cli.html
The following configuration can all be appended to a single .tf file.
The Resource Group
The first step is to define the resource group:
One of the required parameters when creating a VM with the ARM provider is the network interface IDs. The networking interface in turn requires a subnet configuration and virtual network. The following creates all 3:
The above will create a virtual network named iacvnet with an address
10.0.0.0/16, a subnet configuration with an address prefix of
10.0.2.0/24, and finally a virtual network interface using the subnet
configuration. The vNIC also sets the IP address allocation to dynamic
which means our VM will receive a random available IP every time it boots up.
Now that all the required resources have been creates we can create the VM:
Running The Script
Before you can apply the above configuration you first need to initialise the Terraform project. To do this simply run the following command:
This create a .terraform directory which will hold all the required plugins and state for this project. It will also install the required plugins.
To apply the configuration (create the VM) run the following command:
Once Terraform completes the operation you should see something like this in the Azure portal:
To remove the resources it is as easy as deleting the content in the .tf
file and running
terraform apply again.
Azure has a tendency to grow quite quick. So quick that some 3rd party tools such as Terraform struggle to keep up. This means that Terraform might not provide nifty configuration options for all the available Azure resources.
Luckily Terraform provides the ability to run an ARM template. However, you lose the ability to manage the resources created via the ARM template as Terraform will not be able to link it to local state. For more information please see the documentation for azure_template_deployment.
Although this post only scratches the surface of what Terraform can do I hope you can see how easy it is to manage your infrastructure using this tool.
The only problem I have encountered thus far is mapping existing resources. It is definitely easier starting from scratch than trying to create the config files for all the resources we already have set up in Azure.
Terraform does provide an import command, but this requires very specific parameters to successfully map a complex existing infrastructure.