Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.
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:
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.
- 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 more information on how to construct Terraform templates, see Terraform Learn documentation.
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.
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
Create a file named
main.tf
and insert the code from the previous step.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 entering yes at the prompt.terraform apply
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.You can see the App Service and an App Service plan that Terraform created.
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 iswebapp-
followed by that same string of random integers from the resource group.
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.