DEV Community

Cover image for Terraform Variables in AWS
Anil KUMAR
Anil KUMAR

Posted on

Terraform Variables in AWS

Whenever we have to use certain values again and again in a file, then we will use variables for that sop that we do not need to change that file multiple times instead we change that value in variable.

Varibles can be of different types based on purpose or based on values. In this blog, let us focus on variables which are based on purpose.

There are 3 types of Variables which are based on purpose, They are:

  1. input
  2. locals
  3. output

Input Variables:

Input variables are like function parameters - they allow you to customize your Terraform configuration without hardcoding values.

variable "environment" {
  description = "Environment name"
  type        = string
  default     = "staging"
}
Enter fullscreen mode Exit fullscreen mode

In the above syntax, we are defining a variable named environment. Here the description and default types are optional but the type is mandatory as it helps what should be the value of that variable.
If we do not define variable anywhere else, the one mentioned in the default value will be final.

# Define in variables.tf
variable "environment" {
  description = "Environment name"
  type        = string
  default     = "staging"
}

variable "bucket_name" {
  description = "S3 bucket name"
  type        = string
  default     = "my-terraform-bucket"
}

# Reference with var. prefix in main.tf
resource "aws_s3_bucket" "demo" {
  bucket = var.bucket_name  # Using input variable

  tags = {
    Environment = var.environment  # Using input variable
  }
}
Enter fullscreen mode Exit fullscreen mode

Local Variables:

Internal computed values - like local variables in programming.
For use cases like string concatenation, we cannot directly use input variables, locals can be of great value in those cases.

locals {
    Environment = "{var.environment}-vpc
    Project     = "Terraform-Demo"
  }

tags{
   Environment=local.Environment
}
Enter fullscreen mode Exit fullscreen mode

In the above code block, you cannot add tag of Environment=var.environment-vpc so we can use locals here.

Output Variables:

As the name suggests, It provides output values to a variable. If we want to pass any value like aws_instance.id, vpc.id to any other resource, we can make use of this. In simple words, Values returned after deployment - like function return values.

output "bucket_name" {
  description = "Name of the S3 bucket"
  value       = aws_s3_bucket.demo.bucket
}
Enter fullscreen mode Exit fullscreen mode

After you run terraform plan or terraform apply, you can see the values of bucket_name in the console after successfull provisioning of infrastructure.

You can also view outputs by running the below commands too:

terraform output                    # Show all outputs
terraform output bucket_name        # Show specific output
terraform output -json              # Show all outputs in JSON format
Enter fullscreen mode Exit fullscreen mode

Terraform Variable Precedence

We have a precedence in terraform describing which will hold higher values when we are passing values as variables.

In the above image you can see the value preference from low to high.

  1. using default file
variable "environment" {
  default = "staging"
}
Enter fullscreen mode Exit fullscreen mode
  1. ENV variables
export TF_VAR_environment="development"
terraform plan
Enter fullscreen mode Exit fullscreen mode
  1. terraform.tfvars
environment = "demo"
bucket_name = "terraform-demo-bucket"
Enter fullscreen mode Exit fullscreen mode
  1. terraform.tfvars.json (Similar to the above one)

  2. *.auto.tfvars or *.auto.tfvars.json (Similar to the above one)

  3. Any -var or -var-file option

terraform plan -var="environment=production"
Enter fullscreen mode Exit fullscreen mode

Conclusion:

Terraform's variable system is a powerful mechanism for building flexible and maintainable infrastructure:

Input Variables: Parameterize the configuration for different environments.

Local Variables: Compute and reuse internal values like common tags and unique names.

Output Variables: Share the results of the deployment for human review or for passing to other resources.

I hope I have clearly explained you about variables in Terraform, variable types and their precedence
Terraform is simple, powerful, and used everywhere in the industry.
See you in the next blog.

Below is the Youtube Video for reference: Tech Tutorials with Piyush — “Terraform Variables in AWS”

Top comments (0)