您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

创建和部署第一个 Azure 资源管理器模板Create and deploy your first Azure Resource Manager template

本主题介绍如何通过相关步骤创建第一个 Azure 资源管理器模板。This topic walks you through the steps of creating your first Azure Resource Manager template. Resource Manager 模板为 JSON 文件,用于定义针对解决方案进行部署时所需的资源。Resource Manager templates are JSON files that define the resources you need to deploy for your solution. 若要了解与部署和管理 Azure 解决方案相关联的概念,请参阅 Azure 资源管理器概述To understand the concepts associated with deploying and managing your Azure solutions, see Azure Resource Manager overview. 如果有现成的资源,需要为这些资源获取模板,请参阅从现有资源导出 Azure 资源管理器模板If you have existing resources and want to get a template for those resources, see Export an Azure Resource Manager template from existing resources.

若要创建和修改模板,需要 JSON 编辑器。To create and revise templates, you need a JSON editor. Visual Studio Code 是轻量型开源跨平台代码编辑器。Visual Studio Code is a lightweight, open-source, cross-platform code editor. 强烈建议使用 Visual Studio Code 来创建资源管理器模板。We strongly recommend using Visual Studio Code for creating Resource Manager templates. 本文假定你使用 VS Code。This article assumes you are using VS Code. 如果有其他 JSON 编辑器(例如 Visual Studio),可以使用该编辑器。If you have another JSON editor (like Visual Studio), you can use that editor.

先决条件Prerequisites

创建模板Create template

一开始请使用简单的模板将存储帐户部署到订阅。Let's start with a simple template that deploys a storage account to your subscription.

  1. 选择“文件” > “新建文件”。Select File > New File.

    新建文件

  2. 将以下 JSON 语法复制并粘贴到文件中:Copy and paste the following JSON syntax into your file:

    {
      "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
      },
      "variables": {
      },
      "resources": [
        {
          "name": "[concat('storage', uniqueString(resourceGroup().id))]",
          "type": "Microsoft.Storage/storageAccounts",
          "apiVersion": "2016-01-01",
          "sku": {
            "name": "Standard_LRS"
          },
          "kind": "Storage",
          "location": "South Central US",
          "tags": {},
          "properties": {}
        }
      ],
      "outputs": {  }
    }
    

    存储帐户名称有多种限制,因此难以设置。Storage account names have several restrictions that make them difficult to set. 该名称必须为 3 到 24 个字符,只能使用数字和小写字母,而且必需唯一。The name must be between 3 and 24 characters in length, use only numbers and lower-case letters, and be unique. 前述模板使用 uniqueString 函数生成哈希值。The preceding template uses the uniqueString function to generate a hash value. 此模板通过添加前缀 storage 扩展了该哈希值增的含义。To give this hash value more meaning, it adds the prefix storage.

  3. 在本地文件夹中将该文件另存为 azuredeploy.json。Save this file as azuredeploy.json to a local folder.

    保存模板

部署模板Deploy template

已做好部署此模板的准备。You are ready to deploy this template. 请使用 PowerShell 或 Azure CLI 创建一个资源组。You use either PowerShell or Azure CLI to create a resource group. 然后,将存储帐户部署到该资源组。Then, you deploy a storage account to that resource group.

  • 对于 PowerShell,请在包含模板的文件夹中使用以下命令:For PowerShell, use the following commands from the folder containing the template:

    Connect-AzureRmAccount
    
    New-AzureRmResourceGroup -Name examplegroup -Location "South Central US"
    New-AzureRmResourceGroupDeployment -ResourceGroupName examplegroup -TemplateFile azuredeploy.json
    
  • 若要在本地安装 Azure CLI,请在包含模板的文件夹中使用以下命令:For a local installation of Azure CLI, use the following commands from the folder containing the template:

    az login
    
    az group create --name examplegroup --location "South Central US"
    az group deployment create --resource-group examplegroup --template-file azuredeploy.json
    

部署完成后,存储帐户就会存在于资源组中。When deployment finishes, your storage account exists in the resource group.

自定义模板Customize the template

