Deploy Azure WebApp Using Terraform

Introduction

Azure App Service is a fully managed platform designed to simplify the creation, deployment, and scaling of WebApps. With support for continuous deployment (CD) and infrastructure automation using tools like Terraform, it enables a streamlined and efficient development process.

Prerequisites

  1. An active Azure Subscription
  2. Terraform is installed locally.

Here is my GitHub repository. Feel free to clone it using the link Azure_WebApp_Terraform Github Repo

Deploy step by step

Step 1. Setup Terraform using this article Setup Terraform

Step 2. Authenticate Terraform with your Azure subscription using the Azure CLI.

az login
az account set --subscription <your-subscription-id>

Step 3. Create a file named main.tf and include the following code.

# Configure the Azure provider
provider "azurerm" {
  features {}
}
# Create a resource group
resource "azurerm_resource_group" "demo" {
  name     = "demo-resources"
  location = "West Europe"
}
# Create an App Service plan with Standard tier
resource "azurerm_app_service_plan" "demo" {
  name                = "demo-appserviceplan"
  location            = azurerm_resource_group.demo.location
  resource_group_name = azurerm_resource_group.demo.name

  sku {
    tier = "Standard"
    size = "S1"
  }
}
# Create an App Service
resource "azurerm_app_service" "demo" {
  name                = "DemoApp-appservice"
  location            = azurerm_resource_group.demo.location
  resource_group_name = azurerm_resource_group.demo.name
  app_service_plan_id = azurerm_app_service_plan.demo.id

  app_settings = {
    "WEBSITE_RUN_FROM_PACKAGE" = "1"
  }
}
# Output the App Service URL
output "app_service_default_hostname" {
  value = azurerm_app_service.demo.default_site_hostname
}

Step 4. Initialize Terraform.

terraform init
terraform plan
terraform apply

Note. Type yes when prompted to confirm the deployment.

Step 5. Verify Deployment.

  1. Navigate to the Azure Portal.
  2. Open the Web app under App Services and verify the deployment.
  3. Access the app using the provided URL.

Conclusion

This article provides a step-by-step guide to deploying an Azure WebApp using Terraform, leveraging Infrastructure as Code (IaC) for efficient and repeatable deployments.


Similar Articles