creating vm from snapshot when the snapshot is in another resource group - azure

I have created vm from snapshot using azure cli command
Below is the script for the creation of vm from snapshot
az disk create -g $RD_OPTION_RESOURCEGROUP -n $RD_OPTION_DISKNAME --source $RD_OPTION_SNAPSHOTNAME
az vm create -g $RD_OPTION_RESOURCEGROUP -n $RD_OPTION_VMNAME --attach-os-disk $RD_OPTION_DISKNAME --os-type windows
This code will create the vm from snapshot in which snapshot is in one resource group say abc then the newly creating vm should also in same resource group abc
now i need to find the way like i want to create a vm from snapshot like the snapshot and newly creating vm will be in different resource groups... like if snapshot is in abc resource group then the newly creating vm have to be allocated in the other resource group say xyz
Please help me out in this

To create a VM from Snapshot which is in different resource group, copy the snapshot using below CLI script:
Define a variable as sourceSubscriptionId where you have to provide the subscription ID in which your snapshot resides.
sourceSubscriptionId="<subscriptionId>"
Define a variable as sourceResourceGroupName where you have to provide the resource group name in which your snapshot resides.
sourceResourceGroupName=yourSourceResourceGroupName
Define a variable where you have to provide snapshot name.
snapshotName=mySnapshotName
Set the context to the subscription Id where snapshot exists.
az account set --subscription $sourceSubscriptionId
Use the below cmdlet to get the snapshot ID.
snapshotId=$(az snapshot show --name $snapshotName --resource-group $sourceResourceGroupName --query [id] -o tsv)
Define a variable targetResourceGroupName where snapshot will be copied to:
targetResourceGroupName=mytargetResourceGroupName
To copy the snapshot to different resource group try using the below cmdlet:
az snapshot create --resource-group $targetResourceGroupName --name $snapshotName --source $snapshotId --sku Standard_LRS
Reference : azure-cli-samples/copy-snapshot-to-same-or-different-subscription.sh at master · Azure-Samples/azure-cli-samples · GitHub.

Related

Possibility to create and delete databricks resources via Azure CLI deployments

Normally when you create resources using ARM templates and the azure CLI you can create using:
az deployment group create --resource-group $resourceGroupName --template-file "infra/template.json" --parameters "infra/parameters.json"
Then when I want to delete the resources I can:
az group delete --name $resourceGroupName
However, with Databricks this will create other resource groups as well. Even if you delete the DB resource group, you've still got these other lingering resource groups. I'd like to be able to delete the databricks AND all of the other resources that were created, without having to do extra manual steps.
Any idea of a clean way to do this?
The managed resource group created by Databricks cannot be deleted manually since it was created by the Databricks resource itself. The deny assignment prevents deletion of the managed resource group.
One way to remove resource is to delete the existing workspace following is the example using azure cli
Through AZURE CLI
I have created resource group tvs and databricks tvs for the demo purpose
Delete resource
Use following command in AzureCLI to delete a resource.
Azure CLICopy
az resource delete \
--resource-group tvs\
--name tvs\
--ids *****\
(NOTE : ids is the id of the resource that can be picked from JSON view)
Before deletion
After deletion
Delete resource group
Use following command in AzureCLI to delete the resource group.
Azure CLICopy
az group delete --name tvs
Azure doesn’t currently provide a way to delete multiple resource groups at the same time.
Here’s a method that works for me.
Open Azure Portal
Click on Resource Groups
Select the Resource Groups that you want to delete
Click “Assign tags”
Assign a new tag called “disposable-service”(can be named anything) and tag value to be true
Open Azure Cloud Shell https://shell.azure.com or click on the Azure Shell icon in the Azure Portal toolbar.
the following script into Cloud Shell and hit enter.
az group list --tag disposable-service=true --query "[].[id]" --output tsv

az vmss list command returns empty value

I have two resource groups in Azure, each contains one VMSS (Virtual Machine Scale Sets) and the resources are visible in Azure Portal. The problem is, the following commands return empty output:
az resource list --subscription MySubscription -g vmss-rg
az vmss list --subscription MySubscription -g vmss-rg
az vmss list
However, running az resource list with different resource groups which doesn't have VMSS works fine. I also tested it with different Azure account and subscription, it also worked.
We have tested the same command in our local as suggested by #VenkateshDodda-MT and it works fine. Posting it as an answer to help other community members for the similar issue so that they can find and fix their problem.
To achieve the above requirement we need to install the az powershell module in our local .
Post that run the command in powershell to get the vmss list under our subscription or resource groups:-
az resource list --resource-type "Microsoft.Compute/virtualMachineScaleSets"
OUTPUT:-
To get the VMSS list under particular resource group run the below cmd:
az resource list --resource-type "Microsoft.Compute/virtualMachineScaleSets" -g '<rgName>'
OUTPUT:-
For more information please refer this SO THREAD:Azure PS command returns empty list as suggested by #Olga Osinskaya - MSFT

Failed to delete subnet

