Introduction
Azure WebApp is a frequently used cloud service that helps us in hosting our application code as a Platform Service offering. In the development environment, we may create an Azure WebApp using the portal and deploy our code. However, when we are dealing with the production environments, we must take an Infrastructure as Code (IaC) approach to automate the creation of Azure WebApp. In this article let us explore how we can create an Azure WebApp using Terraform.
In the previous article, we explored the concept of Infrastructure as Code (IaC) and how to get started with Infrastructure as Code (IaC) using Terraform. We took a simple use case of how to create a Resource Group using Terraform. The following is the link to the previous article of this series.
Create Azure WebApp using Terraform
Let us create a simple Windows-based Azure WebApp. The following are the resources that we must create before creating a WebApp.
- Resource Group
- App Service Plan
Any Azure resource must be created inside a Resource Group and an Azure WebApp must be associated with an App Service Plan. An App Service Plan defines the compute, scaling, and other necessary requirements for Azure WebApp.
Log in to the Azure portal. Let us use Azure Cloud Shell to create resources using Terraform. Azure Cloud Shell has Terraform installed and you need not do any installation or configuration to work with Terraform.

Once the Azure Cloud Shell opens up, select Bash. Let us start creating scripts to create an Azure WebApp. We can use a nano editor to create the Infrastructure as Code script for the WebApp using Terraform.

Execute the following command to open a nano editor and create a file named myterraformscript.tf.
- nano myterraformscript.tf
Add the following code to the nano editor. This would create a Resource Group. Replace {ResourceGroup} with name of your Resource Group that you are planning to create.
- terraform {
- required_providers {
- azurerm = {
- source = "hashicorp/azurerm"
- }
- }
- }
- provider "azurerm" {
- features {}
- }
- resource "azurerm_resource_group" "{ResourceGroup}" {
- name = "{ResourceGroup}"
- location = "eastus"
- }
Add the following code in nano editor to create an App Service Plan. Replace {ResourceGroup} with name of your Resource Group that you are planning to create. Replace {AppServicePlanName} with name of the App Service Plan you are planning to create.
- resource "azurerm_app_service_plan" "{AppServicePlanName}" {
- name = "{AppServicePlanName}"
- location = "eastus"
- resource_group_name = azurerm_resource_group.{ResourceGroup}.name
- sku {
- tier = "Standard"
- size = "S1"
- }
- }


Neetika JaiswalPosted Jul 21, 2021, 4:16 AM
Nice explanation