Infrastructure as code is very popular for some time already. When talking about deploying infrastructure or application components to Microsoft Azure as code, then the Azure Resource Manager (ARM) will come to your attention. ARM manages resources that live in Azure and is used to deploy and update them as needed. When using the Azure portal, ARM is used under the covers to manage the resources. But it also offers APIs to communicate with it from the outside world. For instance you can pass ARM templates that contain a descriptive configuration of your application and ARM will take care of provisioning it. Nothing new so far, but in today’s world, the perfect world where only Azure is used to host applications and services is not the reality what we see when talking to customers. Hybrid clouds or multi cloud environments are the reality. ARM can only be used for Azure and Azure Stack, but when you need to cover more platforms, Terraform might be a good option for you. In this blog post I will give a quick overview what Terraform is and how it works.
Terraform Series
1. A first introduction (this post)
2. Introducing input variables
3. Using multiple files for configurations and variables
What is Terraform?
Terraform by HashiCorp is an open sourced tool to manage infrastructure as code. It supports multiple providers that allows it to communication with different platforms such as Azure, AWS, Docker, Kubernetes and many more. It also allows you to develop your very own provider for your custom platform or application. Supporting multiple providers actually makes Terraform very powerful, because you can deploy application components to a variety of platforms with a single descriptive file set.
Quick: How Terraform works
In it’s simplest form, Terraform uses a single configuration file (.tf) that describes the infrastructure you want to deploy. The files is based on the HashiCorp’s Configuration Language (HCL). It can be used to plan (dry-run), apply (deploy) or destroy (delete) your described application. The file is comparable to an Azure ARM JSON deployment file.
Terraform CLI
Before you can use Terraform, you need to get access to the CLI. You can download the CLI for the different platforms here. It’s a single executable that you can run anywhere you need it.
The CLI is also available in the Azure cloud shell so that you can immediately start using it to deploy infrastructure components without installing anything.
Editor: Visual Studio Code
You can use any editor to create Terraform files. Visual Studio Code with the Terraform extension (there are multiple of them) is my preference.
First Hands-on: Azure Cloud Shell
To close this blog post, I’d like to quickly demonstrate how Terraform can be used with a simple-to-follow example. To keep things simple, I will use the Azure Cloud Shell. Why? Because when using the Azure Cloud Shell, I am already authenticated against Azure, so I don’t need to provide any authentication details during deployment. After connecting to shell.azure.com I create a new directory and create a new terraform file using the vi editor.
Let’s add some code. First I need to instruct Terraform what provider to use. In this case, I will deploy something to my Azure subscription, so I select “azurerm” (Azure Resource Manager) as the provider I want to use. Remember, I can use multiple providers to deploy components to Azure, AWS and other platforms, but for this example I will only use Azure. Then I add the subscription ID of my subscription I want to deploy to. Next I deploy 2 resources. A resource group that will hold a storage account. The declaration is pretty much self explaining.
You can get the demo files from this GitHub repo > https://github.com/MarcelZehner/blogcodeexamples
After saving the file, I first need to download the used provider to the box (in this case the Azure Cloud Shell). Important to know: Terraform will look for any .tf file in the directory and merge the content together before anything gets executed.
Terraform init
Once done, the deployment can be planned – this is a dry-run of the deployment that gives you some insights before you actually deploy.
Terraform plan
If the results look good, the deployment can be started.
Terraform apply
When checking my Azure subscription, I can now see the created resource group with the storage account. Cool!
To finish the demo (and save some money), I will destroy (delete) the infrastructure again.
Terraform destroy
Gone and done! That’s it for now. A quick overview and example how Terraform works. You can expect more to come on this topic very soon on my blog – with a bit more deep dive and behind the scenes coverage. So stay tuned!
Cheers
Marcel
Pingback: Terraform – Introducing Input Variables | marcelzehner.ch
Pingback: Terraform – using multiple files for configurations and variables | marcelzehner.ch