Starting with Terraform, Windows and Azure Part 3

In the last two posts we got our local toolset going. Now it is time to make sure you can actually connect to your Azure tenant with Terraform. This assumes you have access to an Azure Tennant already. If you don’t then it is really easy to get started with you own Azure Tennant and Microsoft will even throw some credits your way. You can find more information here.

My preferred method of setting up Terraform involves PowerShell, the AzureRM module and a script. The objective is to create a Service Principal and obtain the ClientID and Client Secret. These can be incorporated into you Terraform Scripts or put into environmental variableimages to keep your terraform templates and modules a bit safer.


Now that we got that out of the way, I’ve devised a script to setup the service principal and obtain the credentials.

I Tend to write scripts with mostly functions in them, some I stole, some are my own Winking smile feel free to use the material provided in any way you see fit.

First some variables:

I thought is was a good idea to get the requirements, just to be certain. The following function checks if you have Azure PowerShell installed. If not, you can get is here.

Then we need to get the subscription:

Some code to generate a Random complex password; Note, you’ll get the option to put in your own password if you prefer.

We also need to provide a name for our service principal.

Now the time comes to use the random complex password and convert it to a secure string for  creating the service principle name.

Time to create the Service Principle.

Let’s generate some output, so you can copy that and store it somewhere safe! And I mean SAFE! It’s access to your Azure subscription!

And now to assemble it all in a script and add the code to actually run all the functions 😀

Just copy paste the script above and check the content…(it’s a good habit to check all the code you rip of the web!) and save it as azure-setup.ps1.

You will get an output giving all the info needed to be able to deploy your first resources to Azure.


There are more than one way to setup Terraform and obtain your credentials for Azure. Methods Involving the CLI or the Azure Portal are valid and more information can be found here. The above script fixes everything in about a minute.