08 Azure 빠른 시작 템플릿 사용
<참조>
Azure 빠른 시작 템플릿은 커뮤니티 제공 템플릿의 리포지토리다. 템플릿 개발에 샘플 템플릿을 사용할 수 있다. 이 자습서에서는 웹 사이트 리소스 정의를 찾아서 자체 템플릿에 추가한다.
필수 조건
Resource Manager Tools 확장이 포함된 Visual Studio Code 및 Azure PowerShell 또는 Azure CLI가 있어야 한다.
준비가 안되었다면, Azure 실습을 위한 Visual Studio Code 환경 구성과 Linux용 Windows 하위 시스템 설치를 참고하여 실습 준비를 마친다.
템플릿 검토
다음은 이전 자습서에서 활용한 템플릿이다.
{ "$schema": "https://schema.management.azure.com/schemas/2015-01-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": "2019-04-01", "name": "[variables('uniqueStorageName')]", "location": "[parameters('location')]", "sku": { "name": "[parameters('storageSKU')]" }, "kind": "StorageV2", "properties": { "supportsHttpsTrafficOnly": true } }, { "type": "Microsoft.Web/serverfarms", "apiVersion": "2016-09-01", "name": "[parameters('appServicePlanName')]", "location": "[parameters('location')]", "sku": { "name": "P1v2", "tier": "PremiumV2", "size": "P1v2", "family": "Pv2", "capacity": 1 }, "kind": "linux", "properties": { "perSiteScaling": false, "maximumElasticWorkerCount": 1, "isSpot": false, "reserved": true, "isXenon": false, "hyperV": false, "targetWorkerCount": 0, "targetWorkerSizeId": 0 } } ], "outputs": { "storageEndpoint": { "type": "object", "value": "[reference(variables('uniqueStorageName')).primaryEndpoints]" } } } |
이 템플릿은 스토리지 계정 및 App Service 계획을 배포하는 데 사용되지만 여기에 웹 사이트를 추가할 수도 있다. 미리 빌드된 템플릿을 사용하여 리소스를 배포하는 데 필요한 JSON을 신속하게 검색할 수 있다.
템플릿 찾기
1, Azure 빠른 시작 템플릿을 연다.
2. 검색에 deploy linux web app(Linux 웹앱 배포)을 입력한다.
3. 제목이 Deploy a basic Linux web app(기본 Linux 웹앱 배포)인 항목을 선택한다. 찾는 데 문제가 있으면 직접 링크를 사용한다.
4. GitHub에서 찾아보기를 선택한다.
5. azuredeploy.json을 선택한다.
6. 템플릿을 검토한다. 특히, Microsoft.Web/sites 리소스를 살펴본다.
기존 템플릿 수정
빠른 시작 템플릿을 기존 템플릿과 병합한다.
{ "$schema": "https://schema.management.azure.com/schemas/2015-01-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": "2019-04-01", "name": "[variables('uniqueStorageName')]", "location": "[parameters('location')]", "sku": { "name": "[parameters('storageSKU')]" }, "kind": "StorageV2", "properties": { "supportsHttpsTrafficOnly": true } }, { "type": "Microsoft.Web/serverfarms", "apiVersion": "2016-09-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": "2018-11-01", "name": "[variables('webAppPortalName')]", "location": "[parameters('location')]", "kind": "app", "dependsOn": [ "[resourceId('Microsoft.Web/serverfarms', parameters('appServicePlanName'))]" ], "properties": { "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('appServicePlanName'))]", "siteConfig": { "linuxFxVersion": "[parameters('linuxFxVersion')]" } } } ], "outputs": { "storageEndpoint": { "type": "object", "value": "[reference(variables('uniqueStorageName')).primaryEndpoints]" } } } |
웹앱 이름은 Azure에서 고유해야 한다. 이름이 중복되지 않도록, webAppPortalName 변수가 "webAppPortalName": "[concat(parameters('webAppName'), '-webapp')]" 에서 "webAppPortalName": "[concat(parameters('webAppName'), uniqueString(resourceGroup().id))]" 으로 업데이트 되었다.
Microsoft.Web/serverfarms 정의의 끝에 쉼표를 추가하여 Microsoft.Web/sites 정의와 리소스 정의를 구분한다.
이 새 리소스에는 몇 가지 중요한 기능이 있다.
App Service 계획으로 설정되는 dependsOn이라는 요소가 있음을 알 수 있다. 웹 앱을 만들기 전에 App Service 계획이 있어야 하기 때문에 이 설정이 필요하다. dependsOn 요소는 배포를 위해 리소스를 정렬하는 방법을 Resource Manager에 알려준다.
serverFarmId 속성은 resourceId 함수를 사용한다. 이 함수는 리소스의 고유 식별자를 가져온다. 이 경우 App Service 계획의 고유 식별자를 가져온다. 웹앱은 하나의 특정 App Service 계획과 연결된다.
템플릿 배포
템플릿을 배포한다.
실습 디렉토리에서 탐색기 메뉴 중 Open with Code를 선택해 VS Code를 연다.
Ctrl + Shift + p를 누르고 화면 상단 터미널 선택 창에서 PowerShell Core를 선택하여 shell을 구동한다.
기존 powershell.ps1 이라는 파일을 이용하거나 새로 하나 생성하여 다음 코드를 붙여 넣는다.
위 함수 사용 섹션의 azuredeploy.json 코드도 기존 파일에 덮어 쓰거나 새로 하나 생성하여 붙여 넣는다.
$resourceGroup 이나 $templateFile은 각자 상황에 맞게 수정하고 -storageName의 값은 중복되지 않는 임의의 값으로 정해 입력해 준다.
참고로 이전 실습에서 생성되었던 리소스 그룹(zeroRG)은 모두 정리되었다고 가정하지만 정리가 안되었더라도 코드 실행과정에서 업데이트 여부를 물을 때 Y(기본값)를 눌러 진행하면 된다.
$location = "koreacentral" $resourceGroup = "zeroRG" New-AzResourceGroup -Name $resourceGroup -Location $location
$templateFile = "C:\AzureDevOps-Excercise\azuredeploy.json" New-AzResourceGroupDeployment ` -Name addwebapp ` -ResourceGroupName $resourceGroup ` -TemplateFile $templateFile ` -storagePrefix "store" ` -storageSKU Standard_LRS ` -webAppName deomoapp |
위 코드 전체를 선택하고 F8을 눌러 코드를 실행한다.
이상이 없다면 ProvisioningState가 Succeeded 상태로 확인 될 것이다.
PS C:\AzureDevOps-Exercise> $location="koreacentral"
PS C:\AzureDevOps-Exercise> New-AzResourceGroup -Name $resourceGroup -Location $location
ResourceGroupName : zeroRG
Location : koreacentral
ProvisioningState : Succeeded
ResourceId : /subscriptions/f8764f39-xxxx-xxxx-xxxx-77b286ed971b/resourceGroups/zeroRG
PS C:\AzureDevOps-Exercise>
PS C:\AzureDevOps-Exercise> $templateFile = "C:\AzureDevOps-Exercise\azuredeploy.json"
PS C:\AzureDevOps-Exercise>
PS C:\AzureDevOps-Exercise> New-AzResourceGroupDeployment `
>> -Name addwebapp `
PS C:\AzureDevOps-Exercise> $location="koreacentral"
PS C:\AzureDevOps-Exercise> $resourceGroup="zeroRG"
ResourceGroupName : zeroRG
Location : koreacentral
ProvisioningState : Succeeded
Tags :
ResourceId : /subscriptions/f8764f39-xxxx-xxxx-xxxx-77b286ed971b/resourceGroups/zeroRG
PS C:\AzureDevOps-Exercise>
PS C:\AzureDevOps-Exercise> $templateFile = "C:\AzureDevOps-Exercise\azuredeploy.json"
PS C:\AzureDevOps-Exercise>
PS C:\AzureDevOps-Exercise> New-AzResourceGroupDeployment `
>> -Name addwebapp `
>> -ResourceGroupName $resourceGroup `
>> -TemplateFile $templateFile `
>> -storagePrefix "store" `
>> -storageSKU Standard_LRS `
>> -webAppName deomoapp
DeploymentName : addwebapp
ResourceGroupName : zeroRG
ProvisioningState : Succeeded
Timestamp : 2019-12-08 오전 3:07:22
Mode : Incremental
TemplateLink :
Parameters :
Name Type Value
==================== ========================= ==========
storagePrefix String store
storageSKU String Standard_LRS
location String koreacentral
appServicePlanName String exampleplan
webAppName String deomoapp
linuxFxVersion String php|7.0
Outputs :
Name Type Value
================= ========================= ==========
storageEndpoint Object {
"dfs": "https://storebd5x5yefmp5vw.dfs.core.windows.net/",
"web": "https://storebd5x5yefmp5vw.z12.web.core.windows.net/",
"blob": "https://storebd5x5yefmp5vw.blob.core.windows.net/",
"queue": "https://storebd5x5yefmp5vw.queue.core.windows.net/",
"table": "https://storebd5x5yefmp5vw.table.core.windows.net/",
"file": "https://storebd5x5yefmp5vw.file.core.windows.net/"
}
DeploymentDebugLogLevel :
PS C:\AzureDevOps-Exercise>
터미널 창 우측 셀렉트 박스에 wsl이 있다면 그것을 선택한다.
없다면, Select Default Shell을 선택한다. 상단에 wsl을 선택할 수 있도록 가용한 shell 리스트가 나열될 것이며 그 중에서 WSL Bash를 선택한다.
현재 실습 디렉토리 내 azurecli.azcli라는 파일을 만들고 다음 코드를 붙여 넣는다.
각자의 상황에 맞게 resourceGroup과 templateFile 등의 값을 수정하여 저장한다.
resourceGroup="zerobigRG" az group create \ |
위 코드 전체를 선택하고 Ctrl + '를 눌러 코드를 실행한다.
zerobig@ZEROBIG-NT800:/mnt/c/AzureDevOps-Exercise$ resourceGroup="zerobigRG"
zerobig@ZEROBIG-NT800:/mnt/c/AzureDevOps-Exercise$ location="koreacentral"
zerobig@ZEROBIG-NT800:/mnt/c/AzureDevOps-Exercise$
zerobig@ZEROBIG-NT800:/mnt/c/AzureDevOps-Exercise$ az group create \
> --name $resourceGroup \
> --location $location
{
"id": "/subscriptions/f8764f39-xxxx-xxxx-xxxx-77b286ed971b/resourceGroups/zerobigRG",
"location": "koreacentral",
"managedBy": null,
"name": "zerobigRG",
"properties": {
"provisioningState": "Succeeded"
},
"tags": null,
"type": "Microsoft.Resources/resourceGroups"
}
zerobig@ZEROBIG-NT800:/mnt/c/AzureDevOps-Exercise$
zerobig@ZEROBIG-NT800:/mnt/c/AzureDevOps-Exercise$
zerobig@ZEROBIG-NT800:/mnt/c/AzureDevOps-Exercise$ templateFile="/mnt/c/AzureDevOps-Exercise/azuredeploy.json"
zerobig@ZEROBIG-NT800:/mnt/c/AzureDevOps-Exercise$
zerobig@ZEROBIG-NT800:/mnt/c/AzureDevOps-Exercise$
zerobig@ZEROBIG-NT800:/mnt/c/AzureDevOps-Exercise$ az group deployment create \
> --name addwebapp \
> --resource-group $resourceGroup \
> --template-file $templateFile \
> --parameters storagePrefix=store storageSKU=Standard_LRS webAppName=demoapp
{
"id": "/subscriptions/f8764f39-xxxx-xxxx-xxxx-77b286ed971b/resourceGroups/zerobigRG/providers/Microsoft.Resources/deployments/addwebapp",
"location": null,
"name": "addwebapp",
"properties": {
"correlationId": "d29668fc-xxxx-xxxx-xxxx-7e7827aa08e0",
"debugSetting": null,
"dependencies": [
{
"dependsOn": [
{
"id": "/subscriptions/f8764f39-xxxx-xxxx-xxxx-77b286ed971b/resourceGroups/zerobigRG/providers/Microsoft.Web/serverfarms/exampleplan",
"resourceGroup": "zerobigRG",
"resourceName": "exampleplan",
"resourceType": "Microsoft.Web/serverfarms"
}
],
"id": "/subscriptions/f8764f39-xxxx-xxxx-xxxx-77b286ed971b/resourceGroups/zerobigRG/providers/Microsoft.Web/sites/demoapp3mvt3ndzmlq4o",
"resourceGroup": "zerobigRG",
"resourceName": "demoapp3mvt3ndzmlq4o",
"resourceType": "Microsoft.Web/sites"
}
],
"duration": "PT30.087654S",
"mode": "Incremental",
"onErrorDeployment": null,
"outputResources": [
{
"id": "/subscriptions/f8764f39-xxxx-xxxx-xxxx-77b286ed971b/resourceGroups/zerobigRG/providers/Microsoft.Storage/storageAccounts/store3mvt3ndzmlq4o",
"resourceGroup": "zerobigRG"
},
{
"id": "/subscriptions/f8764f39-xxxx-xxxx-xxxx-77b286ed971b/resourceGroups/zerobigRG/providers/Microsoft.Web/serverfarms/exampleplan",
"resourceGroup": "zerobigRG"
},
{
"id": "/subscriptions/f8764f39-xxxx-xxxx-xxxx-77b286ed971b/resourceGroups/zerobigRG/providers/Microsoft.Web/sites/demoapp3mvt3ndzmlq4o",
"resourceGroup": "zerobigRG"
}
],
"outputs": {
"storageEndpoint": {
"type": "Object",
"value": {
"blob": "https://store3mvt3ndzmlq4o.blob.core.windows.net/",
"dfs": "https://store3mvt3ndzmlq4o.dfs.core.windows.net/",
"file": "https://store3mvt3ndzmlq4o.file.core.windows.net/",
"queue": "https://store3mvt3ndzmlq4o.queue.core.windows.net/",
"table": "https://store3mvt3ndzmlq4o.table.core.windows.net/",
"web": "https://store3mvt3ndzmlq4o.z12.web.core.windows.net/"
}
}
},
"parameters": {
"appServicePlanName": {
"type": "String",
"value": "exampleplan"
},
"linuxFxVersion": {
"type": "String",
"value": "php|7.0"
},
"location": {
"type": "String",
"value": "koreacentral"
},
"storagePrefix": {
"type": "String",
"value": "store"
},
"storageSKU": {
"type": "String",
"value": "Standard_LRS"
},
"webAppName": {
"type": "String",
"value": "demoapp"
}
},
"parametersLink": null,
"providers": [
{
"id": null,
"namespace": "Microsoft.Storage",
"registrationPolicy": null,
"registrationState": null,
"resourceTypes": [
{
"aliases": null,
"apiVersions": null,
"capabilities": null,
"locations": [
"koreacentral"
],
"properties": null,
"resourceType": "storageAccounts"
}
]
},
{
"id": null,
"namespace": "Microsoft.Web",
"registrationPolicy": null,
"registrationState": null,
"resourceTypes": [
{
"aliases": null,
"apiVersions": null,
"capabilities": null,
"locations": [
"koreacentral"
],
"properties": null,
"resourceType": "serverfarms"
},
{
"aliases": null,
"apiVersions": null,
"capabilities": null,
"locations": [
"koreacentral"
],
"properties": null,
"resourceType": "sites"
}
]
}
],
"provisioningState": "Succeeded",
"template": null,
"templateHash": "17092253188324534547",
"templateLink": null,
"timestamp": "2019-12-08T03:10:40.969655+00:00"
},
"resourceGroup": "zerobigRG",
"type": "Microsoft.Resources/deployments"
}
zerobig@ZEROBIG-NT800:/mnt/c/AzureDevOps-Exercise$
배포 확인
Azure Portal에서 리소스 그룹을 탐색하여 배포를 확인할 수 있다.
- Azure Portal에 로그인한다.
- 왼쪽 메뉴에서 리소스 그룹을 선택한다.
- 배포한 리소스 그룹을 선택한다.
- 리소스 그룹에 스토리지 계정과 App Service 계획이 포함되어 있는 것을 확인할 수 있다.
리소스 정리
다음 자습서로 이동하는 경우에는 리소스 그룹을 삭제할 필요가 없다.
지금 중지하는 경우에는 리소스 그룹을 삭제하여 배포된 리소스를 정리하는 것이 좋다.
- Azure Portal의 왼쪽 메뉴에서 리소스 그룹을 선택한다.
- 이름으로 필터링 필드에서 리소스 그룹 이름을 입력한다.
- 해당 리소스 그룹 이름을 선택한다.
- 위쪽 메뉴에서 리소스 그룹 삭제를 선택한다.