An Introduction to Terraform Variables

Getting Started with Terraform Variables

If you’ve read our previous blogs, or if you’re a beginner to the world of Terraform, then it’s a necessity to know about the usage of Terraform variables (tfvars). In this blog, we’ll take you through the definition of variables and values, declarations, usage examples, and leave you with some best practices to get started.

Terraform variables are key to mastering the basics of Terraform, but first, it’s important to understand the definition of a variable: known locations of memory where you can write, read, and reuse the values assigned to the variable. There are three different types of tfvars, which include: 1) input variables, 2) output variables, and 3) local values. We’ll walk you through each type, with key examples to guide you along the way.

As an added bonus, Terraform variables and their definitions can be saved in a different file, with the extension “.tfvars” or “.tfvars.json,” making them easily readable and writable.

In this article, we’ll discuss specific components of tfvars, such as:

  • Input Variables
  • How to Assign Values
  • Output Variables
  • Local Values
  • Terraform Variables Best Practices

Input Variables: Definition and Usage

These tfvars act like key pairs, where for each input variable you’re defining a block with a name, type, and then value. These blocks can be used either in the root module or in the child modules.

Let’s look at a few examples for declaring blocks:

Image for post

Source: Declaring Input Variables

In the above example code, two blocks have been declared.

  1. The first block is for a Terraform variable with the name “availability_zone_names”, where the type is a list of strings and a default value of “us-west-1a” has been assigned.
  2. The second block has the name “docker_ports”, where the type is a list of objects and these objects come in key-value pairs, where there are 3 keys:
  3. a) internal
  4. b) external, which takes the value as numbers
  5. c) protocol, which will accept string values. Accordingly, default values have been assigned to “protocol.”

As seen in the above examples, it should now be clear that Terraform variables are always declared after using the keyword “variable”. We then start to define a block by using the open curly brackets “{“. Inside the block, we can define the type, and assign default values. Finally, the block ends with a closing curly bracket “}”.

NOTE: The data types seen in the below snippet are supported:

Image for post

Source: Types and Values

Other than the above data types, another keyword “any” is used to define that a particular tfvars can be assigned with any value.

We can also have a description tag present between the opening and closing brackets. The description is used mostly for documentation purposes, and the usage of the Terraform variable is added here. Default values and descriptions are not mandatory and can be skipped if not required. The below example is a snippet from one of our configuration files where we have written a description, but skipped default values.

Image for post

How to Assign Values

Up until this point, we’ve discussed how to define and give default values. But, what if there is no default assigned? In this case, how can we assign them?

Firstly, it’s necessary to understand that Terraform variables can be defined either in the root module or child module. When they are defined in child modules, we directly pass values to these modules from their parent module or the caller module.

If your tfvars are defined in the root module, things get a bit more interesting. There are actually three different ways in which values could be assigned (with pros and cons to each, of course). We’ll walk you through your options:

How to Assign Values from the Root Module:

  1. Through commands on the CLI
  2. The “.tfvars” file
  3. Using Environment variables

1._ Through commands on the CLI_

Using the CLI is the most conventional way, but there are disadvantages. When you are using either the “plan” or “apply” commands, you may use the option “-var”. The option may be used any number of times with these commands (even in a single command). However, we would not recommend going through commands on the CLI, particularly if you have many or complex variables in your configuration modules.

Here’s an example of how to use the “apply” command with “-var”:

Image for post

  1. The “tfvars” file — a workaround

As we mentioned above, if you have many or complex variables in your configuration modules, using commands on the CLI may not be the best way. However, there is a workaround: using the definition file. When you have many variables that need to be assigned values, consider creating a file with the extension “.tfvars” or “.tfvars.json” which will make the process simpler and more efficient.

It’s easy to do — simply define a file with only the Terraform variables, with their values, and name the file with the extensions mentioned above. Once completed, you may use the “apply” command with the option “-var-file” and the file name. Below is an example of how this command may be used:

Image for post

**NOTE: **If you have any files with the name “terraform.tfvars” or “terraform.tfvars.json”, then these variable definition files are loaded automatically.

