Open In App

How to Create App Service in Azure using Terraform

Last Updated : 26 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Understanding Of Primary Terminologies

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

  • Terraform: It is Infrastructure as an infrastructure-as-a-service tool that allows the deployment of resources to multiple cloud providers through code.
  • Azure App Service: App Service is a managed platform as a service for building, deploying and scaling applications in Azure.
  • IaaC: Infrastructure as a Code allows to representation of cloud infrastructure in the form of code.

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.

Azure CLI

  • 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.

Configuring Azure CLI

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

oie_WUV62j3y3sNs-(1)

  • 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.

oie_LPjvBk4Zz5At

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

oie_e5FiciXP6lfZ-(1)

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?

A basic Terraform configuration for Azure App Service includes a resource block for defining the Azure App Service itself, specifying properties such as name, location, resource group, and app service plan.

How do I manage configuration settings for my App Service with Terraform?

Terraform allows you to manage configuration settings for your App Service using Azure App Service Configuration resources. You can define settings such as connection strings, environment variables, and app settings directly in your Terraform configuration.

Can I deploy my application code using Terraform?

While Terraform is primarily focused on provisioning infrastructure, you can use additional tools or scripts (such as Azure DevOps pipelines or custom scripts) to automate the deployment of your application code alongside Terraform.

Is there a way to destroy the Azure App Service provisioned with Terraform?

Yes, you can destroy resources provisioned with Terraform using the terraform destroy command. This command will remove all resources defined in your Terraform configuration from your Azure subscription, so use it with caution.

How do I apply changes to my Azure App Service using Terraform?

After making changes to your Terraform configuration files, you can apply those changes by running the terraform apply command. Terraform will compare the desired state defined in your configuration with the current state in Azure and make the necessary changes to bring the infrastructure into the desired state.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads