Skip to content

Instantly share code, notes, and snippets.

@bhanu-prakashl
Forked from plasx/AzureCheatSheet.md
Created April 11, 2022 18:14
Show Gist options
  • Save bhanu-prakashl/124fc20d43b2a89940abb9adc9d6fa16 to your computer and use it in GitHub Desktop.
Save bhanu-prakashl/124fc20d43b2a89940abb9adc9d6fa16 to your computer and use it in GitHub Desktop.
Azure Cheat Sheet

Listing Images via Azure CLI

az vm image list --output table

Deleting certain resoruces in a resource group

List resource groups in a subscription

az resource list

List resources in a group

az resource list -g <resource-group-name>    

Find just partial information you need to delete the resource(this will give you just the name,type)

 az resource list -g <resource-group-name> | grep "name\|type"

Delete resource

az resource delete -g <resource-group-name> --name <name> --resource-type <resource-type>

delete whole resoruces group

Delete a resource group (the whole thing)

az group delete -n <resource-group-name>

Creation

Create Resource Group

az group create -n <resource-group-name> --location <region> --tags "createdby=Daniel Plas Rivera" "year=2019"

Create Storage Account

az storage account create --resource-group <Resource-group-name> --name <storageaccountname> --sku Standard_LRS

NOTES

Typing -h after any command seeing the options will help you incase it starts to not make sense after a while. for instance if you forget why you typed "az storage account create" you can simply traverse the help documentation by typing -h after any part of the command:

az -h
...
options at root level of az
...
az storage -h
...
options at storage level of az
...
az storage create -h
...
options at create level of the storage action
...

Finding object_id for packer windows build

Unlike linux build templates in packer, Windows builds require an object_id, you can find the object_id by going loging into azure cli "az login" then doing the following:

az account show

You'll get returned a json object, grab the user.name value:

{
  "environmentName": "AzureCloud",
  "id": "xxxxxxxx-1111-1111-xxxx-xxxxxxxxxxxx",
  "isDefault": true,
  "name": "Experminent",
  "state": "Enabled",
  "tenantId": "xxxxxxxx-2222-2222-xxxx-xxxxxxxxxxxx",
  "user": {
    "name": "xxxxxxxx-3333-3333-xxxx-xxxxxxxxxxxx",
    "type": "servicePrincipal"
  }
}

Grab the "user":"name" value that's in the object

"name": "xxxxxxxx-3333-3333-xxxx-xxxxxxxxxxxx",

Show the object_id of that user by doing this

az ad sp show --id xxxxxxxx-3333-3333-xxxx-xxxxxxxxxxxx

An json object with the "objectId" will be in it after you enter that command

{
  "appId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
  "displayName": "SuperLab",
  "objectId": "11111111-2222-3333-4444-555566667777",
  "objectType": "ServicePrincipal",
  "servicePrincipalNames": [
    "http://example.com/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  ]
}

You can then use the "objectId" in your packer build template or anyhwere else you might need an objectId. Below is where you provide it in a packer build "object_id"

{
  "builders": [{
    "type": "azure-arm",


    "client_id": "",
    "client_secret": "",
    "subscription_id": "",
    "tenant_id": "",
    "object_id": "11111111-2222-3333-4444-555566667777",

    "managed_image_resource_group_name": "myResourceGroup",
    "managed_image_name": "myStorageAccount",

    "os_type": "Windows",
    "image_publisher": "MicrosoftWindowsServer",
    "image_offer": "WindowsServer",
    "image_sku": "2016-Datacenter",

    "communicator": "winrm",
    "winrm_use_ssl": "true",
    "winrm_insecure": "true",
    "winrm_timeout": "3m",
    "winrm_username": "packer",

    "azure_tags": {
        "dept": "Engineering",
        "task": "Image deployment"
    },

    "location": "East US",
    "vm_size": "Standard_DS2_v2"
  }],
  "provisioners": [{
    "type": "powershell",
    "inline": [
      "Add-WindowsFeature Web-Server",
      "if( Test-Path $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml ){ rm $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml -Force}",
      "& $Env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /shutdown /quiet"
    ]
  }]
}

###create image from VHD az image create --resource-group <> --name <> --os-type Windows --source https://sfdasfasfasdf.vhd

list limits

az network list-usages --location <location> -o table

Delete Image from a resource group

az image delete -n <image name> --resource-group <resource group>

list images

az image list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment