Azure  

Creating an Azure Web App instance with Terraform and deploying ASP.NET Core Web API

Introduction

When on-premises web applications are migrated to cloud, the web solutions hosted in IIS are needed to be deployed to Azure App Service instance. There are multiple ways to manage resources in Azure like using Azure portal i.e., GUI, Azure ARM, Azure CLI, Terraform. In this article I will explain how to create & manage with Terraform code, then I will use the basic .NET Core Web API project template & deploy API solution to Web App instance.

part1_article

Create App Service instance with Terraform :

Terraform is a Infrastructure as Code (IaC) tool that helps to build, change & manage infrastructure. It provides multi cloud support, reusable & easy to use. Infrastructure can be planned & created with running terraform scripts.

When creating a Web App instance, App Service Plan is needed first. It is the computing service container for App Service. It represents the physical sources to host apps, It controls the

  • Pricing tier

  • CPU power

  • Operating System

  • Size, Scaling

Below TF script automates the App Service Plan creation.

provider "azurerm" {
  features {}
}

resource "azurerm_service_plan" "asp" {
  name                = "${var.app_name}-asp"
  location            = var.location
  resource_group_name = var.resource_group_name
  os_type             = "Linux"
   sku_name           = "B1"
}

resource "azurerm_linux_web_app" "webapp" {
  name                = var.app_name
  location            = var.location
  resource_group_name = var.resource_group_name
  service_plan_id     = azurerm_service_plan.asp.id

  site_config {
    application_stack {
      dotnet_version = "9.0"
    }
  }

  app_settings = {
    "WEBSITES_PORT" = "5000"
  }
}

For this demo I have chosen Linux OS, Basic SKU, As I am going to deploy .NET 9.0 solution so application stack is dotnet_version 9.0 & app communication is enabled on port 5000. Now next step is to execute this script.

  1. terraform init : Initialize terraform instance in working directory.

terraform_init
  1. terraform validate : Syntax check & validation

terraform_validate
  1. terraform plan : Provides a infrastructure preview.

terraform_plan
  1. terraform apply : Execute the plan & commissions the infrastructure

terraform_apply
  1. terraform destroy : Destroys the resources. Since Azure follows pay as you go policy, means resources are billed as per consumption & in some cases charges are calculate per second. So its always a better strategy to get rid of unwanted resources.

terraform_destroy

App Service Plan & App Service is created in Azure.

Azure_ASP_WebAppAzure_WebApp_Running

Create ASP.NET Core Web API

Now set up a new ASP.NET Core Web API project with VS.

API_2

There are multiple ways to deploy but in this article, we will use Visual Studio -> Publish feature. Azure login in Visual Studio must match the subscription used in Terraform.

API_Publish_1

We have created App Service Plan with Linux OS.

API_Publish_2

Publish wizard shows the app services associated with the Visual Studio login. There can be multiple subscriptions & each subscription can have multiple app services. In my case I have only one subscription & single app service.

API_Publish_3

Visual Studio creates publish profile based on the selections performed. Now last step is to Publish API.

API_Publish_5

It takes few seconds to refresh published content. The final output can be verified by hitting app service URL.

Azure_WebApp_Running_1

As app service is created & API is published . Now app service resource should be destroyed. Azure has pay as you go & resources are charge based on consumption.

Learnings

  1. Creating App Service & App Service Plan using Terraform

  2. Understanding basic Terraform workflows & commands

  3. Deploying an ASP.NET Core Web API to App Service instance

Summary:

This article demonstrated how to provision Azure App Service infrastructure using Terraform & deploy an ASP.NET Core Web API using Visual Studio. By combining Infrastructure as Code with automated deployment, developers can achieve consistent, repeatable & scalable cloud deployments.