DEV Community

Srinivasulu Paranduru for AWS Community Builders

Posted on • Edited on

Data Types, Variables, Variable files in Terraform

Overview of Data Types

  • string : A sequence of Unicode characters representing some text, like "hello world".

  • number: a numeric value. The number type can represent both whole numbers like 5 and fractional values like 6.283185

  • bool: a boolean value, either true or false

  • list (or tuple): a sequence of values, like ["London", "New York"].

  • map (or object): a group of values identified by named labels, like {name = "Mabel", age = 52}

  1. Strings, numbers, and bools are called as Primitive types
  2. maps/objects are sometimes called complex types, structural types, or collection types

Input Variables

  • Input variables are like function arguments.
  • Output values are like function return values.
  • Local values are like a function's temporary local variables.

Declaring an Input Variable
Each input variable accepted by a module must be declared using a variable block:

Format:

variable "Name" {
      type  = string
      default = "Hello World"
      Description = "Description of the variable"
}

variable "instance_type" {
}
Enter fullscreen mode Exit fullscreen mode

When declaring a variable we can pass few properties such as type, default value and description of the variable and all 3 parameters are not mandatory. If the properties (type, default and description) are not passed, data type will be assigned at the time of variable assignment.

# string variable
variable "image_id" {
  type = string
}

#number variable
variable "NI_No" {
  type = number
}

# list of objects
variable "docker_ports" {
  type = list(object({
    internal = number
    external = number
    protocol = string
  }))
  default = [
    {
      internal = 8300
      external = 8300
      protocol = "tcp"
    }
  ]
}

Enter fullscreen mode Exit fullscreen mode

Using Input Variable Values

resource "aws_instance" "myfirstec2" {
  instance_type = "t2.micro"
  ami           = var.image_id
}
Enter fullscreen mode Exit fullscreen mode

At the time of provisioning the resources

terraform apply -var="image_id=ami-abc123"
Enter fullscreen mode Exit fullscreen mode

More than one variable

resource "aws_instance" "myfirstec2" {
  instance_type = var.instance_type
  ami           = var.image_id
}
Enter fullscreen mode Exit fullscreen mode

At the time of provisioning the resources using the variables

terraform apply -var="image_id=ami-abc123,instance_type=t2.micro"
Enter fullscreen mode Exit fullscreen mode

Note : If the number of variables increases to 100's, 200's and then it will be very tough pass all variable values at the time of apply, better option will be going with variable definition files.

Variable Definitions (.tfvars) Files
To set lots of variables, it is more convenient to specify their values in a variable definitions file (with a filename ending in either .tfvars or .tfvars.json) and then specify that file on the command line with -var-file:

terraform apply -var-file="test.tfvars"
Enter fullscreen mode Exit fullscreen mode

Environment Variables
Environment variables are named by TF_VAR_ followed by the name of a declared variable

$ export TF_VAR_image_id=ami-abc123
$ terraform plan
Enter fullscreen mode Exit fullscreen mode

Conclusion:
Discussed about data types and variables, variable files in terraform.

If you like my blogs, please like and any suggestion give comments and follow me in dev.to and linked in https://www.linkedin.com/in/srinivasuluparanduru/ for more updates on terraform certification

Top comments (0)