Deployment with Terraform - Infrastructure as Code

Deployment with Terraform - Infrastructure as Code

Are you tired of preparing infrastructure for your open source e-commerce software product every time from scratch? Can you navigate AWS and Azure portals blindly? It’s probably because you don’t know Terraform!

As e-commerce web developers who build web apps, cloud-based with Azure and 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 Microsoft Azure e-commerce which in some way automates the process but is hard to maintain and prepare, also writing that files have a 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 one of Microsoft's e-commerce tools that allows you to manage your cloud platform (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 at any time or, what is even better, you can reuse it in other projects. It uses its custom DSL (Domain Specific Language) called HCL (HashiCorp Terraform 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 open source e-commerce 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 a terraform application 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 creating 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 in 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 the whole infrastructure from scratch.

How do we use it?

Let’s jump to the practice part. We’re the nopCommerce solution partner iteo and we know that 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 with cloud hosting on Azure with three environments (Dev, Test and Production). We divided the whole infrastructure into three files, first with the service bus, all topics and subscriptions, the second one with all the Azure functions, and the third with an 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
  • Terraform init
  • Terraform apply

Conclusion

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 the early stages of development and we assure you that it’s worth using.

Terraform Microsoft 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. Together we can deliver everything!

Deixe seu comentário
*

Comentários

01/04/2022 08:35
Very interesting. Love to try it for our next project. It would be nice if you can also share a working example or tutorial for nop. Thanks for sharing.