Azure, Cloud Computing

4 Mins Read

Operations on Virtual Machine Resource Using Azure REST API

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.

Freedom Month Sale — Upgrade Your Skills, Save Big!

  • Up to 80% OFF AWS Courses
  • Up to 30% OFF Microsoft Certs
Act Fast!

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.

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.

Freedom Month Sale — Discounts That Set You Free!

  • Up to 80% OFF AWS Courses
  • Up to 30% OFF Microsoft Certs
Act Fast!

About CloudThat

CloudThat is an award-winning company and the first in India to offer cloud training and consulting services worldwide. As a Microsoft Solutions Partner, AWS Advanced Tier Training Partner, and Google Cloud Platform Partner, CloudThat has empowered over 850,000 professionals through 600+ cloud certifications winning global recognition for its training excellence including 20 MCT Trainers in Microsoft’s Global Top 100 and an impressive 12 awards in the last 8 years. CloudThat specializes in Cloud Migration, Data Platforms, DevOps, IoT, and cutting-edge technologies like Gen AI & AI/ML. It has delivered over 500 consulting projects for 250+ organizations in 30+ countries as it continues to empower professionals and enterprises to thrive in the digital-first world.

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 Andavarapu is a Senior Research Associate at CloudThat, specializing in AWS, Python, SQL, data analytics, and Generative AI. With extensive experience in building scalable data pipelines, interactive dashboards, and AI-driven analytics solutions, he helps businesses transform complex datasets into actionable insights. Passionate about emerging technologies, Sridhar actively researches and shares insights on AI, cloud analytics, and business intelligence. Through his work, he aims to bridge the gap between data and strategy, helping enterprises unlock the full potential of their analytics infrastructure.

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!