Image for post

3.** Using Environment variables**

This is a dynamic value that has been set-up on your operating system and it affects how any process on your system works. These are always in pairs. In this case, your system is searched for the environment variables whose name might be in the format as seen below:

Image for post

Below is an example of how you may assign values in this case:

Image for post

#terraform #devops

What is GEEK

Buddha Community

An Introduction to Terraform Variables
Joseph  Murray

Joseph Murray


Reference Variable in Java

Before We Started with the Reference variable we should know about the following facts.

**1. **When we create an object(instance) of class then space is reserved in heap memory. Let’s understand with the help of an example.

Demo D1 = new Demo();

Reference Variable

Now, The space in the heap Memory is created but the question is how to access that space?.

Then, We create a Pointing element or simply called Reference variable which simply points out the Object(the created space in a Heap Memory).

Heap Memory

**Understanding Reference variable **

1. Reference variable is used to point object/values.

**2. **Classes, interfaces, arrays, enumerations, and, annotations are reference types in Java. Reference variables hold the objects/values of reference types in Java.

3. Reference variable can also store null value. By default, if no object is passed to a reference variable then it will store a null value.

**4. **You can access object members using a reference variable using **dot **syntax.

.<instance variable_name / method_name>

#java #reference variable in java #variables in java #reference variables #refer #variable's

Ray  Patel

Ray Patel


Scope of a Variable In Java [With Coding Example]


Programmers define the scope of a Variable in Java that tells the compiler about the region from where a variable is accessible or visible. The scope of a variable in Java is static by nature. It means we have to declare it at compile time only. In this article, you will learn about the scope of a Java variable along with its types

What is the Scope of a Variable in Java?

Salient Points About the Scope of a Variable in Java


#full stack development #java variable #scope of a variable in java #variable #scope of a variable #scope

Getting Started With Terraform Modules


In this article, we will see a subtle introduction to terraform modules, how to pass data into the module, get something from the module and create a resource (GKE cluster), it’s intended to be as simple as possible just to be aware of what a module is composed of, or how can you do your own modules, sometimes it makes sense to have modules to abstract implementations that you use over several projects, or things that are often repeated along the project. So let’s see what it takes to create and use a module.

The source code for this article can be found here. Note that in this example I’m using GCP since they give you $300 USD for a year to try their services and it looks pretty good so far, after sign-up you will need to go to IAM, then create a service account and after that export the key (this is required for the terraform provider to talk to GCP).

Composition of a Module

A module can be any folder with a file in it, yes, that is the only required file for a module to be usable, but the recommendation is that you also put a file with a description of the module if it’s intended to be used by people if it’s a sub-module it’s not necessary, also you will need a file called and other of course if it’s a big module that cannot be split into sub-modules you can split those files for convenience or readability, variables should have descriptions so the tooling can show you what are they for, you can read more about the basics for a module here.

Before moving on let’s see the folder structure of our project:



├── account.json






├── module


│   ├──


│   ├──


│   └──




└── terraform.tfvars



1 directory, 8 files

The Project

Let’s start with the that will call our module, notice that I added a few additional comments but it’s pretty much straight forward, we set the provider, then we define some variables, call our module and print some output (output can also be used to pass data between modules).



## Set the provider to be able to talk to GCP


