Create an Azure App Service using Terraform

Create an Azure App Service using Terraform

Dhaval Baldha

21 Dec 2023

7 MINUTES READ

Introduction to Terraform

Terraform, developed by HashiCorp, is an open-source IaC tool that allows developers to define and provision infrastructure using a declarative configuration language. Its primary objective is to simplify and streamline the process of managing complex cloud resources efficiently.

Azure App Service Overview

Before we explore Terraform's role, let's understand Azure App Service. This fully managed platform allows developers to build, deploy, and scale web apps effortlessly. With features like automatic scaling, continuous deployment, and integrated developer tools, Azure App Service provides a robust environment for hosting web applications.

Why Terraform for Azure App Service Deployment?

Utilizing Terraform for deploying applications on Azure App Service brings several advantages. It enables developers to define infrastructure as code, providing consistency and repeatability in deployments. Additionally, Terraform allows for efficient collaboration among team members and simplifies the tracking of changes made to the infrastructure.

Setting Up Terraform for Azure

To begin the journey with Terraform and Azure, developers need to install Terraform and configure the Azure provider. This involves obtaining the necessary credentials and permissions to interact with Azure resources programmatically.

Terraform Configuration Files

Terraform uses configuration files written in HashiCorp Configuration Language (HCL). These files define the desired state of the infrastructure and include essential elements such as variables, resources, and providers.

terraform.tfstate and terraform.tfstate.backup files

Understanding the Terraform Project

The Terraform project is structured with a main directory and a modules directory. The main directory contains essential files like prod.tf, providers.tf, test.tf and others, while the modules directory dives deeper into the specifics of your Azure App Service deployment such as appservice.tf, variables.tf, outputs.tf.

The terraform.tfstate and terraform.tfstate.backup files are associated with Terraform, an infrastructure as code (IaC) tool used to provision and manage infrastructure resources. These files store the state of the infrastructure, including the details of the resources created by Terraform.

terraform.tfstate:
  • This file contains the current state of the infrastructure as managed by Terraform.
  • It's a JSON-formatted file that includes information about the resources, their configurations, and any dependencies.
  • Terraform uses this file to determine the differences between the desired state (as defined in the Terraform configuration files) and the actual state.
terraform.tfstate.backup:
  • This file is a backup of the previous state before the last terraform apply operation.
  • It's created to provide a safety net in case something goes wrong during an apply operation.
  • If an apply operation fails or if you want to revert to the previous state, Terraform can use this backup file.

|- appservice

|- providers.tf

|- prod.tf

|- test.tf

|- modules

|- appservice.tf

|- outputs.tf

|- variables.tf

Main Directory

  • providers.tf The provider configuration file. Here, you specify the details of your Azure subscription and any other providers your project might use.

    devops@techvoot:~/Documents/terraform-projects/appservice$ cat providers.tf 
    provider "azurerm" {
        features {}
    }


    terraform {
        required_providers {
            azurerm = {
            source  = "hashicorp/azurerm"
            version = "2.78.0"
            }
        }
    }

  • prod.tf This file likely contains the main configuration for the Azure App Service. It's where you define the resources and configurations specific to your production environment.

    devops@techvoot:~/Documents/terraform-projects/appservice$ cat prod.tf 
    module "app_service_prod" {
        source = "./modules/"
        app_service_plan_name = "plan-prod"
        app_service_name = "test-app-service-prod"
        resource_group_name = "appservice-sample-prod-rg"
        resource_group_location = "West Europe"
    }
    
  • test.tf A file that might contain your testing configurations, ensuring your Terraform scripts work as expected.

    devops@techvoot:~/Documents/terraform-projects/appservice$ cat test.tf 
    module "app_service_test" {
        source = "./modules/"
        app_service_plan_name = "plan-test"
        app_service_name = "test-app-service-test"
        resource_group_name = "appservice-sample-test-rg"
        resource_group_location = "West Europe"
    }

Modules Directory

appservice.tf

In this file, you define the Azure App Service and its associated resources. Key components include:

  • prod.tf This file likely contains the main configuration for the Azure App Service. It's where you define the resources and configurations specific to your production environment.

    devops@techvoot:~/Documents/terraform-projects/appservice/modules$ cat appservice.tf 
        resource "azurerm_resource_group" "rg" {
        name     = var.resource_group_name
        location = var.resource_group_location
    }
    resource "azurerm_app_service_plan" "app_service_plan" {
        name                = var.app_service_plan_name
        location            = azurerm_resource_group.rg.location
        resource_group_name = azurerm_resource_group.rg.name
        
        sku {
            tier = var.app_service_plan.tier
            size = var.app_service_plan.size
        }
        
    }
        
        resource "azurerm_app_service" "app_service" {
        name                = var.app_service_name
        location            = azurerm_app_service_plan.app_service_plan.location
        resource_group_name = azurerm_resource_group.rg.name
        app_service_plan_id = azurerm_app_service_plan.app_service_plan.id
        
    }

variables.tf

This file holds variables used in the Terraform configurations. It allows to parameterize your code, making it more versatile and adaptable to different environments.


    devops@techvoot:~/Documents/terraform-projects/appservice/modules$ cat variables.tf 
    variable "app_service_plan_name" {
        type        = string
        description = "App Service Plan name in Azure"

    }

    variable "app_service_name" {
        type = string
        description = "Name for the app service"
    }

    variable "resource_group_name" {
        type        = string
        description = "RG name in Azure"
    }

    variable "resource_group_location" {
        type        = string
        description = "RG location in Azure"
    }


    variable "app_service_plan" {
        type        = map
        description = "App Service Tier and SKU"
        default = {
            tier = "Free"
            size = "F1"
        }
    }

outputs.tf

Outputs defined here provide valuable information after the deployment. For instance, you might output the URL of the deployed Azure App Service, allowing easy access for further testing or sharing.


    devops@techvoot:~/Documents/terraform-projects/appservice/modules$ cat outputs.tf 
    output "webapp_url" {
        value = azurerm_app_service.app_service.default_site_hostname
    }

    output "webapp_ips" {
        value = azurerm_app_service.app_service.outbound_ip_addresses
    }

Deploying Your Azure App Service with Terraform

Now that you're familiar with the structure of your Terraform project, deploying your Azure App Service is a breeze. Simply navigate to the root directory and run the following commands:

  • $ terraform init: Initializes a Terraform working directory, including downloading providers and modules.
  • $ terraform plan: Creates an execution plan that shows what Terraform will do when you apply your configuration.
  • $ terraform apply: Applies the changes required to reach the desired state.
  • $ terraform show: Displays the current state or a saved plan.
  • $ terraform destroy: Destroys the Terraform-managed infrastructure.
  • $ terraform state : terraform state: Used to perform various operations on the Terraform state.

Conclusion

The integration of Terraform with Azure App Service proves to be a formidable combination for streamlining the deployment and management of web applications on the Azure cloud. By adopting Infrastructure as Code (IaC) principles, developers benefit from a structured and version-controlled approach to defining, provisioning, and maintaining their infrastructure.


Dhaval Baldha
Dhaval Baldha

Co-founder

Dhaval is a visionary leader driving technological innovation and excellence. With a keen strategic mindset and deep industry expertise, he propels the company towards new heights. His leadership and passion for technology make him a cornerstone of Techvoot Solutions' success.

Linkedin
Hire Skilled Developer

*Please fill all the required fields

Get our Newsletter

Customized solutions for your projects

*Please fill all the required fields