연습 - 변환된 템플릿 테스트 및 배포
토이 트럭 서비스를 실행하는 가상 머신을 나타내는 Bicep 파일을 만들었습니다. 파이프라인에서 파일을 사용하기 전에 파일이 현재 Azure 환경을 정확하게 나타내는지 확인해야 합니다. 이 모듈에서는 Bicep 파일을 테스트한 다음 기존 Azure 리소스에 배포합니다.
프로세스 중에 다음 작업을 수행합니다.
- 가상 명령을 실행하여 배포의 영향을 확인합니다.
- 템플릿을 배포하고 배포 상태를 확인합니다.
가상 실행
새 Bicep 파일을 배포하기 전에 먼저 what-if 명령을 실행합니다. 이 명령은 Bicep 파일이 유효한지 확인합니다. 또한 이 명령은 파일을 배포할 때 Azure 환경에서 발생하는 변경 내용 목록을 제공합니다.
참고 항목
실제 마이그레이션에서는 각 환경에 적절한 매개 변수 세트를 사용하여 프로덕션 환경과 비 프로덕션 환경에 대해 가상 명령을 실행해야 합니다. 이렇게 하면 발견되지 않은 구성의 차이를 감지할 수 있습니다. 이 예제에서는 환경을 사용하므로 해당 환경에 대해서만 가상 작업을 실행합니다.
주의
가상 명령 및 후속 템플릿 배포에 사용하는 배포 모드는 위험을 초래할 수 있습니다. 전체 모드는 템플릿에 지정되지 않은 리소스 그룹에 정의된 모든 리소스를 삭제합니다. 이것은 종종 Bicep 코드가 완전한지 확인하는 데 도움이 되므로 좋은 습관입니다. 그러나 이 옵션은 마이그레이션에서 리소스가 누락되었을 수 있으므로 약간의 위험이 따릅니다.
이 연습에서는 누락되었을 수 있는 리소스를 감지하도록 전체 모드를 사용하여 가상 명령을 실행합니다. 그런 다음 증분 모드에서 배포를 실행합니다.
Visual Studio Code 터미널에서 다음 명령을 실행합니다.
az deployment group what-if \ --mode Complete \ --resource-group ToyTruck \ --template-file main.bicep \ --parameters main.parameters.production.json
메시지가 표시되면
virtualMachineAdminPassword
매개 변수 값에 대한 보안 암호를 입력합니다.
Visual Studio Code 터미널에서 다음 명령을 실행합니다.
New-AzResourceGroupDeployment ` -WhatIf ` -Mode Complete ` -ResourceGroupName ToyTruck ` -TemplateFile main.bicep ` -TemplateParameterFile main.parameters.production.json
메시지가 표시되면
virtualMachineAdminPassword
매개 변수 값에 대한 보안 암호를 입력합니다.
가상 출력 검토
다음과 같은 가상 출력을 검토합니다.
Note: The result may contain false positive predictions (noise).
You can help us improve the accuracy of the result by opening an issue here: https://aka.ms/WhatIfIssues.
Resource and property changes are indicated with these symbols:
- Delete
~ Modify
x NoEffect
= NoChange
The deployment will update the following scope:
Scope: /subscriptions/f0750bbe-ea75-4ae5-b24d-a92ca601da2c/resourceGroups/TOYTRUCK
- Microsoft.Compute/disks/ToyTruckServer_disk1_23e6a144c4ea4049b3e2be24b78a9e81
id: "/subscriptions/f0750bbe-ea75-4ae5-b24d-a92ca601da2c/resourceGroups/TOYTRUCK/providers/Microsoft.Compute/disks/ToyTruckServer_disk1_23e6a144c4ea4049b3e2be24b78a9e81"
location: "westus3"
managedBy: "/subscriptions/f0750bbe-ea75-4ae5-b24d-a92ca601da2c/resourceGroups/ToyTruck/providers/Microsoft.Compute/virtualMachines/ToyTruckServer"
name: "ToyTruckServer_disk1_23e6a144c4ea4049b3e2be24b78a9e81"
sku.name: "Premium_LRS"
sku.tier: "Premium"
type: "Microsoft.Compute/disks"
~ Microsoft.Network/networkInterfaces/toytruckserver123 [2022-05-01]
- kind: "Regular"
- properties.allowPort25Out: true
~ properties.ipConfigurations: [
~ 0:
- properties.privateIPAddress: "10.0.0.4"
- properties.publicIPAddress.properties.deleteOption: "Detach"
- properties.publicIPAddress.sku:
name: "Basic"
tier: "Regional"
]
x properties.ipConfigurations[0].properties.primary: true
= Microsoft.Compute/virtualMachines/ToyTruckServer [2022-08-01]
x properties.storageProfile.osDisk.managedDisk.storageAccountType: "Premium_LRS"
= Microsoft.Network/networkSecurityGroups/ToyTruckServer-nsg [2022-05-01]
= Microsoft.Network/publicIPAddresses/ToyTruckServer-ip [2022-05-01]
= Microsoft.Network/virtualNetworks/ToyTruck-vnet [2022-05-01]
Resource changes: 1 to delete, 1 to modify, 4 no change.
출력에는 세 가지 중요한 정보가 포함되어 있습니다. 각 항목을 검토해 보겠습니다.
가상 명령은 관리 디스크가 삭제될 것임을 감지합니다. 이 출력은 정확하지 않습니다. 관리 디스크는 가상 머신을 만들 때 자동으로 만들어집니다. 리소스 목록에 나타나는 관리 디스크를 삭제하더라도 가상 머신은 이를 삭제하지 않습니다. 그러나 신중한 접근 방식을 채택하는 것이 항상 권장되므로 다음 단계에서는 실제 배포를 증분 모드로 실행하여 문제가 발생할 위험을 완화합니다.
what-if 명령은 리소스의
networkInterface
속성이privateIPAddress
제거되었음을 감지합니다. 이 속성은 의도적으로 제거되었으므로 괜찮습니다. 속성이privateIPAllocationMethod
설정Dynamic
되었으므로 속성이 변경되더라도 속성을 제거privateIPAddress
해도 아무런 영향을 주지 않습니다.what-if 명령은 리소스의
networkInterface
두 속성이publicIPAddress
삭제되는 것을 감지합니다. 이러한 속성을 추가하여 문제를 해결합니다.
템플릿 업데이트
기본.bicep에서 다음과 같은 속성을 포함하도록 리소스를 deleteOption
publicIPAddress
sku
업데이트 networkInterface
합니다.
resource networkInterface 'Microsoft.Network/networkInterfaces@2024-05-01' = {
name: networkInterfaceName
location: location
properties: {
ipConfigurations: [
{
name: 'ipconfig1'
properties: {
privateIPAllocationMethod: 'Dynamic'
publicIPAddress: {
id: publicIPAddress.id
properties: {
deleteOption: 'Detach'
}
sku: {
name: 'Basic'
tier: 'Regional'
}
}
subnet: {
id: virtualNetwork::defaultSubnet.id
}
primary: true
privateIPAddressVersion: 'IPv4'
}
}
]
enableAcceleratedNetworking: true
enableIPForwarding: false
disableTcpStateTracking: false
networkSecurityGroup: {
id: networkSecurityGroup.id
}
nicType: 'Standard'
}
}
what-if 명령을 다시 실행합니다.
Visual Studio Code 터미널에서 다음 명령을 실행합니다.
az deployment group what-if \ --mode Complete \ --resource-group ToyTruck \ --template-file main.bicep \ --parameters main.parameters.production.json
메시지가 표시되면
virtualMachineAdminPassword
매개 변수 값에 대한 보안 암호를 입력합니다.작업이 완료되면 출력을 검토합니다. 출력은 다음 샘플과 같습니다. 및
sku
속성은deleteOption
출력에 표시되지 않습니다.Note: The result may contain false positive predictions (noise). You can help us improve the accuracy of the result by opening an issue here: https://aka.ms/WhatIfIssues. Resource and property changes are indicated with these symbols: - Delete ~ Modify x NoEffect = NoChange The deployment will update the following scope: Scope: /subscriptions/f0750bbe-ea75-4ae5-b24d-a92ca601da2c/resourceGroups/TOYTRUCK - Microsoft.Compute/disks/ToyTruckServer_disk1_23e6a144c4ea4049b3e2be24b78a9e81 id: "/subscriptions/f0750bbe-ea75-4ae5-b24d-a92ca601da2c/resourceGroups/TOYTRUCK/providers/Microsoft.Compute/disks/ToyTruckServer_disk1_23e6a144c4ea4049b3e2be24b78a9e81" location: "westus3" managedBy: "/subscriptions/f0750bbe-ea75-4ae5-b24d-a92ca601da2c/resourceGroups/ToyTruck/providers/Microsoft.Compute/virtualMachines/ToyTruckServer" name: "ToyTruckServer_disk1_23e6a144c4ea4049b3e2be24b78a9e81" sku.name: "Premium_LRS" sku.tier: "Premium" type: "Microsoft.Compute/disks" ~ Microsoft.Network/networkInterfaces/toytruckserver123 [2022-05-01] - kind: "Regular" - properties.allowPort25Out: true ~ properties.ipConfigurations: [ ~ 0: - properties.privateIPAddress: "10.0.0.4" ] x properties.ipConfigurations[0].properties.primary: true = Microsoft.Compute/virtualMachines/ToyTruckServer [2022-08-01] x properties.storageProfile.osDisk.managedDisk.storageAccountType: "Premium_LRS" = Microsoft.Network/networkSecurityGroups/ToyTruckServer-nsg [2022-05-01] = Microsoft.Network/publicIPAddresses/ToyTruckServer-ip [2022-05-01] = Microsoft.Network/virtualNetworks/ToyTruck-vnet [2022-05-01] Resource changes: 1 to delete, 1 to modify, 4 no change.
Visual Studio Code 터미널에서 다음 명령을 실행합니다.
New-AzResourceGroupDeployment ` -WhatIf ` -Mode Complete ` -ResourceGroupName ToyTruck ` -TemplateFile main.bicep ` -TemplateParameterFile main.parameters.production.json
메시지가 표시되면
virtualMachineAdminPassword
매개 변수 값에 대한 보안 암호를 입력합니다.작업이 완료되면 출력을 검토합니다. 출력은 다음 샘플과 같습니다. 및
sku
속성은deleteOption
출력에 표시되지 않습니다.Note: The result may contain false positive predictions (noise). You can help us improve the accuracy of the result by opening an issue here: https://aka.ms/WhatIfIssues. Resource and property changes are indicated with these symbols: - Delete ~ Modify x NoEffect = NoChange The deployment will update the following scope: Scope: /subscriptions/f0750bbe-ea75-4ae5-b24d-a92ca601da2c/resourceGroups/TOYTRUCK - Microsoft.Compute/disks/ToyTruckServer_disk1_23e6a144c4ea4049b3e2be24b78a9e81 id: "/subscriptions/f0750bbe-ea75-4ae5-b24d-a92ca601da2c/resourceGroups/TOYTRUCK/providers/Microsoft.Compute/disks/ToyTruckServer_disk1_23e6a144c4ea4049b3e2be24b78a9e81" location: "westus3" managedBy: "/subscriptions/f0750bbe-ea75-4ae5-b24d-a92ca601da2c/resourceGroups/ToyTruck/providers/Microsoft.Compute/virtualMachines/ToyTruckServer" name: "ToyTruckServer_disk1_23e6a144c4ea4049b3e2be24b78a9e81" sku.name: "Premium_LRS" sku.tier: "Premium" type: "Microsoft.Compute/disks" ~ Microsoft.Network/networkInterfaces/toytruckserver123 [2022-05-01] - kind: "Regular" - properties.allowPort25Out: true ~ properties.ipConfigurations: [ ~ 0: - properties.privateIPAddress: "10.0.0.4" ] x properties.ipConfigurations[0].properties.primary: true = Microsoft.Compute/virtualMachines/ToyTruckServer [2022-08-01] x properties.storageProfile.osDisk.managedDisk.storageAccountType: "Premium_LRS" = Microsoft.Network/networkSecurityGroups/ToyTruckServer-nsg [2022-05-01] = Microsoft.Network/publicIPAddresses/ToyTruckServer-ip [2022-05-01] = Microsoft.Network/virtualNetworks/ToyTruck-vnet [2022-05-01] Resource changes: 1 to delete, 1 to modify, 4 no change.
템플릿 배포
Bicep 파일이 유효하다는 것을 알고 있으며, what-if 작업은 템플릿이 예상한 결과를 제공한다는 것을 나타냅니다. 이제 템플릿을 배포할 준비가 되었습니다. 이 단계가 성공하면 효과가 나타나지 않습니다.
Visual Studio Code 터미널에서 다음 명령을 실행합니다.
az deployment group create \ --resource-group ToyTruck \ --template-file main.bicep \ --parameters main.parameters.production.json
메시지가 표시되면
virtualMachineAdminPassword
매개 변수 값에 대한 보안 암호를 입력합니다.몇 초 내에 배포가 성공적으로 완료됩니다.
Azure Portal에서 리소스 그룹을 엽니다. 배포 목록을 보려면 성공 2를 선택합니다.
배포 성공:
Visual Studio Code 터미널에서 다음 명령을 실행합니다.
New-AzResourceGroupDeployment ` -ResourceGroupName ToyTruck ` -TemplateFile main.bicep ` -TemplateParameterFile main.parameters.production.json
메시지가 표시되면
virtualMachineAdminPassword
매개 변수 값에 대한 보안 암호를 입력합니다.몇 초 내에 배포가 성공적으로 완료됩니다.
Azure Portal에서 리소스 그룹을 엽니다. 2 성공 링크를 선택하여 배포 목록을 봅니다.
배포 성공:
팁
실제 마이그레이션에서는 리소스가 여전히 올바르게 작동하는지 확인하기 위해 스모크 테스트도 실행해야 합니다. 스모크 테스트는 의도하지 않은 변경을 수행하지 않았는지 확인하기 위한 최종 검사입니다.
리소스 정리
연습을 완료했으므로 리소스에 대한 요금이 청구되지 않도록 리소스를 제거할 수 있습니다.
Visual Studio Code 터미널에서 다음 명령을 실행합니다.
az group delete --resource-group ToyTruck --yes --no-wait
리소스 그룹 및 모든 해당 리소스는 백그라운드에서 삭제됩니다.
Remove-AzResourceGroup -Name ToyTruck -Force