One of my longer term goals is to become more proficient with utilising Terraform code to design and deliver infrastructure into Azure. I thought I would spend some time today looking at the basics of terraform and working my way through from setting up my VSCode workspace to creating simple infrastructure in Azure with Terraform.
Getting Started Terraform – Azure
First port of call was the Hashicorp Terrafrom Getting started on Azure page: https://learn.hashicorp.com/collections/terraform/azure-get-started
Introduction to Terraform and Infrastructure as Code includes a brief 4 minute video as well as positioning Terraform as the product of choice for managing infrastructure deployments.
Next Up, Installing Terraform, I opted for the Chocolatey on Windows as the option to manage my package installed on my laptop. Chocolatey is a free open-source package management utility for windows.
Next we can verify our installation as follows:
Building Infrastructure on Azure
Install AzureCLI and user ‘az login’ to logonto your required Azure subscription:
Create a new folder: ‘learn-terraform-azure’
Create ‘main.tf’ with the following sample code:
This code is set to create us a new Resource Group in our Azure Subscription named ‘myTFResourceGroup’ and it will be created in the Uk South Datacentre.
Next we use ‘terraform init‘ to initialise the configuration:
Next, before we can build the infrastructure, Terraform needs to make a plan for what needs to be build or torn down, it saves this information in a terraform ‘state’ file once we run the ‘terraform plan‘ command:
Here we can see the ‘Plan’ command has outputted back to us what will be created.
Finally we use the ‘terraform apply‘ command:
We can see that the output in greed confirms we have 1 resource added. If we log on via the Azure Portal, here we can see our newly created resource group:
Ready and waiting for us to put some resources into!!!
Inspecting Our State File
Using the command ‘terraform show‘ we can inspect the terraform state file:
If we had a long state file, we can use the command ‘terraform state list‘ which gives us a list of resources:
We are going to add a couple of tags to our resource group as follows:
We will now run the ‘terraform plan -out=newplan‘ command, the -out flag ensures we can save a copy of our plan so that when we apply the plan we can be sure to get the correct configuration and not one since the terraform code had potentially been modified further.
You can see above that adding our two new tags to our Resource Group we have run the plan command and outputted the plan to ‘newplan’ to change 1 resource, our resourse group!
Finally we can use ‘terraform apply “newplan”‘ to apply our changes:
We can see our tags if we use the Azure Portal:
The final part of the process would be to utilise Terraform to Destroy infrastructure.
Therefore utilising the ‘terraform destroy‘ command we can do just that:
Type ‘yes‘ to confirm:
I thought I would blog these initial steps with getting started with Terraform as this is the basic principle to configuring infrastructure in Azure utilising Terraform.
My Main ReadMe Page is all set up with a bit about me!
The guys at 100DaysofCloud have set up the GitHub repo to be cloned and also have a great repo containing ideas and areas to collaborate on: https://github.com/100DaysOfCloud/100DaysOfCloudIdeas
My Github Journey tracker can be found here: https://github.com/jonnychipz/100DaysOfCloud
Please Watch/Star my repo and feel free to comment of contribute to anything I push! I really look forward to hearing from anyone who is going to jump on the journey around the same time as me! Lets see where I get to in 100 days!
I would encourage others to jump on this journey, I’m not sure that I will be able to commit every day for 100 days, but as long as I can complete 100 days that will be great!