In this post, we will utilize terraform to create an architecture that can be used to deploy a front and backend web application. N-tier architectures are split into multiple tiers and distributed. A common N-tier architecture is the 3-tier which is made up of a presentation, application, and data tier. But this code can be scaled very easily to add more tiers if needed.
We will deploy network infrastructure, which is called a virtual network in Azure. Within that virtual network we will deploy two small subnets. To run the web application, we will use Azure App Service which is a managed PaaS service that allows you easily scale out your application by adding new instances of web apps. Azure App Service can also be used to store mobile backends and REST APIs.
Requirements:
Terraform
Azure Account
Environment Setup:
Before you start, confirm you have a valid Azure Cloud account. Also, ensure you have Terraform installed on your local machine. Terraform provides official documentation on how to do this.
Start by creating a new directory in the desired location and navigate to it. Then paste the following code to create the resource group, two private subnets, service plan and app service:
# Azure provider source and version being used
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.75.0"
}
}
}
provider "azurerm" {
features {
}
}
# create a resource group
resource "azurerm_resource_group" "TerraformCreate" {
name = "TerraformCreate"
location = "eastus"
}
# Create a vnet with two private subnets
resource "azurerm_virtual_network" "TerraformCreateVM" {
name = "TerraformCreateVM"
location = azurerm_resource_group.TerraformCreate.location
resource_group_name = azurerm_resource_group.TerraformCreate.name
address_space = ["10.0.0.0/16"]
subnet {
name = "private-subnet-1"
address_prefix = "10.0.1.0/24"
}
subnet {
name = "private-subnet-2"
address_prefix = "10.0.2.0/24"
}
}
# Create a app service plan and web app
resource "azurerm_service_plan" "TerraformCreateASP" {
name = "TerraformCreateASP"
location = azurerm_resource_group.TerraformCreate.location
resource_group_name = azurerm_resource_group.TerraformCreate.name
os_type = "Linux"
sku_name = "P1v2"
}
resource "azurerm_linux_web_app" "TerraformCreateWA" {
name = "TerraformCreateWA"
location = azurerm_resource_group.TerraformCreate.location
resource_group_name = azurerm_resource_group.TerraformCreate.name
service_plan_id = azurerm_service_plan.TerraformCreateASP.id
site_config {
application_stack {
node_version = "18-lts"
}
}
}
Now let’s break down the above code:
We have a required terraform block that specifies the Azure provider and the version which is the latest currently. It always good to check the latest version of the Azure provider and update your code.
Next, we create a resource group, a virtual network and two private subnets. A resource group is collection of like resources to make monitoring, provisioning, access control and de-provisioning convenient and effective. A virtual network is used to house our network resources like our subnets.
From there we create a service plan and web app. Our web apps are just instances of our application that are connected to our service plan and define those resources for the application to run. In this example, we are using NodeJS on Linux instances. We are also placing all resources in the resource group. Now you might be asking why use App Service over a virtual machine. As mentioned above App Service is a managed service. That means Azure takes more off the responsibility off the hands of the customer so you can easily and quickly deploy your application. You specify your runtime, manage your data, your application and Microsoft Azure will take care of the rest. Whereas when you deploy on a virtual machine, you have more to manage like your runtime and your OS.
Creating our application stack:
Then we will run the following commands to create the above resources:
Terraform init
Terraform plan
Terraform apply
After running apply, you should see a successful apply with four resources created. Be sure to destroy any unused resources.
Melveta Atkinson, DevOps Engineer Melveta is an experienced, self driven devops engineer with a history in supporting infrastructure based on AWS services. Skilled in Terraform, Linux/Windows based servers, Databases and Python. She has a passion for learning Devops related technologies and concepts such as Docker, Kubernetes, Jenkins and CI/CD. |