A few days ago, I had a chance to start working with Terraform using the Azure provider. In this post, I am going to describe how it felt using a dev-ops tool as a software developer and what are the main features I found useful at the beginning, and you might do as well.
What is Terraform?
When I was asked to use Terraform to “program down” the infrastructure that supports all the applications we created, I wasn’t sure what to think about or how to imagine it.
I knew in Azure you can click a lot to mess with the configurations. I played with it for personal projects a few times, so I knew what we are talking about, but I never tried to code the creation of an infrastructure. I saw and read ARM (Azure Resource Management) files before, so I knew there is a way to express it in code, but I didn’t know what toolset this language supports.
Friends Said
Terraform came up once or twice before slightly in discussions among my colleagues and friends. At that time my first question was: “What is Terraform? What can you do with that?”. The answer I got was something like that.
“Terraform is a programming language created for dev-ops engineers. It helps to unify how you express your server infrastructure as code. There are many providers who supports the language, like Azure, Google, Amazon Web Services, and 127 more as of today. A nice touch to use the same programming language to automate the usage of all those different server farms.”
After checking the documentation, it also states that Terraform is a declarative programming language, just like XML or HTML. It means that in code, you describe the state Terraform should create for you. You don’t have to think in algorithms as you would using an imperative programming language, such as JavaScript or C#. Once you express the desired state in your code, Terraform goes ahead and creates it for you. You do not have to think about the how. You just express what you want to be created.
You can express terraform state in either JSON, or in terraform files (.tf files) developed by HashiCorp, the company behind Terraform. I chose the .tf files for my projects.
Spoiler Alert: Introduction page of Terraform. Take a look after you are done with this article. https://www.terraform.io/intro/index.html
Sounds Interesting, Where Can I Learn More?
After I got an idea what this language is about, I looked for resources where I can learn how to use it. I started watching Pluralsight courses, but I felt I miss a lot of background knowledge for these classes. Fortunately, I didn’t have to wait for long and I received an email from my project manager, that pointed to a learning material and I received a playground subscription where I can play with my learning projects. Now this was game changing.
Be Careful with The Costs of Cloud Services
Note: I must emphasize that while learning Terraform is free, creating (most) resources in data-centers costs money.
How much money? It depends on the provider and subscription you chose, but there is always a free option:
- Azure provides a free tier where you can learn, and it can also support smaller projects. You can read more on their website: https://azure.microsoft.com/en-us/free/
- AWS also has a free tier you can play in https://aws.amazon.com/free
- Google Cloud Platform also supports free tier: https://cloud.google.com/free
Probably most of the providers support a free tier to learn and play with smaller projects. You just need to choose one and be careful to only create resources that are covered in the free tier.
- List of Providers: https://www.terraform.io/docs/providers/index.html
It Is Time to Become a Master of Terraform
Bold words. To be honest I never met a Terraform Master, but I know I was able to get up with speed and create production code quite quickly. It took me about a day to get my head around the basics.
At first, I read through the link I received in the email:
https://learn.hashicorp.com/terraform?track=azure#azure
This tutorial is on the HashiCorp website. It is well organized, easy to understand and contains everything you need to get your job done.
On top of that there are advanced topics like:
- Variables
- Creating modules (for code reuse and code organization)
- Loops
- And conditionals
Yes, these are the advanced topics. If you already have a basic understanding of programming concepts, you will catch up with the advanced tools quite easily.
If you haven’t got much experience with those terms, you will still be able to learn and use them after you play with them a little bit. You can go quite far without using these tools. That is why it is so quick to start using Terraform in production. Once you tried out how you can create, destroy, and re-create resources, you can then refactor your code using the advanced toolset.
A side quest: The Azure Specific Tricks
You can use terraform in many ways.
- You can install it on your own machine.
- You can run it through a remote machine.
- Or in Azure you can run it in the Cloud Shell.
I chose the Azure Cloud Shell because:
- I didn’t have to install Terraform on my local machine.
- Authentication to Azure is done automatically since I am running code under my account where I want to create new resources. If I would use it locally or on a separate remote machine I would have to deal with keys and secrets and so on.
- Cloud Shell connects to a Cloud Storage that is cheap.
- I can reach my own project in the cloud from any machine
- Creating Resources is time-consuming. It means once you run your terraform code it can take hours before it finishes. (My record time was about 40 minutes, and it is a relatively small environment. Compared to a few of the projects that are waiting for me.) By running it in the cloud I am free to use my machine to other tasks without being afraid that the terraform code stops running by any incident, like turning off or restarting my machine.
- It supports a simplified browser version of Visual Studio Code.
You can read about how to use Cloud Shell on the Microsoft Docs:
https://docs.microsoft.com/en-us/azure/cloud-shell/quickstart-powershell
What IDE/Code Editor I use?
For editing any file for code, it is recommended to use an IDE, for making your life easier.
Since I edit .tf files on my local machine as well as on the cloud, I use two different editor, depending on what is available.
On my local machine
Here I use Visual Studio Code with an open-source third-party plugin, called vscode-terraform.
- Visual Studio Code: https://code.visualstudio.com/download
- vscode-terraform: https://github.com/mauve/vscode-terraform
As of writing, this plugin is quite handy, but you should not rely on it entirely. It knows about most of the resources you can make, but it is not 100% aware of all the available resources, nor the available arguments those accept. When it works it is great, when it is not it gives you at least a nice syntax highlighting. Since it is open-source it has the potential to improve in the future. If you want you can also create a pull request extending its capabilities.
If you found any tool that has better support for Terraform files, please let us know in the comment section.
In the Cloud Shell
The Azure Cloud Shell comes with support for (as I call it) a light version of Visual Studio Code. It is based on the Monaco editor, and it can be activated from the Cloud Shell by typing the command: code .
It also has syntax highlighting support for terraform files out of the box, and it helps uploading and downloading files from your local machine. Also if you want, you can edit the entire code in the cloud, without the need of a local installation of any developer tool.
Read more on how to use it here: https://docs.microsoft.com/en-us/azure/cloud-shell/using-cloud-shell-editor
What to read next?
This article continues in part 2 here: [Coming Shortly]