Variable

Terraform variables

Terraform supports a few different variable formats. Depending on the usage, the variables are generally divided into inputs and outputs.

The input variables are used to define values that configure your infrastructure. These values can be used again and again without having to remember their every occurrence in the event it needs to be updated.

Output variables, in contrast, are used to get information about the infrastructure after deployment. These can be useful for passing on information such as IP addresses for connecting to the server.

 Output value : - 


Output values make information about our infrastructure available on the command line, and can expose information for other Terraform Configuration 

resource "aws_eip" "lb" {
  domain   = "vpc"
}

output "public_ip"  {
value = aws_eip.lb
}                                                                                          


INPUT Variable
                                                 

Overview of Type Constraints

The type argument in a variable block allows us to restrict the type of value that will be accepted
as the value for a variable 

variable "image_id" {
type = string
}

If no type constraint is set then a value of any type is accepted. 


To apply different types of variables

variable "types" {
  type = map 
  default = {
    us-east-1 = "t2.micro"
    us-west-2 = "t2.nano"
    ap-south-1 = "t2.small"
}
}

to apply map variables

We need to use var.types["us-west-2]

resource "aws_instance" "ec2" {
  ami           = "ami-08abb3eeacc61972d"
  instance_type = var.types["us-west-2"]
}

Variable List 

variable "list" {
  type = list 
  default = ["m5.large","m5.xlarge","t2.medium"]
}

Apply variable to var.list[0]


resource "aws_instance" "ec2" {
  ami           = "ami-08abb3eeacc61972d"
  instance_type = var.list[0]
}

Count Parameter

With Count Parameter, we can simply specify the count value and the resource can be scaled accordingly 

Count index

We can use count.index to get 5 users with differnet name for all 5 users 

  resource "aws_iam_user" "lb" {
  name = "loadbalancer-${count.index}"
  path = "/system/"
  count = 5

}

use list of count.index

resource "aws_iam_user" "lb" {
  name = var.elb_names[count.index]
  path = "/system/"
  count = 3

}

variable "elb_names" {
    type = list
    default = ["dev-loadbalancer","stage-loadbalancer", "prod-loadlbancaer"]
}

Conditional Expression

A conditional expression uses the value of a bool expression to select one of two values.

Syntax of Conditional expression:

condition ? true_val : false_val

If conditon is true then the results is true_val. If condition is false then the result if false_val.


Local Values

A local values assigns a name to an expression, 
allowing it to be used multiple times within a module repating it


Local Values Support for Expression 

Local VAlues can be used for multiple different use-cases like having a condtional expression. 

locals {
 
  name_prefix = "${var.name != "" ? var.name : var.defualt}" 
}

Important Poniters for Local Values 

Local values can be helpful to aviod repeating teh same values or expressions multiple times configuraiton

If overused they can also make a configuraiton hard to read by future maintainers by hiding the actual values used

Use local values only in moderation, in situations where a single value or result is used in many places and that value is likely to be changed in future. 


Comments