Azureのリソースを作るときはわりとAzure PortalからGUIをぽちぽちしながらやっています。が、AppService名を間違えるなどでリソースの作り直しが必要になることがわりとあって、その度にGUIからリソースの設定をポチポチするのが面倒なのでARMテンプレートを使おう!というのがモチベになります。 ARMテンプレートはAzureのリソースの情報をjsonで定義したもので、これがあればリソースを作るときにGUIで設定していた各種設定が不要になりリソースの作成が楽になります。
ARMテンプレートの準備
AppServiceとAppServiceプランを作るためのARMテンプレートを準備します。 今回はこちらのMSのドキュメントから拝借しました。
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "metadata": { "_generator": { "name": "bicep", "version": "0.4.1124.51302", "templateHash": "18185082675760208473" } }, "parameters": { "webAppName": { "type": "string", "defaultValue": "[format('webApp-{0}', uniqueString(resourceGroup().id))]", "minLength": 2, "metadata": { "description": "Web app name." } }, "location": { "type": "string", "defaultValue": "[resourceGroup().location]", "metadata": { "description": "Location for all resources." } }, "sku": { "type": "string", "defaultValue": "F1", "metadata": { "description": "The SKU of App Service Plan." } }, "linuxFxVersion": { "type": "string", "defaultValue": "DOTNETCORE|3.0", "metadata": { "description": "The Runtime stack of current web app" } }, "repoUrl": { "type": "string", "defaultValue": " ", "metadata": { "description": "Optional Git Repo URL" } } }, "variables": { "appServicePlanPortalName": "[format('AppServicePlan-{0}', parameters('webAppName'))]" }, "resources": [ { "type": "Microsoft.Web/serverfarms", "apiVersion": "2021-02-01", "name": "[variables('appServicePlanPortalName')]", "location": "[parameters('location')]", "sku": { "name": "[parameters('sku')]" }, "kind": "linux", "properties": { "reserved": true } }, { "type": "Microsoft.Web/sites", "apiVersion": "2021-02-01", "name": "[parameters('webAppName')]", "location": "[parameters('location')]", "properties": { "httpsOnly": true, "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanPortalName'))]", "siteConfig": { "linuxFxVersion": "[parameters('linuxFxVersion')]", "minTlsVersion": "1.2", "ftpsState": "FtpsOnly" } }, "identity": { "type": "SystemAssigned" }, "dependsOn": [ "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanPortalName'))]" ] }, { "condition": "[contains(parameters('repoUrl'), 'http')]", "type": "Microsoft.Web/sites/sourcecontrols", "apiVersion": "2021-02-01", "name": "[format('{0}/{1}', parameters('webAppName'), 'web')]", "properties": { "repoUrl": "[parameters('repoUrl')]", "branch": "master", "isManualIntegration": true }, "dependsOn": [ "[resourceId('Microsoft.Web/sites', parameters('webAppName'))]" ] } ] }
必要に応じてparameters、variablesの値を変えてカスタマイズします。とりあえずサンプルそのままでデプロイしてみます。
デプロイしてみる
Azure Portalのカスタムテンプレートのデプロイからリソースを作ってみます。事前にサブスクリプションとリソースグループは作成しておきました。
エディターで独自のテンプレートを作成を選択すると次のようなエディターが表示されるのでさきほどのARMテンプレートを使います。
次へ進むとWebAppNameやSKUなどがARMテンプレートに書かれていたものが反映されています。
これでデプロイを実行します。デプロイ完了後にリソースグループの中を見てみると、AppServiceプランとAppServiceが作られていました。