Using Terraform with Azure?

This is the fastest method that I could find to discover all of your credentials that you will need to get terraform up and running with Azure.

For these instructions to work, you will need the jq tool, npm and nodejs (linked to node – required on Debian, Ubuntu & Mint)

apt install nodejs npm
ln /usr/bin/nodejs /usr/bin/node

(I use Linux Mint, so need to do this Debian/Ubuntu trick).

Install the azure CLI tool

npm install -g azure-cli

Log into the CLI tool

azure login

Fetch your subscription ID and tenant ID

azure account show --json | jq ".[] | {subscription_id: .id, tenant_id: .tenantId}"

Create an Active Directory application
The -i option is the URL for your application. It doesn’t matter what *URL* or *homepage* you set as Terraform is command line application.

Note: The password specified here will be your *client_secret*, so make it a secure one.

This will return a value, which will be your *client_id*

azure ad app create --json -n unixguru-terraform -i *URL* --home-page *home-page* -p *password* | jq .appId

Create a service principal
As well as creating an active directory application, we need to create a service principal to use under that application

azure ad sp create -a *client_id*

Add the correct permissions
For this example, I’ve given the application access to everything, but you should try to use the minimum permissions necessary to carry out any given task.
You can find a list of all available roles here.
The “Role name” is what you need to pass to the -o flag.

To add permissions to our application, we need to provide both our application’s URL and our subscription_id

azure role assignment create --spn *URL* -o "Owner" -c /subscriptions/*subscription_id*

At this point, you should be able to create a terraform definition and try and execute it.
Here’s a test definition:

provider "azurerm" {
subscription_id = "*subscription_id*"
client_id = "*client_id*"
client_secret = "*client_secret*"
tenant_id = "*tenant_id*"
}

resource "azurerm_resource_group" "tf-test-unixguru`" {
name = "playpen"
location = "UK South"
}

Save that to test.tf then run terraform plan to make sure that all of your credentials work

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

The Terraform execution plan has been generated and is shown below.
Resources are shown in alphabetical order for quick scanning. Green resources
will be created (or destroyed and then created if an existing resource
exists), yellow resources are being changed in-place, and red resources
will be destroyed. Cyan entries are data sources to be read.

Note: You didn't specify an "-out" parameter to save this plan, so when
"apply" is called, Terraform can't guarantee this is what will execute.

+ azurerm_resource_group.tf-test-unixguru
    location: "uksouth"
    name:     "playpen"
    tags.%:   "-computed-"


Plan: 1 to add, 0 to change, 0 to destroy.