Voiced by Amazon Polly |
Introduction
We can fetch any resource details under any service in Azure using Microsoft Azure REST API of that respective resource here I have elaborated on Virtual Machine using REST API. For this, we should have four IDs that are Subscription ID, Tenant ID, Client ID, and Secret Value which are created in the App Registration process.
Customized Cloud Solutions to Drive your Business Success
- Cloud Migration
- Devops
- AIML & IoT
Steps to Generate Bearer Token using Tenant ID, Client ID, Secret Value
- https://login.microsoftonline.com/<your tenant id>/oauth2/token
- Paste the above URL with your Tenant ID in Postman Application
- Go To Body -> select x-www-form-urlencoded and provide the below body as Key and Value pair
Ex: Provide grant_type as Key and client_credentials as Value
grant_type : client_credentials
client_id : “<your Client ID>”
client_secret : ”<your Secret Value>”
resource:https : //management.azure.com/
4. The above API request will respond in JSON format, and we only need to pass the value of the access_token key as a token in all the REST API operations.
Steps to Create Virtual Machine using REST API
- Click here for the URL
- Paste the above URL with your subscription id present under that provided tenant id, provide any resource group name in which the machine is to be created and provide the VM name in the Postman and use the PUT method.
- Go to Headers and provide the below data as key and value pair.
Content-Type: application/json
Authorization: Bearer <paste the generated bearer token in the first step>
- Provide the below sample API Request body in the Body -> select raw.
5. Sample API Request Body:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
{ "location": "eastus", "properties": { "hardwareProfile": { "vmSize": "Standard_DS1_v2" }, "storageProfile": { "imageReference": { "sku": "18.04-LTS", "publisher": "Canonical", "version": "latest", "offer": "UbuntuServer" }, "osDisk": { "caching": "ReadWrite", "managedDisk": { "storageAccountType": "Premium_LRS" }, "name": "myVMosdisk", "createOption": "FromImage" } }, "osProfile": { "adminUsername": "testadmin", "computerName": "testvm", "linuxConfiguration": { "ssh": { "publicKeys": [ { "path": "/home/testadmin/.ssh/authorized_keys", "keyData": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDW9OIrCLYJgrdfPtpGFois7wFrA82V5R13qdoKU5N3EbmOJpPKIEqz4SxKoEzEfQi63KaKMaTlKuG3tVqrCXL7z+eXkth7Wj3ZSwwDimM1tRy50Ko6VsXBRZ/PE9vkPxgPxLoodnFGo1+rdz5o1YHSXK5lonXIr7TA2iVSeUJic0zA64bkDur0//myLF5bXqTtu2VV1yQMwZ5GM8SnwmKKUDK5OMg1hspGRwx6YmHJM5AmQpEbRDaZK1oKq/gSGJFtam2HTK0wI3L2qtcBnes0bv6bBOziv3zkETW0xb218I/7Ze+FNOEB8XNLcRXP4BaDgULQNtsOnjmAdI+6OcA7nUTJpp77GWmXTxPxK+c3g1PeE2pnPIiC0Q0ybPQEEQ1rSKAIqV5E2eb5Z5IAlpv3f0ygnE0vsgjv6TP7gmMCURYqI1tZg6uY3VdN20MgWTEu+lyySwP9nDkj4dJp8c62BGWPlLqq99OP9nAwFqY7nBapYU5BMTBQaEX9TNEekm0= generated-by-azure" } ] }, "disablePasswordAuthentication": true } }, "networkProfile": { "networkInterfaces": [ { "id": "/subscriptions/84ce48b2-19e4-4d7c-96a2-2e3508692c89/resourceGroups/samplegroup/providers/Microsoft.Network/networkInterfaces/testnic", "properties": { "primary": true } } ] } } } |
6. After completing all the above steps, the API will get processed with the API response.
7. From the response, the virtual machine will be created with the given VM name, under the samplegroup resource group, and the same configuration as you provided in the request body.
8. The created VM will get reflected in that same subscription id Azure Portal.
Steps to List Virtual Machine by Resource Group using REST API
- https://management.azure.com/subscriptions/{{subscription_id}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.Compute/virtualMachines?api-version=2022-08-01
- Paste the above URL with the same subscription id, provide the resource group name in which the machine is created in the Postman and use the GET
- Go To Headers and provide the below data as key and value pair.
Content-Type: application/json
Authorization: Bearer <paste the generated bearer token in the first step>
Steps to List All Virtual Machines by Subscription ID using REST API
- https://management.azure.com/subscriptions/{{subscription_id}}/providers/Microsoft.Compute/virtualMachines?api-version=2022-08-01
- Paste the above URL with only the same subscription id and use the GET
- Go To Headers and provide the below data as key and value pair.
Content-Type: application/json
Authorization: Bearer <paste the generated bearer token in the first step>
Steps to Deallocate the Specific Virtual Machine using REST API
- https://management.azure.com/subscriptions/{{subscription_id}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.Compute/virtualMachines/{{vmName}}/deallocate?api-version=2022-08-01
- Paste the above URL with the same subscription id, provide the resource group name in which the machine is created and the VM name in the Postman, and use the POST
- Go To Headers and provide the below data as key and value pair.
Content-Type: application/json
Authorization: Bearer <Generate another token>
- The API will get processed with code 202 saying Accepted and the machine will be deallocated in some time and reflected in the Azure Portal too.
Steps to Delete the Specific Virtual Machine using REST API
- https://management.azure.com/subscriptions/{{subscription_id}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.Compute/virtualMachines/{{vmName}}?api-version=2022-08-01
- Paste the above URL with the same subscription id, provide the resource group name in which the machine is created and the VM name in the Postman and use the DELETE
- Go To Headers and provide the below data as key and value pair.
Content-Type: application/json
Authorization: Bearer <Generate another token>
- The API will get processed with code 202 saying Accepted and the machine will be deleted in some time and reflected in the Azure Portal too.
Conclusion
From this blog, we can perform operations on Virtual Machines like Creating, Listing (By Resource Group and Subscription ID), Deallocating, and Deleting using the REST API from the POSTMAN Application.
Get your new hires billable within 1-60 days. Experience our Capability Development Framework today.
- Cloud Training
- Customized Training
- Experiential Learning
About CloudThat
CloudThat is also the official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft gold partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.
Drop a query if you have any questions regarding Azure REST API and I will get back to you quickly.
To get started, go through our Consultancy page and Managed Services Package that is CloudThat’s offerings.
FAQs
1. Can we perform the same operation on other resources?
ANS: – Yes, we can do the same for all other resources in Azure and API will be different with its related service.
2. Can we fetch other resource details of a created virtual machine like on Disks and Snapshots?
ANS: – Yes, we can fetch those details also using their REST API only.
WRITTEN BY Sridhar Andavarapu
Sridhar works as a Research Associate at CloudThat. He is highly skilled in both frontend and backend with good practical knowledge of various skills like Python, Azure Services, AWS Services, and ReactJS. Sridhar is interested in sharing his knowledge with others for improving their skills too.
Click to Comment