该模板可以正常使用,但不灵活。The template works fine, but it is not flexible. 它始终将本地冗余存储部署到美国中南部。It always deploys a locally redundant storage to South Central US. 名称始终为存储后跟哈希值。The name is always storage followed by a hash value. 若要允许将模板用于不同的方案,请向模板添加参数。To enable using the template for different scenarios, add parameters to the template.

以下示例显示带两个参数的 parameters 节。The following example shows the parameters section with two parameters. 第一个参数 storageSKU 用于指定冗余类型。The first parameter storageSKU enables you to specify the type of redundancy. 它将可以传入的值限制为适用于存储帐户的值。It limits the values you can pass in to values that are valid for a storage account. 它还指定默认值。It also specifies a default value. 第二个参数 storageNamePrefix 设置为最多允许 11 个字符。The second parameter storageNamePrefix is set to allow a maximum of 11 characters. 它指定默认值。It specifies a default value.

"parameters": {
  "storageSKU": {
    "type": "string",
    "allowedValues": [
      "Standard_LRS",
      "Standard_ZRS",
      "Standard_GRS",
      "Standard_RAGRS",
      "Premium_LRS"
    ],
    "defaultValue": "Standard_LRS",
    "metadata": {
      "description": "The type of replication to use for the storage account."
    }
  },
  "storageNamePrefix": {
    "type": "string",
    "maxLength": 11,
    "defaultValue": "storage",
    "metadata": {
      "description": "The value to use for starting the storage account name. Use only lowercase letters and numbers."
    }
  }
},

在 variables 节,请添加名为 storageName 的变量。In the variables section, add a variable named storageName. 它组合了来自变量的前缀值和来自 uniqueString 函数的哈希值。It combines the prefix value from the parameters and a hash value from the uniqueString function. 它使用 toLower 函数将所有字符转换为小写。It uses the toLower function to convert all characters to lowercase.

"variables": {
  "storageName": "[concat(toLower(parameters('storageNamePrefix')), uniqueString(resourceGroup().id))]"
},

若要将这些新值用于存储帐户,请更改资源定义:To use these new values for your storage account, change the resource definition:

"resources": [
  {
    "name": "[variables('storageName')]",
    "type": "Microsoft.Storage/storageAccounts",
    "apiVersion": "2016-01-01",
    "sku": {
      "name": "[parameters('storageSKU')]"
    },
    "kind": "Storage",
    "location": "[resourceGroup().location]",
    "tags": {},
    "properties": {}
  }
],

请注意,存储帐户的名称现在设置为已添加的变量。Notice that the name of the storage account is now set to the variable that you added. SKU 名称设置为参数的值。The SKU name is set to the value of the parameter. 位置设置为资源组所在的位置。The location is set the same location as the resource group.

保存文件。Save your file.

模板现在如下所示:Your template now looks like:

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageSKU": {
      "type": "string",
      "allowedValues": [
        "Standard_LRS",
        "Standard_ZRS",
        "Standard_GRS",
        "Standard_RAGRS",
        "Premium_LRS"
      ],
      "defaultValue": "Standard_LRS",
      "metadata": {
        "description": "The type of replication to use for the storage account."
      }
    },   
    "storageNamePrefix": {
      "type": "string",
      "maxLength": 11,
      "defaultValue": "storage",
      "metadata": {
        "description": "The value to use for starting the storage account name. Use only lowercase letters and numbers."
      }
    }
  },
  "variables": {
    "storageName": "[concat(toLower(parameters('storageNamePrefix')), uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "name": "[variables('storageName')]",
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2016-01-01",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "Storage",
      "location": "[resourceGroup().location]",
      "tags": {},
      "properties": {}
    }
  ],
  "outputs": {  }
}

重新部署模板Redeploy template

请使用不同的值重新部署模板。Redeploy the template with different values.

对于 PowerShell,请使用:For PowerShell, use:

New-AzureRmResourceGroupDeployment -ResourceGroupName examplegroup -TemplateFile azuredeploy.json -storageNamePrefix newstore -storageSKU Standard_RAGRS

对于 Azure CLI,请使用:For Azure CLI, use:

az group deployment create --resource-group examplegroup --template-file azuredeploy.json --parameters storageSKU=Standard_RAGRS storageNamePrefix=newstore

使用自动完成功能Use autocomplete

