How to Deploy an Azure App Service using Terraform

0
3666
How to Deploy an Azure App Service using Terraform

How to Deploy an Azure App Service using Terraform: We have seen the azure app service and its feature, now let’s check how to create the azure app service via Terraform, which help us to maintain the IaC.  Team or organization can focus on the application development and Azure App Service will take care of the infrastructure required, and automatically scale our apps.

Terraform allows you to define and create complete infrastructure deployments in Azure. You build Terraform templates in a human-readable format that create and configure Azure resources in a consistent, reproducible manner. This article shows you how to create a Windows app with Terraform.

Creating the Resource Group

The first step is to create a Resource Group for the App Service:

# Create a Resource Group
resource "azurerm_resource_group" "appservice-rg" {
  name = "foxutech-rg"
  location = eastus
  tags = {
    environment = Staging
  }
}

Creating an Azure App Service

An Azure App Service plan defines a set of computing resources for a web app to run. We can have a Windows (default) or Linux Service Plan.

Creating a Linux App Service

Linux Service Plan

In the following example, we will deploy a Linux Service Plan, in a Standard tier (recommended for Staging environments):

resource "azurerm_app_service_plan" "service-plan" {
  name = "foxutech-service-plan"
  location = azurerm_resource_group.appservice-rg.location
  resource_group_name = azurerm_resource_group.appservice-rg.name
  kind = "Linux"
  reserved = true  sku {
    tier = "Standard"
    size = "S1"
  }
 
tags = {
    environment = Staging
}
}

Create the App Service

In this example, we will deploy a NodeJS application inside the Linux App Service created in the previous step.

resource "azurerm_app_service" "app-service" {
  name = "foxutech-app-service"
  location = azurerm_resource_group.appservice-rg.location
  resource_group_name = azurerm_resource_group.appservice-rg.name
  app_service_plan_id = azurerm_app_service_plan.service-plan.id  site_config {
    linux_fx_version = "
NODE|16-lts"
  }
  tags = {
    environment = Staging
  }
}

Creating a Windows App Service

Windows Service Plan

In the following example, we will deploy a Windows Service Plan, in a Premium V2 tier (recommended for Production environments):

resource "azurerm_app_service_plan" "service-plan" {
  name = "Foxutech-service-plan"
  location = azurerm_resource_group.appservice-rg.location
  resource_group_name = azurerm_resource_group.appservice-rg.name
  kind = "Windows"
  reserved = false  sku {
    tier = "PremiumV2"
    size = "P1v2"
  }
  tags = {
    environment = Staging
  }
}

Create the App Service

In this example, we will deploy an application with the latest version of the .NET Framework v5.0 inside the Windows App Service created in the previous step.

resource "azurerm_app_service" "app-service" {
  name = "Foxutech-app-service"
  location = azurerm_resource_group.appservice-rg.location
  resource_group_name = azurerm_resource_group.appservice-rg.name
  app_service_plan_id = azurerm_app_service_plan.service-plan.id  site_config {
    dotnet_framework_version = "v5.0"
  }
  tags = {
    environment = Staging
  }
}

Find the pricing details on: https://azure.microsoft.com/en-in/pricing/details/app-service/windows/#pricing

Clean up:

When no longer needed, either delete the resource group or head back to your terminal/command line and execute terraform destroy to delete all resources associated with this quickstart.

Google search engine