티스토리 뷰
<참조>
이 자습서에서는 템플릿의 리소스에 태그를 추가하는 방법에 대해 알아봅니다. 태그를 통해 리소스를 논리적으로 구성할 수 있다.
필수 조건
Resource Manager Tools 확장이 포함된 Visual Studio Code 및 Azure PowerShell 또는 Azure CLI가 있어야 한다.
준비가 안되었다면, Azure 실습을 위한 Visual Studio Code 환경 구성과 Linux용 Windows 하위 시스템 설치를 참고하여 실습 준비를 마친다.
템플릿 검토
이전 템플릿에서는 스토리지 계정, App Service 계획 및 웹앱을 배포했다.
{ "$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]" } } } |
이러한 리소스를 배포한 후에는 비용을 추적하고 범주에 속하는 리소스를 찾아야 할 수도 있다. 이러한 문제를 해결하는 데 도움이 되는 태그를 추가할 수 있다.
기존 템플릿 수정
리소스에 태그를 지정하여 사용 여부를 식별하는 데 도움이 되는 값을 추가한다. 예를 들어 환경 및 프로젝트를 나열하는 태그를 추가할 수 있다. 비용 센터 또는 리소스를 소유하는 팀을 식별하는 태그를 추가할 수 있다. 조직에 적합한 값을 추가한다.
다음 예제에서는 템플릿의 변경 내용을 강조 표시한다. 전체 파일을 복사하고 템플릿을 해당 콘텐츠로 바꾼다.
{ "$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" } }, "resourceTags": { "type": "object", "defaultValue": { "Environment": "Dev", "Project": "Tutorial" } } }, "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')]", "tags": "[parameters('resourceTags')]", "sku": { "name": "[parameters('storageSKU')]" }, "kind": "StorageV2", "properties": { "supportsHttpsTrafficOnly": true } }, { "type": "Microsoft.Web/serverfarms", "apiVersion": "2016-09-01", "name": "[parameters('appServicePlanName')]", "location": "[parameters('location')]", "tags": "[parameters('resourceTags')]", "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": "2016-08-01", "kind": "app", "name": "[variables('webAppPortalName')]", "location": "[parameters('location')]", "tags": "[parameters('resourceTags')]", "properties": { "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('appServicePlanName'))]", "siteConfig": { "linuxFxVersion": "[parameters('linuxFxVersion')]" } }, "dependsOn": [ "[parameters('appServicePlanName')]" ] } ], "outputs": { "storageEndpoint": { "type": "object", "value": "[reference(variables('uniqueStorageName')).primaryEndpoints]" } } } |
템플릿 배포
템플릿을 배포한다.
실습 디렉토리에서 탐색기 메뉴 중 Open with Code를 선택해 VS Code를 연다.
Ctrl + Shift + p를 누르고 화면 상단 터미널 선택 창에서 PowerShell Core를 선택하여 shell을 구동한다.
기존 powershell.ps1 이라는 파일을 이용하거나 새로 하나 생성하여 다음 코드를 붙여 넣는다.
위 함수 사용 섹션의 azuredeploy.json 코드도 기존 파일에 덮어 쓰거나 새로 하나 생성하여 붙여 넣는다.
$resourceGroup 이나 $templateFile은 각자 상황에 맞게 수정하고 -storageName의 값은 중복되지 않는 임의의 값으로 정해 입력해 준다.
참고로 이전 실습에서 생성되었던 리소스 그룹(zeroRG)은 모두 정리되었다고 가정하지만 정리가 안되었더라도 코드 실행과정에서 업데이트 여부를 물을 때 Y(기본값)를 눌러 진행하면 된다.
$location = "koreacentral" |
위 코드 전체를 선택하고 F8을 눌러 코드를 실행한다.
이상이 없다면 ProvisioningState가 Succeeded 상태로 확인 될 것이다.
PS C:\AzureDevOps-Exercise> $location="koreacentral"
PS C:\AzureDevOps-Exercise> $resourceGroup="zeroRG"
PS C:\AzureDevOps-Exercise> New-AzResourceGroup -Name $resourceGroup -Location $location
ResourceGroupName : zeroRG
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 addtags `
>> -ResourceGroupName $resourceGroup `
>> -TemplateFile $templateFile `
>> -storagePrefix "store" `
>> -storageSKU Standard_LRS `
>> -webAppName deomoapp
DeploymentName : addtags
ResourceGroupName : zeroRG
ProvisioningState : Succeeded
Timestamp : 2019-12-11 오후 10:41:17
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
resourceTags Object {
"Environment": "Dev",
"Project": "Tutorial"
}
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" location="koreacentral" az group create \ --resource-group $resourceGroup \ --location $location templateFile="/mnt/c/AzureDevOps-Excercise/azuredeploy.json" az group deployment create \ --name addtags \ --resource-group $resourceGroup \ --template-file $templateFile \ --parameters storagePrefix=store storageSKU=Standard_LRS webAppName=demoapp |
위 코드 전체를 선택하고 Ctrl + '를 눌러 코드를 실행한다.
zerobig@ZEROBIG-NT800:/mnt/c/AzureDevOps-Exercise$ templateFile="/mnt/c/AzureDevOps-Exercise/azuredeploy.json"
zerobig@ZEROBIG-NT800:/mnt/c/AzureDevOps-Exercise$ templateParameterFile="/mnt/c/AzureDevOps-Exercise/azuredeploy.parameters.json"
zerobig@ZEROBIG-NT800:/mnt/c/AzureDevOps-Exercise$
zerobig@ZEROBIG-NT800:/mnt/c/AzureDevOps-Exercise$ az group deployment create \
> --name addtags \
> --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/addtags",
"location": null,
"name": "addtags",
"properties": {
"correlationId": "602b52fd-xxxx-xxxx-xxxx-00835df9380e",
"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.6380739S",
"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"
},
"resourceTags": {
"type": "Object",
"value": {
"Environment": "Dev",
"Project": "Tutorial"
}
},
"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": "18198432259854125308",
"templateLink": null,
"timestamp": "2019-12-11T22:33:56.756838+00:00"
},
"resourceGroup": "zerobigRG",
"type": "Microsoft.Resources/deployments"
}
zerobig@ZEROBIG-NT800:/mnt/c/AzureDevOps-Exercise$
배포 확인
Azure Portal에서 리소스 그룹을 탐색하여 배포를 확인할 수 있다.
- Azure Portal에 로그인한다.
- 왼쪽 메뉴에서 리소스 그룹을 선택한다.
- 배포한 리소스 그룹을 선택한다.
- 스토리지 계정 리소스와 같은 리소스 중 하나를 선택한다. 이제 태그가 있음을 볼 수 있다.
리소스 정리
다음 자습서로 이동하는 경우에는 리소스 그룹을 삭제할 필요가 없다.
지금 중지하는 경우에는 리소스 그룹을 삭제하여 배포된 리소스를 정리하는 것이 좋다.
- Azure Portal의 왼쪽 메뉴에서 리소스 그룹을 선택한다.
- 이름으로 필터링 필드에서 리소스 그룹 이름을 입력한다.
- 해당 리소스 그룹 이름을 선택한다.
- 위쪽 메뉴에서 리소스 그룹 삭제를 선택한다.
'Azure와 함께 하는 DevOps' 카테고리의 다른 글
Azure DevOps 계정 만들기 (0) | 2019.12.22 |
---|---|
10 매개 변수 파일을 사용하여 Resource Manager 템플릿 배포 (0) | 2019.12.16 |
08 Azure 빠른 시작 템플릿 사용 (0) | 2019.12.09 |
07 Azure Portal에서 내보낸 템플릿 사용 (0) | 2019.12.05 |
06 Resource Manager 템플릿에 변수와 출력 추가 (0) | 2019.12.02 |