Azure, Cloud Computing

4 Mins Read

Operations on Virtual Machine Resource Using Azure REST API

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.

Steps to Generate Bearer Token using Tenant ID, Client ID, Secret Value

  1. https://login.microsoftonline.com/<your tenant id>/oauth2/token
  2. Paste the above URL with your Tenant ID in Postman Application
  3. 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/

step3GB

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.

  • Cloud Migration
  • Devops
  • AIML & IoT
Know More

Steps to Create Virtual Machine using REST API

  1. Click here for the URL
  2. 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.
  3. 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>

  1. Provide the below sample API Request body in the Body -> select raw.

step4vm

step4vmb

5. Sample API Request Body:

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.

step8vm

Steps to List Virtual Machine by Resource Group using REST API

  1. https://management.azure.com/subscriptions/{{subscription_id}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.Compute/virtualMachines?api-version=2022-08-01
  2. 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
  3. 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>

Step3RG

Steps to List All Virtual Machines by Subscription ID using REST API

  1. https://management.azure.com/subscriptions/{{subscription_id}}/providers/Microsoft.Compute/virtualMachines?api-version=2022-08-01
  2. Paste the above URL with only the same subscription id and use the GET
  3. 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>

Step3sub

Steps to Deallocate the Specific Virtual Machine using REST API

  1. https://management.azure.com/subscriptions/{{subscription_id}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.Compute/virtualMachines/{{vmName}}/deallocate?api-version=2022-08-01
  2. 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
  3. Go To Headers and provide the below data as key and value pair.

Content-Type: application/json

Authorization: Bearer <Generate another token>

  1. 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.

step4d

Steps to Delete the Specific Virtual Machine using REST API

  1. https://management.azure.com/subscriptions/{{subscription_id}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.Compute/virtualMachines/{{vmName}}?api-version=2022-08-01
  2. 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
  3. Go To Headers and provide the below data as key and value pair.

Content-Type: application/json

Authorization: Bearer <Generate another token>

  1. 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.

step4svm

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
Read More

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.

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!