使用 Bicep 和 Azure Cache for Redis 创建 Web 应用

在本文中,你将利用 Bicep 部署使用 Azure Cache for Redis 的 Azure Web 应用,并部署一个应用服务计划。

Bicep 是一种特定于域的语言 (DSL),使用声明性语法来部署 Azure 资源。 它提供简明的语法、可靠的类型安全性以及对代码重用的支持。 Bicep 会针对你的 Azure 基础结构即代码解决方案提供最佳创作体验。

可以将此 Bicep 文件用于自己的部署。 Bicep 文件为 Azure Web 应用、应用服务计划和 Azure Cache for Redis 提供唯一的名称。 如果需要,可以在将 Bicep 文件保存到本地设备后对其进行自定义以满足要求。

若要详细了解如何创建 Bicep 文件,请参阅快速入门:使用 Visual Studio Code 创建 Bicep 文件。 要了解 Bicep 语法,请参阅了解 Bicep 文件的结构和语法

查阅 Bicep 文件

本快速入门中使用的 Bicep 文件来自 Azure 快速入门模板

@description('Describes plan\'s pricing tier and instance size. Check details at https://www.azure.cn/pricing/details/app-service/')
@allowed([
  'F1'
  'D1'
  'B1'
  'B2'
  'B3'
  'S1'
  'S2'
  'S3'
  'P1'
  'P2'
  'P3'
  'P4'
])
param skuName string = 'F1'

@description('Describes plan\'s instance count')
@minValue(1)
@maxValue(7)
param skuCapacity int = 1

@description('The pricing tier of the new Azure Redis Cache.')
@allowed([
  'Basic'
  'Standard'
])
param cacheSKUName string = 'Basic'

@description('The family for the sku.')
@allowed([
  'C'
])
param cacheSKUFamily string = 'C'

@description('The size of the new Azure Redis Cache instance. ')
@minValue(0)
@maxValue(6)
param cacheSKUCapacity int = 0

@description('Location for all resources.')
param location string = resourceGroup().location

var hostingPlanName = 'hostingplan${uniqueString(resourceGroup().id)}'
var webSiteName = 'webSite${uniqueString(resourceGroup().id)}'
var cacheName = 'cache${uniqueString(resourceGroup().id)}'

resource hostingPlan 'Microsoft.Web/serverfarms@2021-03-01' = {
  name: hostingPlanName
  location: location
  tags: {
    displayName: 'HostingPlan'
  }
  sku: {
    name: skuName
    capacity: skuCapacity
  }
  properties: {
  }
}

resource webSite 'Microsoft.Web/sites@2021-03-01' = {
  name: webSiteName
  location: location
  tags: {
    'hidden-related:${hostingPlan.id}': 'empty'
    displayName: 'Website'
  }
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    serverFarmId: hostingPlan.id
    httpsOnly: true
  }
  dependsOn: [
    cache
  ]
}

resource appsettings 'Microsoft.Web/sites/config@2021-03-01' = {
  parent: webSite
  name: 'appsettings'
  properties: {
    CacheConnection: '${cacheName}.redis.cache.chinacloudapi.cn,abortConnect=false,ssl=true,password=${cache.listKeys().primaryKey}'
    minTlsVersion: '1.2'
    ftpsState: 'FtpsOnly'
  }
}

resource cache 'Microsoft.Cache/Redis@2021-06-01' = {
  name: cacheName
  location: location
  tags: {
    displayName: 'cache'
  }
  properties: {
    sku: {
      name: cacheSKUName
      family: cacheSKUFamily
      capacity: cacheSKUCapacity
    }
  }
}

使用此 Bicep 文件,可以部署:

部署 Bicep 文件

  1. 将该 Bicep 文件另存为本地计算机上的 main.bicep。

  2. 使用 Azure CLI 或 Azure PowerShell 来部署该 Bicep 文件。

    az group create --name exampleRG --location chinanorth
    az deployment group create --resource-group exampleRG --template-file main.bicep
    

    部署完成后,应会看到一条指出部署成功的消息。

查看已部署的资源

使用 Azure 门户、Azure CLI 或 Azure PowerShell 列出资源组中已部署的资源。

az resource list --resource-group exampleRG

清理资源

如果不再需要资源组及其资源,请使用 Azure 门户、Azure CLI 或 Azure PowerShell 将其删除。

az group delete --name exampleRG

后续步骤

若要了解有关 Bicep 的详细信息,请继续阅读以下文章: