在 Azure 应用服务中,可以按需进行自定义备份或配置计划的自定义备份。 在本快速入门中,你将借助备份计划和 .NET 应用程序堆栈,使用 Terraform 创建 Azure Windows Web 应用。 有关应用服务备份和还原的详细信息,请参阅“在 Azure 应用服务中备份和还原应用”。
使用 Terraform 可以定义、预览和部署云基础结构。 使用 Terraform 时,请使用 HCL 语法来创建配置文件。 利用 HCL 语法,可指定 Azure 这样的云提供程序和构成云基础结构的元素。 创建配置文件后,请创建一个执行计划,利用该计划,可在部署基础结构更改之前先预览这些更改。 验证了更改后,请应用该执行计划以部署基础结构。
- 使用随机生成的名称创建 Azure 存储帐户和容器。
- 使用随机生成的名称创建 Azure 服务计划。
- 为存储帐户生成共享访问签名 (SAS)。
- 使用随机生成的名称创建 Azure Windows Web 应用。
- 为 Web 应用配置备份计划。
- 指定 Web 应用的应用程序堆栈。
- 输出使用 Terraform 脚本创建的关键资源的名称。
- 输出 Windows Web 应用的默认主机名。
创建具有活动订阅的 Azure 帐户。 可以创建帐户。
备注
本文中的示例代码位于 Azure Terraform GitHub 存储库中。 你可以查看包含当前和以前 Terraform 版本的测试结果的日志文件。
有关更多示例,请参阅演示如何使用 Terraform 管理 Azure 资源的文章和示例代码
创建用于测试和运行示例 Terraform 代码的目录,并将其设为当前目录。
创建名为
providers.tf
的文件并插入下列代码。
terraform {
required_version = ">=1.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
random = {
source = "hashicorp/random"
version = "~>3.0"
}
}
}
provider "azurerm" {
features {}
}
- 创建名为
main.tf
的文件并插入下列代码。
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"
}
}
}
- 创建名为
variables.tf
的文件并插入下列代码。
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."
}
- 创建名为
outputs.tf
的文件并插入下列代码。
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
}
运行 terraform init,将 Terraform 部署进行初始化。 此命令将下载管理 Azure 资源所需的 Azure 提供程序。
terraform init -upgrade
要点:
- 参数
-upgrade
可将必要的提供程序插件升级到符合配置版本约束的最新版本。
运行 terraform plan 以创建执行计划。
terraform plan -out main.tfplan
运行 terraform apply,将执行计划应用到云基础结构。
terraform apply main.tfplan
要点:
- 示例
terraform apply
命令假设你先前运行了terraform plan -out main.tfplan
。 - 如果为
-out
参数指定了不同的文件名,请在对terraform apply
的调用中使用该相同文件名。 - 如果未使用
-out
参数,请调用不带任何参数的terraform apply
。
运行 az webapp show 以查看 Azure Windows Web 应用。
az webapp show --name <web_app_name> --resource-group <resource_group_name>
请将 <web_app_name>
替换为 Azure Windows Web 应用的名称,将 <resource_group_name>
替换为资源组的名称。
不再需要通过 Terraform 创建的资源时,请执行以下步骤:
运行 terraform plan 并指定
destroy
标志。terraform plan -destroy -out main.destroy.tfplan
运行 terraform apply 以应用执行计划。
terraform apply main.destroy.tfplan