CLI를 사용하여 스냅샷에서 가상 머신 만들기

이 스크립트는 OS 디스크의 스냅샷에서 가상 머신을 만듭니다.

이 샘플을 실행하려면 최신 버전의 Azure CLI를 설치합니다. 시작하려면 az login을 실행하여 Azure와 연결합니다.

Azure CLI 샘플은 bash 셸용으로 작성됩니다. Windows PowerShell 또는 명령 프롬프트에서 이 샘플을 실행하려면 스크립트의 요소를 변경해야 할 수도 있습니다.

Azure 구독이 아직 없는 경우 시작하기 전에 체험 계정을 만듭니다.

샘플 스크립트

# Verified per Raman Kumar as of 2/23/2022

# <FullScript>
#Provide the subscription Id of the subscription where you want to create Managed Disks
subscriptionId="<subscriptionId>"

#Provide the name of your resource group
resourceGroupName=myResourceGroupName

#Provide the name of the snapshot that will be used to create Managed Disks
snapshotName=mySnapshotName

#Provide the name of the Managed Disk
osDiskName=myOSDiskName

#Provide the size of the disks in GB. It should be greater than the VHD file size.
diskSize=128

#Provide the storage type for Managed Disk. Premium_LRS or Standard_LRS.
storageType=Premium_LRS

#Provide the OS type
osType=linux

#Provide the name of the virtual machine
virtualMachineName=myVirtualMachineName


#Set the context to the subscription Id where Managed Disk will be created
az account set --subscription $subscriptionId

#Get the snapshot Id 
snapshotId=$(az snapshot show --name $snapshotName --resource-group $resourceGroupName --query [id] -o tsv)

#Create a new Managed Disks using the snapshot Id
az disk create --resource-group $resourceGroupName --name $osDiskName --sku $storageType --size-gb $diskSize --source $snapshotId 

#Create VM by attaching created managed disks as OS
az vm create --name $virtualMachineName --resource-group $resourceGroupName --attach-os-disk $osDiskName --os-type $osType
# </FullScript>

배포 정리

다음 명령을 실행하여 리소스 그룹, VM 및 모든 관련된 리소스를 제거할 수 있습니다.

az group delete --name myResourceGroup

스크립트 설명

이 스크립트는 다음 명령을 사용하여 관리 디스크, 가상 컴퓨터 및 모든 관련된 리소스를 만듭니다. 테이블에 있는 각 명령은 명령에 해당하는 문서에 연결됩니다.

명령 메모
az snapshot show 스냅샷 이름 및 리소스 그룹 이름을 사용하여 스냅샷을 가져옵니다. 반환된 개체의 Id 속성은 관리 디스크를 만드는 데 사용됩니다.
az disk create 스냅샷 Id, 디스크 이름, 스토리지 유형 및 크기를 사용하여 스냅샷에서 관리 디스크를 만듭니다.
az vm create 관리 OS 디스크를 사용하여 VM을 만듭니다.

다음 단계

Azure CLI에 대한 자세한 내용은 Azure CLI 설명서를 참조하세요.

추가 가상 머신 CLI 스크립트 샘플은 Azure Linux VM 설명서에서 확인할 수 있습니다.