I created a new Azure container instance (ACI) with a new vnet and subnet by Azure CLI
I deleted ACI from Azure portal and now Im trying to delete subnet but gives me the following errors:
Failed to delete subnet 'SubnetNAme'. Error: Subnet SubnetNAme is in use by aci-network-profile-VNETNAME-SubnetNAme/eth0/ipconfigprofile and cannot be deleted. In order to delete the subnet, delete all the resources within the subnet. See aka.ms/deletesubnet.
If I tried to access aci-network-profile-VNETNAME-SubnetNAme/eth0/ipconfigprofile, it tells me that doesnt exist this resource:
Details
The resource was not found, it may have been deleted. If this was launched from a pinned tile on the dashboard, it should be removed.
For your issue, instead of finding the aci-network-profile-VNETNAME-SubnetNAme/eth0/ipconfigprofile in the portal, you need to delete the Network Profile through Azure CLI command like this:
NETWORK_PROFILE_ID=$(az network profile list --resource-group yourResourceGroup --query [0].id --output tsv)
az network profile delete --id $NETWORK_PROFILE_ID -y
After you delete the Network Profile, then you can delete the subnet as you want. For mor details, see Delete network resources.
well, i'd assume your best bet is to try and find the lingering resource and delete it (rest api would work best here, probably). another option is to recreate the ACI with the same name and remove the binding to the network before deleting it.
And your last option would be to contact support ;)
I had the same issue and me help below steps to remove aci subnet and vnet with trash container nic:
1st - find name of problem network profile by command - > az network profile list --query [].name -o tsv
2nd - create resource by terraform -> resource "azurerm_network_profile" "example" with the same network profile name and problem subnet id.
3rd - after terraform successfully create network profile, remove it by -> az network profile delete --id ...(you can find id by az network profile list --query [].id -o tsv)
4th - go to portal and change subnet delegate from container to none, after save I could delete subnet and vnet.
Below is the solution. Sometime trying just delete doesn't work. Follow the below steps and which is tested and worked for me.
NETWORK_PROFILE_ID=$(az network profile list --resource-group yourResourceGroup --query [0].id --output tsv)
az network profile delete --id $NETWORK_PROFILE_ID -y
This is a known issue and Microsoft is working on it. The workaround that worked for me is to update the containerNetworkInterfaceConfigurations property in Network profile properties to an empty list
# Get network profile ID
NETWORK_PROFILE_ID=$(az network profile list --resource-group <reource-group-name> --query [0].id --output tsv)
az resource update --ids $NETWORK_PROFILE_ID --set properties.containerNetworkInterfaceConfigurations=[]
And then deleting it works
az network profile delete --id $NETWORK_PROFILE_ID -y

Azure CLI list Disk

I want to write a bash shell script which will take VMname and Resource Group name as argument and take a snapshot backup of given VM.
Please guide me the command which can list all the disks of particular VM
I dont know Azure CLI to list disks of particular VM
It seems there is no CLI command to list disks associated with a particular VM directly. But Azure VM just have two disk types, so you can list all disks Id associated with the VM through two CLI commands with the two types:
Data disk:
az vm show -d -g groupName -n vmName --query "storageProfile.dataDisks[].managedDisk.id"
Os disk:
az vm show -d -g groupName -n vmName --query "storageProfile.osDisk.managedDisk.id"
And if you want to create a snapshot from the VM, you can create it from the disks one by one through the CLI command az snapshot create.
Hope this will help you, any more question please give me the message.

Copy managed disk snapshots to a different region from the azure command line

I am trying to copy a managed disk snapshot to different region using the Azure command line and it fails with the following error - "The entity was not found"
Here is the command I am using
az snapshot create --resource-group test-azstdsouthcentral --name testcopy3 --location WestUS2 --source /subscriptions/xxx/resourceG
roups/test-azstdsouthcentral/providers/Microsoft.Compute/snapshots/testcopy
Is it supported to copy a managed disk snapshot to a separate region?
Is it supported to copy a managed disk snapshot to a separate region?
You can't create snapshot to different region.
But you can create snapshot to the same region, then copy the snapshot as VHD to a storage account in different region, then create a managed disk from a VHD.
Sample script:
#Provide the subscription Id where snapshot is created
subscriptionId=dd80b94e-0463-4a65-8d04-c94f403879dc
#Provide the name of your resource group where snapshot is created
resourceGroupName=myResourceGroupName
#Provide the snapshot name
snapshotName=mySnapshotName
#Provide Shared Access Signature (SAS) expiry duration in seconds e.g. 3600.
#Know more about SAS here: https://learn.microsoft.com/en-us/azure/storage/storage-dotnet-shared-access-signature-part-1
sasExpiryDuration=3600
#Provide storage account name where you want to copy the snapshot.
storageAccountName=mystorageaccountname
#Name of the storage container where the downloaded snapshot will be stored
storageContainerName=mystoragecontainername
#Provide the key of the storage account where you want to copy snapshot.
storageAccountKey=mystorageaccountkey
#Provide the name of the VHD file to which snapshot will be copied.
destinationVHDFileName=myvhdfilename
az account set --subscription $subscriptionId
sas=$(az snapshot grant-access --resource-group $resourceGroupName --name $snapshotName --duration-in-seconds $sasExpiryDuration --query [accessSas] -o tsv)
az storage blob copy start --destination-blob $destinationVHDFileName --destination-container $storageContainerName --account-name $storageAccountName --account-key $storageAccountKey --source-uri $sas
More information about export/copy managed snapshots as VHD to a storage account in different region, please refer to this link.
More information about create a managed disk from a VHD, please refer to this link.

Resources