Deploying 2 high availability web applications using terraform
Overview :
Hello, in this article we are going to create step by step two virtual machines to run our api and then create a loadbalancer and link it to them .
This is the architecture that we are going to build :
First of all, let’s talk about the idea behind Infrastructure as code and Terraform.
Infrastructure as code :
Infrastructure as Code (IaC) is the managing and provisioning of infrastructure through code instead of through manual processes.
With IaC, configuration files are created that contain your infrastructure specifications, which makes it easier to edit and distribute configurations. It also ensures that you provision the same environment every time.
Terraform :
HashiCorp Terraform is an infrastructure as code tool that lets you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share.
Now, let’s explain our script main.tf which will hold the resources for our infrastructure :
We created the subnet for the virtual machines inside the virtual network, we setup the availability set, the 2 network interfaces for our machines and finally the machines which are Ubuntu Servers.
Note: We used a provisioner to inject the api inside the machines and make it run .
Then we create a public ip and assign to a loadbalancer :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#public ip for azure loadbalancer resource "azurerm_public_ip""notes-api-public-ip" { name = "my-public-ip" resource_group_name = azurerm_resource_group.notes-api-azure-resource-group.name location = azurerm_resource_group.notes-api-azure-resource-group.location allocation_method = "Static" }
After that, we add some NAT inboud rules so we could ssh to our virtual machines by passing through the loadbalancer (i’m still a newbie so i added them through the azure portal) :
Fo the last step, we setup the loadbalancig rule as below :
Let’s test the api now :
And voila everything seems working perfectly !
To conclude, we learnt in this article about Infrastructure as code, its most popular open-source tool Terraform and the way to deploy two high availability virtual machines with a public ip loadbalancer .
Was this helpful ? Confusing ? If you have any questions, feel free to comment below ! Make sure to follow on Linkedin : https://www.linkedin.com/in/malekzaag/ and github : https://github.com/Malek-Zaag if you’re interested in similar content and want to keep learning alongside me!
Title: Deploying 2 high availability web applications using terraform