Part 8: Terraform with Azure – Deploy terraform.tfvars file

Reading Time: 5 minutes

My journey learning Terraform to allow me to deploy workloads into Microsoft Azure continues. If you missed the previous posts, please visit the links below.

Part 1: Terraform with Azure – How to install Terraform
Part 2: Terraform with Azure – How to install Azure cli
Part 3: Terraform with Azure – How to Install Visual Studio Code
Part 4: Terraform with Azure – How to install Azure Terraform Plugin in Visual Studio Code
Part 5: Terraform with Azure – Install Git and initialise repository
Part 6: Terraform with Azure – Deploy resources in Azure
Part 7: Terraform with Azure – Deploy a variables file in Terraform

Did you know:
A sub directory in Terraform is known as a module. Terraform will only process the *.tf files located in the folder where you run the command terraform plan. If you would like to include configuration from sub directories, then you use the module syntax. For example, If I have a sub directory named networking, which contains my azure network configuration, I can import the networking directory into my working config directory by specifying the below command. We’ll cover more on this as we move on.

module "my_network" {
  source = "./networking"
}

In this blog post I go through the process of compiling a .tfvars file, but before I continue,

What is a .tfvars file?

This is a confusing topic as terraform.tfvars and variables.tf serve a similar role, as in both are variable files. However, a tfvars file stores the default values from a variables.tf file and allows you to override values if required. Still a little confusing, right? Let’s try to work this out by discussing why we would use a tfvars files.

You could create tfvars files to deploy into multiple environments such as prod.tfvars and dev.tfvars. Therefore, the .tf files would hold the default settings that are common among all configurations but the tfvars files could hold different values. Another example of when you could make use of a tfvars file is for applications which have their own unique settings.

Something else to note, Terraform will automatically load the variables from a tfvars file if it’s named terraform.tfvars or *.auto.tfvars and placed in the same directory as your Terraform configuration file. If you wish to execute different named tfvars files, such as, prod.tfvars, staging.tfvars, test.tfvars etc, you can specify a custom tfvars file using the command line parameter -var-file and then specify the path to a variable file. I have specified examples below,

terraform plan -var-file="prod.tfvars"

or 

terraform plan -var-file="app1.tfvars"

So custom named tfvars files can be called upon based on the platform you are deploying to.

Let’s get started from where we left in Part 7. We removed the default values from the variables.tf file, this action resulted in terraform prompting for values when we executed the command terraform plan.

  1. Create a new file named terraform.tfvars

  2. I have loaded both variables.tf and the newly created terraform.tfvars file in a split screen as seen below.

3. In the previous blog post, I removed the default values. For the purpose of this demo, I have hashed the default values as per the screen shot above. If you removed the default values from the variables.tf file, your file will look similar to the one below.

variable "resource_group_name" {
   type = string
  description = "Azure Resource Group Name"
}

variable "resource_group_location" {
   type = string
  description = "Azure Resource Group location"
}

4. I will now copy the text resource_group_name and resource_group_location to the newly created terraform.tfvars file.

5. Next, I input the values for the variables I have copied to the terraform.tfvars file.

My terraform.tfvars file code below,

resource_group_name = "RG-DEMO-PROD1"
resource_group_location = "UK South"


6. Save changes. Next we run terraform plan. Terraform should no longer prompt us to input default values, instead Terraform will automatically recognise the terraform.tfvars file and apply the values. We named the file terraform.tfvars, therefore, the file will be loaded automatically.

Note:
If there was a requirement to run a different named tfvars file such as prod.tfvars, you would specify a command after terraform -plan

Example,

terraform plan -var-file="prod.tfvars"

Type terraform plan -h for a list of available commands

Back to deploying our resource group using the terraform.tfvars file, let’s give it a go. The results are as expected. Terraform has recognised the terraform.tfvars file and not prompted us to insert values manually.

Note:
A terraform.tfstate.backup file is created automatically in case the state file is lost or corrupted and simplifies recovery if needed.


7. So we executed terraform plan and the output shows that our tfvars file was successfully picked up and the values processed by Terraform. Next, we run terraform apply. Review and input ‘yes’

Let’s visit the Azure Portal. The resource group was created successfully.

8. Next, I would like to override a value within the tfvars file, for example, I want to create a resource group but with a name different to the one specified in the terraform.tfvars file. As per the below, my resource group is RG-DEMO-PROD1 and variable name is resource_group_name.

