Azure Resource Management via API: Postman Workflow

ยท

5 min read

This guide explains authentication, resource creation, deletion, and backup management with Azure REST APIs using Postman.


1. Authentication

To use Azure APIs, you must first obtain a Bearer Token. Follow these steps:

POST: Generate Bearer Token

URL:

https://login.microsoftonline.com/{{tenantId}}/oauth2/token

Body (x-www-form-urlencoded):

KeyValueDescription
grant_typeclient_credentialsGrant type for client authentication
client_id{{clientId}}Azure AD Application's client ID
client_secret{{clientSecret}}Azure AD Application's client secret
resourcehttps://management.azure.com/API Resource (Azure management endpoint)

Steps in Postman:

  1. Go to Body, select x-www-form-urlencoded.

  2. Add the keys and values as above.

  3. Click Send to receive the token in the response.

Copy the access_token from the response and set it as a variable (e.g., {{bearerToken}}) in Postman.


2. Resource Management

PUT: Create Resource Group

URL:

https://management.azure.com/subscriptions/{{subscriptionId}}/resourcegroups/{{resourceGroupName}}?api-version=2021-04-01

Headers:

KeyValue
AuthorizationBearer {{bearerToken}}
Content-Typeapplication/json

Body (JSON):

{
  "location": "{{region}}"
}


PUT: Create Virtual Network with Subnet

URL:

https://management.azure.com/subscriptions/{{subscriptionId}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.Network/virtualNetworks/{{virtualNetworkName}}?api-version=2024-03-01

Body (JSON):

{
  "properties": {
    "addressSpace": {
      "addressPrefixes": [
        "{{vNet-cidr}}"
      ]
    },
    "subnets": [
      {
        "name": "{{subent-name}}",
        "properties": {
          "addressPrefix": "{{subnet-cidr}}"
        }
      }
    ]
  },
  "location": "{{region}}"
}


PUT: Create Network Interface (NIC)

URL:

https://management.azure.com/subscriptions/{{Subscription ID}}/resourceGroups/{{Resource Group Name}}/providers/Microsoft.Network/networkInterfaces/{{NIC Name}}?api-version=2023-05-01

Body (JSON):

{
    "location": "{{region}}",
    "properties": {
        "ipConfigurations": [
            {
                "name": "ipconfig1",
                "properties": {
                    "subnet": {
                        "id": "/subscriptions/{{subscriptionId}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.Network/virtualNetworks/{{vnetName}}/subnets/default"
                    },
                    "privateIPAllocationMethod": "Dynamic"
                }
            }
        ]
    }
}


PUT: Create Virtual Machine

URL:

https://management.azure.com/subscriptions/{{subscription-id}}/resourceGroups/{{Resourcegroup-Name}}/providers/Microsoft.Compute/virtualMachines/{{VM-Name}}?api-version=2024-07-01

Body (JSON):

{
  "location": "{{region}}",
  "properties": {
    "hardwareProfile": {
      "vmSize": "{{VM-Size}}"
    },
    "storageProfile": {
      "imageReference": {
        "sku": "16.04-LTS",
        "publisher": "Canonical",
        "version": "latest",
        "offer": "UbuntuServer"
      },
      "osDisk": {
        "caching": "ReadWrite",
        "managedDisk": {
          "storageAccountType": "Standard_LRS"
        },
        "name": "{{VM-Name}}-OS-disk",
        "createOption": "FromImage"
      }
    },
    "osProfile": {
      "adminUsername": "Otis-POC",
      "computerName": "{{VM-Name}}",
      "adminPassword": "Otis-POC123!@",
      "linuxConfiguration": {
        "provisionVMAgent": true,
        "patchSettings": {
          "patchMode": "ImageDefault"
        }
      }
    },
    "networkProfile": {
      "networkInterfaces": [
        {
          "id": "/subscriptions/{{subscription-id}}/resourceGroups/{{Resourcegroup-Name}}/providers/Microsoft.Network/networkInterfaces/{{NIC-Name}}",
          "properties": {
            "primary": true
          }
        }
      ]
    }
  }
}


GET: Get VM Details

URL:

https://management.azure.com/subscriptions/{{subscriptionId}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.Compute/virtualMachines/{{vmName}}?api-version=2024-07-01

Headers:

KeyValue
AuthorizationBearer {{bearerToken}}
Content-Typeapplication/json


DELETE: Delete Virtual Machine

URL:

https://management.azure.com/subscriptions/{{subscriptionId}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.Compute/virtualMachines/{{vmName}}?api-version=2024-07-01

Headers:

KeyValue
AuthorizationBearer {{bearerToken}}

Body (JSON):

{
  "location": "westeurope",
  "properties": {
    "hardwareProfile": {
      "vmSize": "{{VM-Size}}"
    },
    "storageProfile": {
      "imageReference": {
        "sku": "16.04-LTS",
        "publisher": "Canonical",
        "version": "latest",
        "offer": "UbuntuServer"
      },
      "osDisk": {
        "caching": "ReadWrite",
        "managedDisk": {
          "storageAccountType": "Standard_LRS"
        },
        "name": "{{VM-Name}}-OS-disk",
        "createOption": "FromImage"
      }
    },
    "osProfile": {
      "adminUsername": "Otis-POC",
      "computerName": "{{VM-Name}}",
      "adminPassword": "Otis-POC123!@",
      "linuxConfiguration": {
        "provisionVMAgent": true,
        "patchSettings": {
          "patchMode": "ImageDefault"
        }
      }
    },
    "networkProfile": {
      "networkInterfaces": [
        {
          "id": "/subscriptions/{{subscription-id}}/resourceGroups/{{Resourcegroup-Name}}/providers/Microsoft.Network/networkInterfaces/{{NIC-Name}}",
          "properties": {
            "primary": true
          }
        }
      ]
    }
  }
}


