Create Service Principal : Implementation Guide

We create service principals to use it instead of doing Sign In every time while using automation tools like terraform.

Step 5: This will also be done with help of Azure CLI so open any terminal on your computer.

Step 6: First set the MSYS_NO_PATHCONV environment variable.

PowerShell Syntax

$env:MSYS_NO_PATHCONV = 1

Linux Syntax

export MSYS_NO_PATHCONV=1

Step 7: To create service principal use command az ad sp create-for-rbac for creating role-based access control (RBAC) service principal.

Syntax

az ad sp create-for-rbac --name <service_principal_name> --role Contributor --scopes /subscriptions/<subscription_id>
  • Replace the <service_principal_name> with any name, or you can also skip this and random name will be assigned automatically.
  • the –role here Contributor which grants full access to all resources in a specified scope.
  • Replace the <subscription_id> with you subscription id which you got in Step 4 in Authenticate to Azure part.

Example: Keep your subscription id secure, I have altered mine in this example command

az ad sp create-for-rbac --name gfgVMtut --role Contributor --scopes /subscriptions/120160be-fda9-4201-85rf-fi5d4551012b 

output: Save the appId, password and tenant securely as they will be needed in further steps and remember to don’t include them in code.

How to Create Windows VM in Azure Using Terraform

In this article, we will cover the whole process of creating Windows VM in Azure using Terraform. When you have to create multiple VMs, perhaps even identical ones, or machines with nearly identical configurations, it can be repetitive and time-consuming to go through the manual setup process each time. That’s where Terraform comes in. Think of it as a blueprint for building VMs without the need for manual work, reducing errors and saving you valuable time. Let’s first see what virtual machines, azure, and Terraform are.

Similar Reads

What Is Azure?

Azure is a cloud computing platform provided by Microsoft and is fully known as Microsoft Azure. Azure provides a lot of computing services through the cloud which are easily available and are cost effective. This platform provides a wide variety of services like storage, Virtual Machines, databases, AI, Machine Learning, Virtual Networks, functions, and the list goes on and on. If you want to learn more about Azure you can visit its official website or visit our GFG Microsoft Azure Tutorial Page....

What Is Terraform?

Terraform is an Infrastructure as Code(IaC) software tool which is open source and is provided by HashiCorp. You need to write code i.e. a configuration file ( written in HashiCorp Configuration Language – HCL ) for what you want to build and Terraform builds that exactly as told....

How To Create Windows VM In Azure Using Terraform? A Step-By-Step Guide

This Whole tutorial is divided into parts so if you have any part already configured then you can skip that part....

Authenticate To Azure : Implementation Guide

For this you need to have Azure CLI installed already....

Create Service Principal : Implementation Guide

We create service principals to use it instead of doing Sign In every time while using automation tools like terraform....

Saving Credentials In Environment

Save the credential in environment variables so that when we’ll write the configuration file then we don’t need to put the credentials explicitly and this will be secure way....

Method 1: Save credentials in Linux

Step 1: Open your terminal....

Method 2: Save Credentials In Windows

Step 1: Open PowerShell....

Create Terraform Configuration Files

All the code provided for the configuration files is provided by Microsoft Azure. So copy and paste code as it is and don’t change it you don’t know what you are doing....

Creating Virtual Machine

Step 1: Open a terminal either Linux Terminal or PowerShell in windows operating system and remember to be in the folder where you created the terraform configuration files....

Delete Resources In Azure Using Terraform

When you don’t need the resources which you created using the terraform you can clear them....

Conclusion

So now you know how to create windows Virtual machine using Azure terraform. The steps can be used to create as many virtual machines as you want, and not only for windows but whatever available operating system, you need to specify that in the terraform configuration files. Terraform is a great way for doing stuffs like this and is great tool for DevOps. While there are other options also to create a virtual machine in Azure like manually creating it using Azure Portal....

Creating VM In Azure Using Terraform – FAQ’s

Why Use Terraform To Create Virtual Machine When It Can Created Manually Using Azure Portal?...