次の方法で共有


チュートリアル: Azure クイック スタート テンプレートを使用する

Azure クイック スタート テンプレート は、コミュニティが提供するテンプレートのリポジトリです。 テンプレート開発でサンプル テンプレートを使用できます。 このチュートリアルでは、Web サイトのリソース定義を見つけて、独自のテンプレートに追加します。 この命令の完了には 12 分 かかります。

[前提条件]

エクスポートされたテンプレートに関するチュートリアルを完了することをお勧めしますが、必須ではありません。

Visual Studio Code と、Azure PowerShell または Azure Command-Line インターフェイス (CLI) が必要です。 詳細については、 テンプレート ツールを参照してください。

テンプレートを確認する

前のチュートリアルの最後に、テンプレートには次の JSON ファイルがありました。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storagePrefix": {
      "type": "string",
      "minLength": 3,
      "maxLength": 11
    },
    "storageSKU": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_RAGRS",
        "Standard_ZRS",
        "Premium_LRS",
        "Premium_ZRS",
        "Standard_GZRS",
        "Standard_RAGZRS"
      ]
    },
    "___location": {
      "type": "string",
      "defaultValue": "[resourceGroup().___location]"
    },
    "appServicePlanName": {
      "type": "string",
      "defaultValue": "exampleplan"
    }
  },
  "variables": {
    "uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2021-09-01",
      "name": "[variables('uniqueStorageName')]",
      "___location": "[parameters('___location')]",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    },
    {
      "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2021-03-01",
      "name": "[parameters('appServicePlanName')]",
      "___location": "[parameters('___location')]",
      "sku": {
        "name": "B1",
        "tier": "Basic",
        "size": "B1",
        "family": "B",
        "capacity": 1
      },
      "kind": "linux",
      "properties": {
        "perSiteScaling": false,
        "reserved": true,
        "targetWorkerCount": 0,
        "targetWorkerSizeId": 0
      }
    }
  ],
  "outputs": {
    "storageEndpoint": {
      "type": "object",
      "value": "[reference(variables('uniqueStorageName')).primaryEndpoints]"
    }
  }
}

このテンプレートは、ストレージ アカウントと App Service プランのデプロイに使用できますが、Web サイトを追加することもできます。 事前構築済みのテンプレートを使用して、リソースのデプロイに必要な JSON をすばやく検出できます。

テンプレートの検索

  1. Azure クイック スタート テンプレートを開く

  2. タイトルが [ Deploy a basic Linux web app]\(基本的な Linux Web アプリをデプロイする\) のタイルを選択します。 見つけるのに問題がある場合は、 直接リンクを次に示します。

  3. GitHub で「ブラウズ」を選択します

  4. [azuredeploy.json] を選択します

  5. テンプレートを確認します。 Microsoft.Web/sites リソースを探します。

    Resource Manager テンプレートのクイック スタート Web サイト

既存のテンプレートを変更する

クイックスタート テンプレートを既存のテンプレートとマージします。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storagePrefix": {
      "type": "string",
      "minLength": 3,
      "maxLength": 11
    },
    "storageSKU": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_RAGRS",
        "Standard_ZRS",
        "Premium_LRS",
        "Premium_ZRS",
        "Standard_GZRS",
        "Standard_RAGZRS"
      ]
    },
    "___location": {
      "type": "string",
      "defaultValue": "[resourceGroup().___location]"
    },
    "appServicePlanName": {
      "type": "string",
      "defaultValue": "exampleplan"
    },
    "webAppName": {
      "type": "string",
      "metadata": {
        "description": "Base name of the resource such as web app name and app service plan "
      },
      "minLength": 2
    },
    "linuxFxVersion": {
      "type": "string",
      "defaultValue": "php|7.0",
      "metadata": {
        "description": "The Runtime stack of current web app"
      }
    }
  },
  "variables": {
    "uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]",
    "webAppPortalName": "[concat(parameters('webAppName'), uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2021-09-01",
      "name": "[variables('uniqueStorageName')]",
      "___location": "[parameters('___location')]",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    },
    {
      "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2021-03-01",
      "name": "[parameters('appServicePlanName')]",
      "___location": "[parameters('___location')]",
      "sku": {
        "name": "B1",
        "tier": "Basic",
        "size": "B1",
        "family": "B",
        "capacity": 1
      },
      "kind": "linux",
      "properties": {
        "perSiteScaling": false,
        "reserved": true,
        "targetWorkerCount": 0,
        "targetWorkerSizeId": 0
      }
    },
    {
      "type": "Microsoft.Web/sites",
      "apiVersion": "2021-03-01",
      "name": "[variables('webAppPortalName')]",
      "___location": "[parameters('___location')]",
      "dependsOn": [
        "[resourceId('Microsoft.Web/serverfarms', parameters('appServicePlanName'))]"
      ],
      "kind": "app",
      "properties": {
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('appServicePlanName'))]",
        "siteConfig": {
          "linuxFxVersion": "[parameters('linuxFxVersion')]"
        }
      }
    }
  ],
  "outputs": {
    "storageEndpoint": {
      "type": "object",
      "value": "[reference(variables('uniqueStorageName')).primaryEndpoints]"
    }
  }
}

Web アプリ名は、Azure 全体で一意である必要があります。 名前が重複しないように、 webAppPortalName 変数は "webAppPortalName": "[concat(parameters('webAppName'), '-webapp')]" から "webAppPortalName": "[concat(parameters('webAppName'), uniqueString(resourceGroup().id))]"に更新されます。

リソース定義をMicrosoft.Web/serverfarms定義から分離するには、Microsoft.Web/sites定義の末尾にコンマを追加します。

この新しいリソースには、注意すべき重要な機能がいくつかあります。

dependsOn という名前の要素があり、それはアプリサービスプランに設定されています。 この設定は、Web アプリを作成する前に App Service プランが存在する必要があるために必要です。 dependsOn要素は、デプロイ用のリソースを並べ替える方法を Resource Manager に指示します。

serverFarmId プロパティは resourceId 関数を使用します。 この関数は、リソースの一意の識別子を取得します。 この場合は、App Service プランの一意の識別子を取得します。 Web アプリは、1 つの特定の App Service プランに関連付けられています。

テンプレートのデプロイ

テンプレートをデプロイするには、Azure CLI または Azure PowerShell を使用します。

リソース グループを作成していない場合は、「リソース グループの 作成」を参照してください。 この例では、最初のチュートリアルに示すように、templateFile 変数をテンプレート ファイルへのパスに設定していることを前提としています。

New-AzResourceGroupDeployment `
  -Name addwebapp `
  -ResourceGroupName myResourceGroup `
  -TemplateFile $templateFile `
  -storagePrefix "store" `
  -storageSKU Standard_LRS `
  -webAppName demoapp

デプロイが失敗した場合は、 verbose スイッチを使用して、作成しているリソースに関する情報を取得します。 デバッグの詳細を取得するには、 debug スイッチを使用します。

リソースをクリーンアップする

次のチュートリアルに進む場合は、リソース グループを削除する必要はありません。

ここで停止している場合は、リソース グループを削除できます。

  1. Azure portal で、左側のメニューから [リソース グループ ] を選択します。
  2. 任意のフィールドの [フィルター] にリソース グループ名を入力します。.. テキスト フィールド。
  3. myResourceGroup の横にあるチェック ボックスをオンにし、myResourceGroup またはリソース グループ名を選択します。
  4. トップ メニューから [リソース グループの削除] を選択します。

次のステップ

テンプレート開発にクイック スタート テンプレートを使用する方法について学習しました。 次のチュートリアルでは、リソースにタグを追加します。