resource_group_name = "RG-DEMO-PROD1"
resource_group_location = "UK South"

9. I type the command below to override the tfvars file for variable resource_group_name. I specify the resource group NEW-RG.

terraform plan -var "resource_group_name=NEW-RG"

10. The result, Terraform reports that the resource group I created earlier will be replaced with the new resource group specified in the command I executed. The old group will be destroyed. I won’t be applying the command below as I only wanted to try out the variable over ride command.


Access the next blog post for part 9, where I deploy a VNET, Subnet, NSG and a tag in Azure. Part 9: Terraform with Azure – Deploy a VNET and Subnet

Thanks for following

Part 5: Terraform with Azure – Install Git and initialise repository

Reading Time: 6 minutes

My journey preparing to get started with learning Terraform to allow me to deploy workloads into Microsoft Azure continues. If you missed the previous posts, please visit the links below.

Part 1: Terraform with Azure – How to install Terraform
Part 2: Terraform with Azure – How to install Azure cli
Part 3: Terraform with Azure – How to Install Visual Studio Code
Part 4: Terraform with Azure – How to install Azure Terraform Plugin in Visual Studio Code

In this blog post I go through the process of installing Git, enabling in Visual Studio code and initialise a git repository.

Notes:
Git is a tool that allows you to create a local repository on your local machine and manages the versions of your files.

GitHub is a cloud hosted solution that will allow you to host your Git repository in the Cloud.

Visual Studio Code also known as VSCode is a code editor which allows users to develop, execute and debug code. There are a range of editor tools available. VS Code is one of the popular ones.

Note, the official Visual Studio Code website states,

VS Code will leverage your machine’s Git installation, so you need to install Git first before you get these features. Make sure you install at least version 2.0.0

However, if Git is not installed, we can still install the software for free from the Git website after the installation of Visual Studio Code.

  1. Visit the download page at Git – Downloads (git-scm.com)

At the time of writing this blog post, the latest version of Git for Windows was 2.35.1

2. I’m using a Windows Operating system, therefore the website detects this automatically and displays a download for Windows. Click ‘Download for Windows’

3. Click here to download

4. After download, run the .exe

5. When ready to continue with setup, click next

6. Click next

7. Click Additional icons, this option will add shortcuts to your desktop for easy access. Click Next

8. Click Next

9. We select the default editor. I’ll be selecting Use Visual Studio Code as Git’s default editor

10. Review and click next

11. Review and click next

12. Review and click next

13. Review and click next

14. Review and click next

15. Review and click next

16. Review and click next

17. Review and click next

18. Review and click next

19. Review and click install

20. and we’re done with the installation of Git. Click Finish

21. Launch the Git Bash shortcut available on your desktop

22. To confirm the version installed, type git –version

How to enable Git in Visual Studio Code (VSCode)

In the next part of this blog post, we will go through the process of enabling Git in VSCode

  1. Launch Visual Studio Code
  2. Click File > Preferences >> Settings

3. Type git enabled in the search bar, and if not already enabled select the check box Git: enabled

That’s VSCode configured to use Git.

Next we need to run some basic configuration to setup Git and create a folder for our Terraform project

4. Launch command prompt (cmd.exe) or use Git-bash and type the following commands, replace the fields with your name and email address.

git config –global user.email “you@example.com

git config –global user.name “Your Name

Now we’ll create a folder for our Terraform project and open the folder in Visual Studio Code.

5. Create a new Project folder on your local machine. For the purpose of this demo, I have created C:\Users\myusername\Projects\Blog-Terraform-Tutorial

6. Launch Visual Studio Code if not already open

7. Click File > Open Folder

8. Browse to the newly created folder and click select. My folder was named Blog-Terraform-Tutorial

9. In Visual Studio Code, click view and enable the option SCM (integrated source control management). Visual Studio Code has integrated source control management (SCM) and includes Git support out-of-the-box.

10. Click the option Initialise Repository

Git is now setup for your new project

11. A .git folder is created within your new project folder. .git is hidden by default due to this folder storing important git related information. However, there may be a requirement to access the folder. You can view the folder in explorer by clicking view and selecting hidden items.

12. The folder will remain hidden within Visual Studio Code by default. To make it visible, back in Visual Studio Code (VSCode) click File > Preferences > Settings and type exclude in the search box. Here is a list of folders hidden in VSCode.

