The more I am using Azure the more I want to make my life easier. Terraform to the rescue it seems. Below you will find my first look at Terraform with the Azure Cloud Shell. I have written as a how to guide so hopefully it helps.
Let’s get to it!
Open the Azure Cloud Shell from within the Azure Portal.
Microsoft was kind enough to install Terraform for us in the Clod Shell so you will not have to install it.
Allow Terraform access to Azure
For
First, you need to make sure you are in the right subscription. (Only needed if you have multiple subscriptions). In the
1 |
az account list --output table |

Now you need to select the subscription you want to use to create the service principal on. To do that just use the following, but make sure you change to your subscription ID.
1 |
az account set --subscription=SubscriptionID |

Now you are ready to create a service principal for use with Terraform. Use the following, but make sure you change the scope to yours.
1 |
az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/subscriptionID" |

<strong>Not</strong>e
Make sure you take a copy of the appID, password and tenant. You will need them.
Time to configure some Terraform environment variables
For Terraform to be able to use the new service principle you need to set some environment variables. Luckily this is easy with the below commands. You will need to fill in some details here such as subscription ID, appID, Password, and tenant ID from before.
1 2 3 4 5 |
echo "Setting environment variables for Terraform" export ARM_SUBSCRIPTION_ID=subscription_id export ARM_CLIENT_ID=appId export ARM_CLIENT_SECRET=password export ARM_TENANT_ID=tenant_id |

Time to test!
To test we need to create a simple Terraform file from within the Cloud Shell. To do this open up Nano by using the following command.
1 |
nano test.tf |
Copy the following code into the window and press Ctrl O to save and then Ctrl X to exit.
1 2 3 4 5 6 |
provider "azurerm" { } resource "azurerm_resource_group" "rg" { name = "Pixel-Terraform" location = "ukwest" } |

Now you can use the following command to download the Azure modules needed to create the Resource in the
1 |
terraform init |

To make sure your Terraform file will work use the following.
1 |
terraform plan |

As you can see it will create a resource group in
To create the resources from the Terraform file use the following.
1 |
terraform apply |

Now if you go to Resource Groups in the Portal you will see your newly created Resource group.

There you have it you have created your first Terraform file and applied it to create an Azure resource group.
Going forward I am going to start looking at building an AKS cluster that will host a RabbitMQ cluster and hopefully will deploy this using Terraform, so look out for that article.
I hope you found this helpful, if you have any questions please reach out.
0 Comments