Azure Terraform

Exploring the basics - Testing Terraform

Exploring the basics - Testing Terraform

TL;DR You can test Terraform without an Azure/AWS/GCP subscription. It’s free to use.

Jump to recipe

More than IaC?

Some may think that Terraform is only for deploying infrastructure as code, but this is only half the truth. Terraform can be used for anything that has an API, and that does not only include Cloud providers. If no provider is created yet, you develop one yourself with the correct knowhow.

Some of the things you can do:

  • Order pizza from Dominoes.
  • Manage your Spotify playlists.
  • Run local scripts.
  • Write text output.
  • Manage Kubernetes and Helm charts.
  • Manage VMware.

Installation

Installing Terraform is easy. It’s also free. You download it, and place it somewhere that makes it available to your prompts. This means putting the folder in your “PATH” environment variable. Information about this can be found here for Windows and in the Hashicorp install guide for Linux. This again ensures you can run terraform from in any folder by only using the executable name.

I use to download it to my program files folder (%PROGRAMFILES%\Terraform), but you can choose whichever folder you like.

Free Azure

There are free ways of using Azure, and deploying resources to Azure. More information on the free tier here. This will only work if you haven’t created any free Azure account earlier. I would recommend using a dedicated Microsoft account. The experience of deploying resources is pretty important for getting to know Terraform, and the free Azure tier is a great way of getting into this.

Avoid using your work account for this. The optimal solution is to create a dedicated email address in either gmail or outlook.

Deployment

For most examples, this is the way of deploying:

  1. Git clone the repository torivara/public
  2. Navigate to public/terraform/tf-testing
  3. Log in with Az
  4. Run terraform init
  5. Run terraform plan
  6. Run terraform apply

Code for all the examples can be found here.

Azure Free

If you have access to a free tier Azure account, you can deploy some simple Azure services mentioned in the free services table on this page.

This example deploys a resource group and a free tier app service plan. You can deploy as many resource groups as you like. All resources in this Azure example should be free of charge.

Spotify

For this example you need a bit more local config before running, and an active Spotify subscription. You can find all information here. I am not sure if this needs paid or if you can do it with free.

Local authentication proxy

You need to be able to run docker engine in your WSL. I am using Ubuntu for which you can find installation instructions here. Docker Desktop with WSL backend is much more convenient, but for now I have to do without because of licensing issues.

Go to developer.spotify.com and create an app with a client secret. Note the client id and client secret for use later.

Run the docker container in your WSL:

export SPOTIFY_CLIENT_REDIRECT_URI=http://localhost:27228/spotify_callback docker run –rm -it -p 27228:27228 –env-file ./.env ghcr.io/conradludgate/spotify-auth-proxy

I have put the apikey and some other info in a .env file locally, but you can choose to do this however you like. Please review the actual guide from Hashicorp for more instructions on how to do this, or send me a question if something does not work. The only thing I did differently was manual docker instead of Docker Desktop.

Authenticate with the url you got from the container.

Run terraform:

You can see my playlists here from when I played around with this (if they have not been deleted):

Write outputs

Write output without deploying a single resource:

Write the output based on an input variable

Generate random strings and “pet names”

Conditional strings

Run scripts

PowerShell with a null_resource (no resource deployed). Triggers on every run. Lists a process and writes one file to your local folder.

Other free providers for testing

In summary

There are ways of using Terraform which do not require you to have access to any Azure/AWS/GCP subscriptions/accounts. You can practice using HCL, and get to know Terraform without any cost or cloud provider.

The best way of practicing this is of course using cloud providers, and getting knowledge on actual infrastructure deployments, but getting experience with HCL is also worth it in the long run.