13. Hover over the record **/.git and click the X to delete

14. If the .git folder does not appear, close and relaunch Visual Studio Code

Next, I create a git ignore text file in my local repository (Right click, click new file and name it .gitignore. We’ll be making use of this file later.

What is gitignore?
A gitignore file is a text file you create manually in your repository. The purpose of gitignore is to ensure that certain files which are not tracked by git remain untracked. In most coding projects you will have files that you don’t want to be included in version control, such as build, cache files, etc. Therefore, you can include these files in the .gitignore file which instructs git not to add these excluded files to version control. It’s recommended that a .gitignore file is created after the repository is created.

That’s it for now. I hope you found this blog post useful. Please feel free to comment below if you wish to share any tips.

In the next blog post, I go through the process of deploying the first resources in Azure using Terraform. Check out part 6 at the following link https://cloudbuild.co.uk/part-6-terraform-with-azure-deploy-resources-in-azure/

Part 4: Terraform with Azure – How to install Azure Terraform Plugin in Visual Studio Code

Reading Time: 3 minutes

My journey preparing to get started with learning Terraform to allow me to deploy workloads into Microsoft Azure continues. If you missed the previous posts, please visit the links below.

Part 1: Terraform with Azure – How to install Terraform
Part 2: Terraform with Azure – How to install Azure cli
Part 3: Terraform with Azure – How to Install Visual Studio Code

In this blog post I will go through the process of installing the Azure Terraform plugin within Visual Studio Code.

The VSCode Azure Terraform extension is designed to increase developer productivity authoring, testing and using Terraform with Azure. The extension provides terraform command support, resource graph visualization and CloudShell integration inside VSCode. For more information visit the following link, Azure Terraform Extension

  1. Launch Visual Studio Code
  2. Click the extensions icon as shown in the screenshot below

3. Type Terraform in the search box and take a look at the various plugins available.

For now, I’ll only be installing extensions Azure Terraform and Syntax highlighting and autocompleting . You can also use keys Ctrl Shift and X to access the extensions area within Visual Studio Code

4. Allow the installs to complete

5. To verify the installations, type @installed in the search box as shown below.

Incase you’re wondering why the Azure Account extension was installed. When installing the Azure Terraform extension, Visual Studio Code will automatically install the Azure Account extension. Azure Account is a dependency file for the Azure Terraform extension, which it uses to perform Azure subscription authentications and Azure related code extensions.

In part 5 I go through the process of installing Git on my Windows device and enabling Git in Visual Studio Code.

The difference between Git vs GitHub

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git will allow you create a local repository on your device and manage versions of your files. It runs locally but if required you could clone your local version to the GitHub repository. More info at Git

GitHub, hosted in the cloud, is a code hosting platform for version control and collaboration. It lets you and others work together on projects from anywhere. More info at GitHub

Click on the following link to access part 5 – Part 5: Terraform with Azure – Install Git and enable in Visual Studio Code | Cloud Build

Part 3: Terraform with Azure – How to Install Visual Studio Code

Reading Time: 2 minutes

My journey preparing to get started with learning Terraform to allow me to deploy workloads into Microsoft Azure continues. If you missed the previous posts, please visit the links below.

Part 1: Terraform with Azure – How to install Terraform
Part 2: Terraform with Azure – How to install Azure cli

Moving to Part 3, in this post I will go through the process of installing Visual Studio Code. Let’s get started.

What is Visual Studio Code?
Visual Studio Code combines the simplicity of a source code editor with powerful developer tooling, like IntelliSense code completion and debugging. First and foremost, it is an editor that gets out of your way. The delightfully frictionless edit-build-debug cycle means less time fiddling with your environment, and more time executing on your ideas. For more information on Visual Studio Code click the following link Microsoft.

  1. Visit https://code.visualstudio.com/download
  2. I’ll be downloading the Windows version (System Installer 64-bit) as shown below. The download was approx 70MB in size at the time of writing this post.

3. Run the installation

4. Accept the agreement when ready to do so and click next

5. Click next

6. Click next

7. I select ‘Create a desktop icon’ and click next

8. Click Install

9. Click finish

In part 4, I go through the process of installing the Azure Terraform plugin within Visual Studio Code, click the following link to continue on my journey, Part 4: Terraform with Azure – How to install Azure Terraform Plugin in Visual Code