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.
In Azure App Service, you can make on-demand custom backups or configure scheduled custom backups. In this quickstart, you use Terraform to create an Azure Windows web app with a backup schedule and a .NET application stack. For more information about App Service backups and restores, see Back up and restore your app in Azure App Service.
Terraform enables the definition, preview, and deployment of cloud infrastructure. Using Terraform, you create configuration files using HCL syntax. The HCL syntax allows you to specify the cloud provider - such as Azure - and the elements that make up your cloud infrastructure. After you create your configuration files, you create an execution plan that allows you to preview your infrastructure changes before they're deployed. Once you verify the changes, you apply the execution plan to deploy the infrastructure.
- Create an Azure storage account and container with the randomly generated name .
- Create an Azure service plan with the randomly generated name .
- Generate a Shared Access Signature (SAS) for the storage account.
- Create an Azure Windows web app with the randomly generated name .
- Configure a backup schedule for the web app.
- Specify the application stack for the web app.
- Output the names of key resources created with the Terraform script.
- Output the default hostname of the Windows web app.
Create an Azure account with an active subscription. You can create an account.
Note
The sample code for this article is located in the Azure Terraform GitHub repo. You can view the log file containing the test results from current and previous versions of Terraform.
See more articles and sample code showing how to use Terraform to manage Azure resources
Create a directory in which to test and run the sample Terraform code and make it the current directory.
Create a file named
providers.tf
and insert the following code.
terraform {
required_version = ">=1.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
random = {
source = "hashicorp/random"
version = "~>3.0"
}
}
}
provider "azurerm" {
features {}
}
- Create a file named
main.tf
and insert the following code.
resource "random_pet" "rg_name" {
prefix = var.resource_group_name_prefix
}
resource "azurerm_resource_group" "rg" {
location = var.resource_group_location
name = random_pet.rg_name.id
}
resource "random_string" "storage_account_name" {
length = 8
lower = true
numeric = false
special = false
upper = false
}
resource "azurerm_storage_account" "example" {
name = random_string.storage_account_name.result
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "random_string" "storage_container_name" {
length = 8
lower = true
numeric = false
special = false
upper = false
}
resource "azurerm_storage_container" "example" {
name = random_string.storage_container_name.result
storage_account_name = azurerm_storage_account.example.name
container_access_type = "private"
}
resource "random_string" "service_plan_name" {
length = 8
lower = true
numeric = false
special = false
upper = false
}
resource "azurerm_service_plan" "example" {
name = random_string.service_plan_name.result
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
os_type = "Windows"
sku_name = "S1"
}
data "azurerm_storage_account_sas" "example" {
connection_string = azurerm_storage_account.example.primary_connection_string
https_only = true
resource_types {
service = false
container = false
object = true
}
services {
blob = true
queue = false
table = false
file = false
}
# Please change the start_date variable (in variables.tf) to the appropriate
# value for your environment.
start = formatdate(var.start_date, timestamp())
expiry = formatdate(var.start_date, timeadd(timestamp(), "8765h"))
permissions {
read = false
write = true
delete = false
list = false
add = false
create = false
update = false
process = false
tag = false
filter = false
}
}
resource "random_string" "windows_web_app_name" {
length = 8
lower = true
numeric = false
special = false
upper = false
}
resource "azurerm_windows_web_app" "example" {
name = random_string.windows_web_app_name.result
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
service_plan_id = azurerm_service_plan.example.id
backup {
name = "Example"
storage_account_url = "https://${azurerm_storage_account.example.name}.blob.core.chinacloudapi.cn/${azurerm_storage_container.example.name}${data.azurerm_storage_account_sas.example.sas}&sr=b"
schedule {
frequency_interval = 30
frequency_unit = "Day"
}
}
site_config {
application_stack {
dotnet_version = "v6.0"
current_stack = "dotnet"
}
}
}
- Create a file named
variables.tf
and insert the following code.
variable "resource_group_name_prefix" {
type = string
default = "rg"
description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
}
variable "resource_group_location" {
type = string
default = "eastus"
description = "Location of the resource group."
}
variable "start_date" {
type = string
default = "2024-06-01"
description = "Start date."
}
- Create a file named
outputs.tf
and insert the following code.
output "resource_group_name" {
value = azurerm_resource_group.rg.name
}
output "storage_account_name" {
value = azurerm_storage_account.example.name
}
output "storage_container_name" {
value = azurerm_storage_container.example.name
}
output "service_plan_name" {
value = azurerm_service_plan.example.name
}
output "windows_web_app_name" {
value = azurerm_windows_web_app.example.name
}
output "windows_web_app_default_hostname" {
value = azurerm_windows_web_app.example.default_hostname
}
Run terraform init to initialize the Terraform deployment. This command downloads the Azure provider required to manage your Azure resources.
terraform init -upgrade
Key points:
- The
-upgrade
parameter upgrades the necessary provider plugins to the newest version that complies with the configuration's version constraints.
Run terraform plan to create an execution plan.
terraform plan -out main.tfplan
Run terraform apply to apply the execution plan to your cloud infrastructure.
terraform apply main.tfplan
Key points:
- The example
terraform apply
command assumes you previously ranterraform plan -out main.tfplan
. - If you specified a different filename for the
-out
parameter, use that same filename in the call toterraform apply
. - If you didn't use the
-out
parameter, callterraform apply
without any parameters.
Run az webapp show to view the Azure Windows web app.
az webapp show --name <web_app_name> --resource-group <resource_group_name>
Replace <web_app_name>
with the name of your Azure Windows web app and <resource_group_name>
with the name of your resource group.
When you no longer need the resources created via Terraform, do the following steps:
Run terraform plan and specify the
destroy
flag.terraform plan -destroy -out main.destroy.tfplan
Run terraform apply to apply the execution plan.
terraform apply main.destroy.tfplan