Terraform — Infrastructure as Code

Are you tired of preparing infrastructure for your application every time from scratch? Can you navigate AWS and Azure portals blindly? It’s probably because you don’t know Terraform!

As a company that specializes in deploying web applications in cloud (Azure, AWS), we have noticed that a lot of time is spent on preparing infrastructure by doing it manually or using ARM template files in case of Azure which in some way automates the process but is hard to maintain and prepare, also writing that files have high learning curve. And that’s when Terraform shows up.

What is Terraform?

Let’s roll from the start. What even is Terraform? It’s a tool that allows you to manage your cloud infrastructure (and not only that!) in the form of a code. So, it means you can keep the code on a version control system like Git which means you can go back to previous versions in any time or, what is even better, you can reuse it in other projects. Terraform uses its custom DSL (Domain Specific Language) called HCL (HashiCorp Configuration Language) and it takes care of provisioning and updating your infrastructure. Ok, but now you probably want to ask “Where can I use it?”.

Where can I use Terraform?

Terraform is based on providers — at the moment of writing this article it has 142 official and verified ones like Azure, AWS, Google Cloud, Kubernetes, and over 600 custom community-created providers. So basically there is a high chance that whatever platform you think of will have a Terraform provider and you will be able to prepare your infrastructure in it.

How to use Terraform?

Ok, so we have chosen a provider, prepared our infrastructure in code, and what next? Simple as that, you need to only use two commands to get from code to running and working cloud components. The first command is terraform init that just initializes Terraform and providers used in your code. The second one is terraform apply that creates infrastructure on your chosen cloud provider. There are of course more commands that you can use like validate which checks if your code is in the proper manner, as well as plan which checks the current state, compares it with code and lists all changes.

Terraform state

The state we mentioned earlier is nothing more than a file that is generated after running command terraform apply. This file allows Terraform to see what has changed between running apply commands and create new, update changed, and delete removed components. You can store this file locally or in the cloud. Without it Terraform will try to create a new component even if it already exists. So, just don’t lose it :).

Terraform — Pros and Cons

Now, that we know what Terraform is and how to apply it, let’s talk about the pros of using it. What was mentioned in the beginning, having your infrastructure as a code allows you to version it and go back to previous versions in no time. The second thing is the possibility of fast reconstruction or duplication of your environment on another server/subscription. Thirdly, very rich documentation for itself and each official provider allows you to learn how to use it in no time.

Of course, Terraform has some drawbacks. First of all, you have to learn a new language even if the documentation is well written. This is a time overhead that cannot be circumvented. Secondly when you go with Terraform you need to stay with it to the end. You can of course add new resources manually but you have to keep in mind that you have to do it manually on other environments or when you are reconstructing or duplicating infrastructure. And thirdly, sadly for now you can’t prepare generic infrastructure that will translate to any cloud provider so when you decide to move from AWS to Azure you have to create whole infrastructure from scratch.

How we use it?

Let’s jump to the practice part. Of course, there are a lot of ways of using Terraform but we have to keep it short. For example, we have an application based on microservices with Azure Service Bus, Azure Functions and REST Api hosted on Azure with three environments (Dev, Test and Production). We divided the whole infrastructure into three files, first with service bus, all topics and subscriptions, second one with all the Azure functions, and the third with app service plan, app service and application insights. For every file we have additional files with parameters like components name, tiers or in case of the app service information about creating a slot (we only need staging slot on prod environment) for every environment. This way, we avoid creating massive files and we can quickly navigate through a project and add new components. And to deploy it to Azure we have prepared very simple Azure DevOps pipelines consisting of three steps:

  • Install Terraform

Conclusion

Thanks for staying with us. If you are in need of a tool to maintain your infrastructure in the form of code search no further! Of course, it has its drawbacks but it’s still in early stages of development and we assure you that it’s worth using.

Terraform is a simple, yet very powerful tool that allows you to version, maintain and update your components. It’s easy to learn yet hard to master.

Do you want to implement this amazing service in your project? Feel free to contact us. It will be our pleasure to cooperate with you. Together we can deliver everything!

human-centric software design & development. check out our website: www.iteo.com