provider "google" {


  credentials = "${file("account.json")}"


  project     = "${var.project_name}"


  region      = "${var.region}"





## Variable definition


variable "project_name" {


  default = "testinggcp"


  type    = "string"





variable "cluster_name" {


  default = "demo-terraform-cluster"


  type    = "string"





variable "region" {


  default = "us-east1"


  type    = "string"





variable "zone" {


  default = "us-east1-c"


  type    = "string"





## Call our module and pass the var zone in, and get cluster_name out


module "terraform-gke" {


  source = "./module"


  zone = "${}"


  cluster_name = "${var.cluster_name}"





## Print the value of k8s_master_version


output "kubernetes-version" {


  value = module.terraform-gke.k8s_master_version



Then terraform.tfvars has some values to override the defaults that we defined:



project_name = "testingcontainerengine"


cluster_name = "demo-cluster"


region = "us-east1"


zone = "us-east1-c"

#tutorial #devops #terraform #gcp cloud #terraform tutorial #kubernetes for beginners #terraform modules

Lindsey  Koepp

Lindsey Koepp


Terraform Imports: Resources, Modules, for_each, and Count

If you are developing Terraform you will at some point work with Terraform imports. A simple web search yields plenty of results for simple imports of Terraform resources. However, often missing are some of the more complex or nuanced imports one might encounter in the real world (such as importing modules or resources created from for_each and count).

This guide will quickly cover the generic examples you find easily on the web, focus on some more unique stuff usually hidden in forum posts, and provide a handful of techniques I’ve picked up since Terraform imports became a functionality.

This guide assumes the reader has a good understanding of Terraform, Terraform modules, state file manipulation, and CI/CD. I’ll be using AWS for the examples.

Resource Import

This is perhaps the most prevalent example when searching for Terraform imports. Quite simply you have a resource defined in your Terraform code, some infrastructure out in the environment matching your Terraform resource definition, and you want to import that infrastructure into your Terraform state.

This example is for an aws_iam_user. I’ve already created a user named “bill” via the AWS IAM console and I would like to import this user into my Terraform state. Easy enough!

My Terraform code:

resource "aws_iam_user" "bill" {
  name = "bill"
  tags = { "foo" = "bar" }

A simple command:

$ terraform import aws_iam_user.bill bill

#aws #terraform-import #infrastructure-as-code #terraform-modules #terraform

Kole  Haag

Kole Haag


Using Terraform to Create an EC2 Instance With Cloudwatch Alarm Metrics

Hey guys! I wanted to do a quick tutorial on how I created an EC2 module for Terraform. If you want to see the repository it is located in check it out here. This module will do a few things:

  1. Create an EC2 Instance
  2. Automatically look up the latest Windows Server 2019 AMI for the EC2 instance.
  3. Create and attach a additional drive.
  4. Create a Cloudwatch Alarm Metric to monitor CPU.

The folder structure looks like this:

Image for post

First things first… I created the file which contains all of my configuration except for the variables and outputs. The has a few parts to it.

AWS Instance Code

The first section is the instance resource code

#AWS Instance

resource "aws_instance" "example" {
     ami =
     instance_type = "t2.micro"
     availability_zone = var.availability_zone

You will notice a few things here.

  1. The instance type is set in the module to t2.micro
  2. availability_zone is set using a variable
  3. ami is set using data

We will get the the availability zone piece in just a bit, first we are going to tackle the data used for the ami argument.

Data for AMI Using a Filter

The next bit of code for the filter looks like this

#AMI Filter for Windows Server 2019 Base

data "aws_ami" "windows" {
     most_recent = true
     filter {
       name   = "name"
       values = ["Windows_Server-2019-English-Full-Base-*"]
     filter {
       name   = "virtualization-type"
       values = ["hvm"]
     owners = ["801119661308"] ## Canonical

The argument most_recent is set to true. This means that it will grab the most recent AMI that fits the criteria that we specify in our filter.

Next you will notice that in the name we set the value to *Windows_Server-2019-English-Full-Base- **with the star at the end. This lets Terraform know we don’t care about what text comes after that point and it was done because the standard format puts the date there. If we set the date the ami was created and set the most_recent argument to true it would not do us any good.

After that we set the virtualization-type to hvm. I am not going to go into a lot of detail here. Just know this is a good idea and do some additional research on hvm vs pv.

Last we set **owners **to 801119661308.

Now I am sure you are asking… how the heck do I actually get this information? Well you are going to have to run a quick command with the AWS cli.

First, login to AWS and get the ami you want to grab the information for. Here is an example:

Image for post

If you click on launch instance you can do a search.

#aws-ec2 #hashicorp-terraform #aws-cloudwatch #terraform-modules #terraform