DELETE: Delete Disk

URL:

https://management.azure.com/subscriptions/{{subscriptionId}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.Compute/disks/{{diskName}}?api-version=2024-07-01


DELETE: Delete Resource Group

URL:

https://management.azure.com/subscriptions/{{subscriptionId}}/resourcegroups/{{resourceGroupName}}?api-version=2021-04-01


3. Backup Services Management

PUT: Create Recovery Services Vault

URL:

https://management.azure.com/subscriptions/{{subscriptionId}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.RecoveryServices/vaults/{{vaultName}}?api-version=2016-06-01

Body (JSON):

{
  "properties": {},
  "sku": {
    "name": "{{sku}}"
  },
  "location": "{{region}}"
}


PUT: Create Backup Policy

URL:

https://management.azure.com/Subscriptions/{{subscriptionId}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.RecoveryServices/vaults/{{vaultName}}/backupPolicies/{{policyName}}?api-version=2019-05-13

Body (JSON):

{
  "properties": {
    "backupManagementType": "AzureIaasVM",
    "timeZone": "Pacific Standard Time",
    "schedulePolicy": {
      "schedulePolicyType": "SimpleSchedulePolicy",
      "scheduleRunFrequency": "Weekly",
      "scheduleRunTimes": [
        "2018-01-24T10:00:00Z"
      ],
      "scheduleRunDays": [
        "Monday",
        "Wednesday",
        "Thursday"
      ]
    },
    "retentionPolicy": {
      "retentionPolicyType": "LongTermRetentionPolicy",
      "weeklySchedule": {
        "daysOfTheWeek": [
          "Monday",
          "Wednesday",
          "Thursday"
        ],
        "retentionTimes": [
          "2018-01-24T10:00:00Z"
        ],
        "retentionDuration": {
          "count": 1,
          "durationType": "Weeks"
        }
      },
      "monthlySchedule": {
        "retentionScheduleFormatType": "Weekly",
        "retentionScheduleWeekly": {
          "daysOfTheWeek": [
            "Wednesday",
            "Thursday"
          ],
          "weeksOfTheMonth": [
            "First",
            "Third"
          ]
        },
        "retentionTimes": [
          "2018-01-24T10:00:00Z"
        ],
        "retentionDuration": {
          "count": 2,
          "durationType": "Months"
        }
      },
      "yearlySchedule": {
        "retentionScheduleFormatType": "Weekly",
        "monthsOfYear": [
          "February",
          "November"
        ],
        "retentionScheduleWeekly": {
          "daysOfTheWeek": [
            "Monday",
            "Thursday"
          ],
          "weeksOfTheMonth": [
            "Fourth"
          ]
        },
        "retentionTimes": [
          "2018-01-24T10:00:00Z"
        ],
        "retentionDuration": {
          "count": 1,
          "durationType": "Years"
        }
      }
    }
  }
}


PUT: Back Up an Azure VM

URL:

https://management.azure.com/Subscriptions/{{subscriptionId}}/resourceGroups/{{vaultresourceGroupName}}/providers/Microsoft.RecoveryServices/vaults/{{vaultName}}/backupFabrics/Azure/protectionContainers/iaasvmcontainer;iaasvmcontainerv2;{{vaultresourceGroupName}};{{vmname}}/protectedItems/vm;iaasvmcontainerv2;{{vaultresourceGroupName}};{{vmname}}?api-version=2019-05-13

Body (JSON):

{
  "properties": {
    "protectedItemType": "Microsoft.Compute/virtualMachines",
    "sourceResourceId": "/subscriptions/{{subscriptionId}}/resourceGroups/{{vaultresourceGroupName}}/providers/Microsoft.Compute/virtualMachines/{{vmname}}",
    "policyId": "/subscriptions/{{subscriptionId}}/resourceGroups/{{vaultresourceGroupName}}/providers/microsoft.recoveryservices/vaults/{{vaultName}}/backupPolicies/{{backuppolicyname}}"
  }
}


4. Security Considerations

  1. Store sensitive information securely: Use environment variables or secret managers.

  2. Rotate credentials periodically: Regularly update service principal keys.

  3. Minimum required permissions: Assign only the permissions necessary for operations.

  4. Keep bearer tokens secure: Never expose them in client-side code or logs.


5. Error Handling

Common HTTP Status Codes:

  • 200 OK: Success

  • 201 Created: Resource successfully created

  • 401 Unauthorized: Invalid or expired token

  • 403 Forbidden: Insufficient permissions

  • 404 Not Found: Resource not found

  • 429 Too Many Requests: Throttling error, implement retries

  • 500 Internal Server Error: Azure-side error


6. Conclusion

This guide ensures you can manage Azure resources effectively using Postman and Azure REST APIs. By parameterizing requests, adhering to security best practices, and implementing robust error handling, you can automate operations with confidence. Leverage Postman environments for reusability and ease of collaboration within teams.

Let me know if you need further assistance or enhancements! ๐Ÿ˜Š

ย