目前为止,你在模板上所做的工作只是从本文复制并粘贴 JSON。So far, your work on the template has consisted of only copying and pasting JSON from this article. 但在开发自己的模板时,需查找并指定适用于资源类型的属性和值。However, when developing your own templates, you want to find and specify properties and values that are available for the resource type. VS Code 读取资源类型的架构,并建议属性和值。VS Code reads the schema for the resource type, and suggests properties and values. 若要查看自动完成功能,请转到模板的属性元素处,然后添加新的一行。To see the autocomplete feature, go the properties element of your template and add a new line. 键入一个引号,此时你会注意到,VS Code 立即建议可以在属性元素中使用的名称。Type a quotation mark, and notice that VS Code immediately suggests names that available within the properties element.

显示可用属性

选择“加密”。Select encryption. 键入冒号 (:),VS Code 会建议添加新对象。Type a colon (:), and VS Code suggests adding a new object.

添加对象

若要添加对象,请按 Tab 或 Enter。Press tab or enter to add the object.

再次键入引号,此时会看到 VS Code 建议适用于加密的属性。Again, type a quotation mark, and see that VS Code now suggests properties that are available for encryption.

显示加密属性

选择“服务”,然后根据 VS Code 扩展继续添加值,直到:Select services and continue adding values based on VS Code extensions until you have:

"properties": {
    "encryption":{
        "services":{
            "blob":{
              "enabled":true
            }
        }
    }
}

为存储帐户启用 Blob 加密。You have enabled blob encryption for the storage account. 但是,VS Code 发现了一个问题。However, VS Code has identified a problem. 请注意,加密有一个警告。Notice that encryption has a warning.

加密警告

若要查看警告,请将鼠标悬停在绿线上。To see the warning, hover over the green line.

缺少属性

可以看到,加密元素需要 keySource 属性。You see that the encryption element requires a keySource property. 在服务对象后添加一个逗号,然后添加 keySource 属性。Add a comma after the services object, and add the keySource property. VS Code 建议使用 "Microsoft.Storage" 作为有效值。VS Code suggests "Microsoft.Storage" as a valid value. 完成后,属性元素为:When finished, the properties element is:

"properties": {
    "encryption":{
        "services":{
            "blob":{
              "enabled":true
            }
        },
        "keySource":"Microsoft.Storage"
    }
}

最终模板为:The final template is:

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageSKU": {
      "type": "string",
      "allowedValues": [
        "Standard_LRS",
        "Standard_ZRS",
        "Standard_GRS",
        "Standard_RAGRS",
        "Premium_LRS"
      ],
      "defaultValue": "Standard_LRS",
      "metadata": {
        "description": "The type of replication to use for the storage account."
      }
    },   
    "storageNamePrefix": {
      "type": "string",
      "maxLength": 11,
      "defaultValue": "storage",
      "metadata": {
        "description": "The value to use for starting the storage account name. Use only lowercase letters and numbers."
      }
    }
  },
  "variables": {
    "storageName": "[concat(toLower(parameters('storageNamePrefix')), uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "name": "[variables('storageName')]",
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2016-01-01",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "Storage",
      "location": "[resourceGroup().location]",
      "tags": {},
      "properties": {
        "encryption":{
          "services":{
            "blob":{
              "enabled":true
            }
          },
          "keySource":"Microsoft.Storage"
        }
      }
    }
  ],
  "outputs": {}
}

部署加密的存储Deploy encrypted storage

再次部署模板并提供新的存储帐户名称。Again, deploy the template and provide a new storage account name.

对于 PowerShell,请使用:For PowerShell, use:

New-AzureRmResourceGroupDeployment -ResourceGroupName examplegroup -TemplateFile azuredeploy.json -storageNamePrefix storesecure

对于 Azure CLI,请使用:For Azure CLI, use:

az group deployment create --resource-group examplegroup --template-file azuredeploy.json --parameters storageNamePrefix=storesecure

清理资源Clean up resources

不再需要时,请通过删除资源组来清理部署的资源。When no longer needed, clean up the resources you deployed by deleting the resource group.

对于 PowerShell,请使用:For PowerShell, use:

Remove-AzureRmResourceGroup -Name examplegroup

对于 Azure CLI,请使用:For Azure CLI, use:

az group delete --name examplegroup

后续步骤Next steps