Create App Service app 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 with Terraform.
Prerequisites
Azure subscription: If you don't have an Azure subscription, create a free account before you begin.
Configure Terraform: If you haven't already done so, configure Terraform using one of the following options:
- Configure Terraform in Azure Cloud Shell with Bash
- Configure Terraform in Azure Cloud Shell with PowerShell
- Configure Terraform in Windows with Bash
- Configure Terraform in Windows with PowerShell
The Azure Terraform Visual Studio Code extension enables you to work with Terraform from the editor. With this extension, you can author, test, and run Terraform configurations. The extension also supports resource graph visualization. See this guide for configuring the Azure Terraform Visual Studio Code extension.
Review the template
The template used in this quickstart is shown below. 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 = "eastus"
}
# 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
https_only = true
site_config {
minimum_tls_version = "1.2"
}
}
# 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 = "master"
use_manual_integration = true
use_mercurial = false
}
Four Azure resources are defined in the template. Links to the Azure Provider Terraform Registry are given below for further details and usage information:
- Microsoft.Resources/resourcegroups: create a Resource Group if one doesn't already exist.
- Microsoft.Web/serverfarms: create an App Service plan.
- Microsoft.Web/sites: create a Linux App Service app.
- Microsoft.Web/sites/sourcecontrols: create an external git deployment configuration.
For further information on how to construct Terraform templates, have a look at the Terraform Learn documentation.
Implement the Terraform code
Terraform provides many features for managing, building, deploying, and updating infrastructure. The steps below will just guide you through deploying and destroying 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.
Create a directory in which to test and run the sample Terraform code and make it the current directory.
mkdir appservice_tf_quickstart cd appservice_tf_quickstart
Create a file named
main.tf
and insert the above code.code main.tf
Initialize Terraform.
terraform init
Create the Terraform plan.
terraform plan
Provision the resources that are defined in the
main.tf
configuration file (Confirm the action by enteringyes
at the prompt).terraform apply
Validate the deployment
On the main menu of the Azure portal, select Resource groups and navigate to the resource group you created with the above template. It will be named "myResourceGroup-" followed by a string of random integers.
You now see all the resources that Terraform has created (an App Service and an App Service Plan).
Select the App Service and navigate to the url to verify your site has been created properly. Instead, you can just browse to
http://<app_name>.azurewebsites.net/
where 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 head back to your terminal/command line and execute terraform destroy
to delete all resources associated with this quickstart.
Note
You can find more Azure App Service Terraform samples here. You can find even more Terraform samples across all of the Azure services here.
Next steps
Feedback
Submit and view feedback for