Terraform On Azure - Creating A WebApp

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.
 
Terraform On Azure - Creating A WebApp
 
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.
 
Terraform On Azure - Creating A WebApp
 
Execute the following command to open a nano editor and create a file named myterraformscript.tf.
  1.   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.
  1. terraform {    
  2.   required_providers {    
  3.     azurerm = {    
  4.       source = "hashicorp/azurerm"    
  5.     }    
  6.   }    
  7. }    
  8. provider "azurerm" {    
  9.   features {}    
  10. }    
  11. resource "azurerm_resource_group" "{ResourceGroup}" {    
  12.   name = "{ResourceGroup}"    
  13.   location = "eastus"    
  14. }    
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.
  1. resource "azurerm_app_service_plan" "{AppServicePlanName}" {  
  2.   name                = "{AppServicePlanName}"  
  3.   location            = "eastus"  
  4.   resource_group_name = azurerm_resource_group.{ResourceGroup}.name  
  5.   
  6.   sku {  
  7.     tier = "Standard"  
  8.     size = "S1"  
  9.   }  
  10. }   
Add the following code to create a WebApp. 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. Replace {WebAppName} with name of the WebApp that you are planning to create. You are also adding few appsetting keys and values.
  1. resource "azurerm_app_service" "{WebAppName}" {  
  2.   name                = "{WebAppName}"  
  3.   location            = "eastus"  
  4.   resource_group_name = azurerm_resource_group.{ResourceGroup}.name  
  5.   app_service_plan_id = azurerm_app_service_plan.{AppServicePlanName}.id  
  6.   
  7.   app_settings = {  
  8.     "DeviceName" = "SampleDevice",  
  9.     "DeviceId" = "2"  
  10.   }  
  11. }  
The following is the script file that you created. You can refer to the attached script file and try out the sample. Replace {ResourceGroup} with the name of your Resource Group that you are planning to create. Replace {AppServicePlanName} with the name of the App Service Plan you are planning to create. Replace {WebAppName} with the name of the WebApp that you are planning to create. You are also adding a few appsetting keys and values.
  1. terraform {    
  2.   required_providers {    
  3.     azurerm = {    
  4.       source = "hashicorp/azurerm"    
  5.     }    
  6.   }    
  7. }    
  8. provider "azurerm" {    
  9.   features {}    
  10. }    
  11. resource "azurerm_resource_group" "{ResourceGroup}" {    
  12.   name = "{ResourceGroup}"    
  13.   location = "eastus"    
  14. }   
  15. resource "azurerm_app_service_plan" "{AppServicePlanName}" {  
  16.   name                = "{AppServicePlanName}"  
  17.   location            = "eastus"  
  18.   resource_group_name = azurerm_resource_group.{ResourceGroup}.name  
  19.   
  20.   sku {  
  21.     tier = "Standard"  
  22.     size = "S1"  
  23.   }  
  24. }  
  25.   
  26. resource "azurerm_app_service" "{WebAppName}" {  
  27.   name                = "{WebAppName}"  
  28.   location            = "eastus"  
  29.   resource_group_name = azurerm_resource_group.{ResourceGroup}.name  
  30.   app_service_plan_id = azurerm_app_service_plan.{AppServicePlanName}.id  
  31.   
  32.   app_settings = {  
  33.     "DeviceName" = "SampleDevice",  
  34.     "DeviceId" = "2"  
  35.   }  
  36. }  
Run the following command to initiate Terraform. This would fetch all dependencies needed to execute the Terraform script.
  1. terraform init  
Now let us create an execution plan for Terraform. Let us provide the name of the execution plan in the out parameter.
  1. terraform plan -out myrg.tfplan   
Execute the execution plan using the following command,
  1. terraform apply "myrg.tfplan"  
The WebApp gets created inside the Resource Group using the App Service Plan that we have specified.
 
Terraform On Azure - Creating A WebApp
 

Conclusion

 
In this article, we learned how to create an Azure WebApp. In the next article, we will learn how to create a Virtual Machine using Terraform scripts.


Similar Articles