Setup Azure App Service Using Terraform: A Step-By-Step Guide

Step 1: Set Up Terraform

  • Download the Terraform zip from the installation page of the Terraform website.
  • Extract and paste the terraform folder to the required location and add the path to runnable in environment variables.
  • On MacOS download Terraform using brew command.

Step 2: Set Up Azure CLI

  • Download the Azure CLI setup from the official website.
  • Run the installer and follow the steps to install.

  • For MacOS install CLI using below brew command.
brew update && brew install azure-cli

Step 3: Configure Azure CLI

  • Open terminal and run below command.
az login
  • A browser window will open for login. Login with your azure credentials. Once it is done you will see output as below.

Step 4: Create Terraform Code

  • Goto your project folder and create main.tf file.
  • Add terraform block to code with azure as required provider with latest version. You can find the latest version at hashicorp registry.
  • Terraform block should look like below. You can add required version to avoid invalidation.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
}
}
  • Now add provider as azurerm like below. Specify other details as required.
provider "azurerm" {
features {}
}
  • Add configuration for Azure App Service. First we will create app service plan described as below.
resource "azurerm_app_service_plan" "deepcodr-firstplan" {
name = "firstapp-plan"
resource_group_name = "DeepsLab"
location = "eastus"

sku {
tier = "Standard"
size = "S1"
}
}
  • We have specified name for App service plan. We have specified the location and resource group where plan should be created.
  • Now lets add configuration for app service. We will also specify app service plan id which was described earlier.
resource "azurerm_app_service" "deepcodr-firstapp" {
name = "deepcodr-firstapp"
resource_group_name = "DeepsLab"
location = "eastus"
app_service_plan_id = azurerm_app_service_plan.deepcodr-firstplan.id

site_config {
python_version = "3.4"
scm_type = "LocalGit"
}
}
  • The complete code will look like below.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
}
}

provider "azurerm" {
features {}
}

resource "azurerm_app_service_plan" "deepcodr-firstplan" {
name = "firstapp-plan"
resource_group_name = "DeepsLab"
location = "eastus"

sku {
tier = "Standard"
size = "S1"
}
}

resource "azurerm_app_service" "deepcodr-firstapp" {
name = "deepcodr-firstapp"
resource_group_name = "DeepsLab"
location = "eastus"
app_service_plan_id = azurerm_app_service_plan.deepcodr-firstplan.id

site_config {
python_version = "3.4"
scm_type = "LocalGit"
}
}
  • Site config has been added to describe application language and source control type.

Step 5: Apply The Terraform Code

  • Once the code is ready you can apply it.
  • First init the terraform by running below command in project folder where main.tf is present.
terraform init

  • After successful output of terraform apply the changes using below command.
terrraform apply
  • After verifying type “yes” to confirm and apply.
  • Terraform will start creating service plan and app service.

  • You can also verify deployment by visiting App Service page of Azure.

How to Create App Service in Azure using Terraform

Azure App Service is a service that provides a managed platform for deploying applications in the Azure cloud. It supports multiple language applications. App service allows building, deploying and scaling of applications in Azure. Setting up an app service is a complicated process. let’s see how we can set up an app service in Azure using Terraform.

Similar Reads

Understanding Of Primary Terminologies

The following are the primary components of Azure App Service related to Terraform:...

Setup Azure App Service Using Terraform: A Step-By-Step Guide

Step 1: Set Up Terraform...

Conclusion

We have successfully created an Azure App service with the help of terraform in this article. the configuration described can be further modified to make changes to site configuration and application runtime. This is how terraform allows reusable and modifiable configuration of infrastructure....

How to create app service in azure using terraform – FAQ’s

What does a basic Terraform configuration for Azure App Service look like?...

Contact Us