Quickstart: Create an App Service app by using a Terraform template

Get started with Azure App Service by deploying an app to the cloud using Terraform . Because you use a free App Service tier, you incur no costs to complete this quickstart.

Terraform allows you to define and create complete infrastructure deployments in Azure. You build Terraform templates in a human-readable format that create and configure Azure resources in a consistent, reproducible manner. This article shows you how to create a Windows app by using Terraform.

Prerequisites

  • An Azure subscription. If you don't have an Azure subscription, create a Trial before you begin.

  • A Terraform configuration. Use one of the following options:

Review the template

This quickstart uses the following template. It deploys an App Service plan and an App Service app on Linux and a sample Node.js Hello World app from the Azure Samples repo.

# Configure the Azure provider
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.0.0"
    }
  }
  required_version = ">= 0.14.9"
}
provider "azurerm" {
  features {}
}

# Generate a random integer to create a globally unique name
resource "random_integer" "ri" {
  min = 10000
  max = 99999
}

# Create the resource group
resource "azurerm_resource_group" "rg" {
  name     = "myResourceGroup-${random_integer.ri.result}"
  location = "chinaeast"
}

# Create the Linux App Service Plan
resource "azurerm_service_plan" "appserviceplan" {
  name                = "webapp-asp-${random_integer.ri.result}"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  os_type             = "Linux"
  sku_name            = "B1"
}

# Create the web app, pass in the App Service Plan ID
resource "azurerm_linux_web_app" "webapp" {
  name                  = "webapp-${random_integer.ri.result}"
  location              = azurerm_resource_group.rg.location
  resource_group_name   = azurerm_resource_group.rg.name
  service_plan_id       = azurerm_service_plan.appserviceplan.id
  depends_on            = [azurerm_service_plan.appserviceplan]
  https_only            = true
  site_config { 
    minimum_tls_version = "1.2"
    application_stack {
      node_version = "16-lts"
    }
  }
}

#  Deploy code from a public GitHub repo
resource "azurerm_app_service_source_control" "sourcecontrol" {
  app_id             = azurerm_linux_web_app.webapp.id
  repo_url           = "https://github.com/Azure-Samples/nodejs-docs-hello-world"
  branch             = "main"
  use_manual_integration = true
  use_mercurial      = false
}

The template defines the following four Azure resources. For further details and usage information, see the Azure Provider Terraform Registry.

For more information on how to construct Terraform templates, see Terraform Learn documentation.

Implement the Terraform code

Terraform provides many features that you can use to manage, build, deploy, and update infrastructure. The following steps show you how to deploy and destroy your resources. The Terraform Learn documentation and Terraform on Azure documentation go into more detail and should be reviewed if Terraform is part of your Azure infrastructure strategy.

  1. Create a directory in which to test and run the sample Terraform code. Make it the current directory.

    mkdir appservice_tf_quickstart
    cd appservice_tf_quickstart
    
  2. Create a file named main.tf and insert the code from the previous step.

    code main.tf
    
  3. Initialize Terraform.

    terraform init
    
  4. Create the Terraform plan.

    terraform plan
    
  5. Provision the resources that are defined in the main.tf configuration file. Confirm the action by entering yes at the prompt.

    terraform apply
    

Validate the deployment

  1. On the main menu of the Azure portal, select Resource groups and go to the resource group that you created by using the preceding template. The name is myResourceGroup- followed by a string of random integers.

  2. You can see the App Service and an App Service plan that Terraform created.

  3. Select App Service and go to the URL to verify that your site was created properly. You can also browse to http://<app_name>.chinacloudsites.cn, where the app name is webapp- followed by that same string of random integers from the resource group.

Clean up resources

When no longer needed, either delete the resource group or go back to your terminal and execute terraform destroy to delete all resources associated with this quickstart.

Note

To find more Azure App Service Terraform samples, see Terraform samples for Azure App Service. You can find even more Terraform samples across all of the Azure services on GitHub.