Terraform Input Variables

Deepak KumarDeepak Kumar
3 min read

Table of contents

Terraform supports several types of variables, which allow you to customize and manage your infrastructure configurations more effectively. Below are the types of variables.

Terraform Input Variables

It allow you to customize the behavior of your Terraform modules without modifying the source code. Below are the way to declare or pass the variable to terraform resources.

  • Declaration: Using the variable block. In the variable block define type, description and default value.

      variable "Variable_name" {
        description = ""
        type        = string # Type of variable
        default     = "us-east-1" # Any default value
      }
      }
    
  • Usage: Accessed using the var keyword, e.g., var.instance_type

    Lets create versions.tf, variables.tf, security-groups.tf, ec2-instance.tf

    version.tf

      # Terraform Block
      terraform {
        required_version = ">= 1.7"
        required_providers {
          aws = {
            source  = "hashicorp/aws"
            version = "~> 4.0"
          }
        }
      }
    
      # Provider Block
      provider "aws" {
        region  = var.aws_region
        profile = "default"
      }
    

    variables.tf

      variable "aws_region" {
        description = "Region name"
        type        = string
        default     = "us-east-1"
      }
    
      variable "ec2_ami_id" {
        description = "AMI name of ec2"
        type        = string
        default     = "ami-0915bcb5fa77e4892"
      }
    
      variable "ec2_instance_count" {
        description = "EC2 Count"
        type        = number
        default     = 1
      }
    

    security-groups.tf

      # Create Security Group - SSH Traffic
      resource "aws_security_group" "MY-SG-SSH" {
        name        = "vpc-ssh"
        description = "MY Security group"
        ingress {
          description = "Allow SSH Port"
          from_port   = 22
          to_port     = 22
          protocol    = "tcp"
          cidr_blocks = ["0.0.0.0/0"]
        }
        egress {
          description = "Allow all IP and port outbond"
          from_port   = 0
          to_port     = 0
          protocol    = -1
          cidr_blocks = ["0.0.0.0/0"]
        }
      }
    
      # Create Security Group - WEB Traffic
      resource "aws_security_group" "MY-SG-WEB" {
        name        = "vpc-web"
        description = "Security group for web"
    
        ingress {
          description = "Allow port 80"
          from_port   = 80
          to_port     = 80
          protocol    = "tcp"
          cidr_blocks = ["0.0.0.0/0"]
        }
    
        ingress {
          description = "Allow port 443"
          from_port   = 443
          to_port     = 443
          protocol    = "tcp"
          cidr_blocks = ["0.0.0.0/0"]
        }
    
        egress {
          description = "Allow all IP and port outbound"
          from_port   = 0
          to_port     = 0
          protocol    = -1
          cidr_blocks = ["0.0.0.0/0"]
        }
      }
    

    ec2-instance.tf

      # Create EC2 Instance
      resource "aws_instance" "MY-EC2-VM" {
        ami                    = var.ec2_ami_id
        instance_type          = "t2.micro"
        key_name               = "terraform-key"
        count                  = var.ec2_instance_count
        vpc_security_group_ids = [aws_security_group.MY-SG-SSH.id, aws_security_group.MY-SG-WEB.id]
        tags = {
          "Name" = "My ec2 vm"
        }
        user_data = <<-EOF
          #!/bin/bash
          sudo yum update -y
          sudo yum install httpd -y
          sudo systemctl enable httpd
          sudo systemctl start httpd
          echo "<h1>Welcome ! AWS Infra created using Terraform in us-east-1 Region</h1>" > /var/www/html/index.html
          EOF
      }
    
  • Provide Input Variables when prompted during terraform plan or apply

    Lets add one new variable instance type ec2_instance_type without any default value

  • v3: Override default variable values using CLI argument -var

  • v4: Override default variable values using Environment Variables

  • v5: Provide Input Variables using terraform.tfvars files

  • v6: Provide Input Variables using <any-name>.tfvars file with CLI argument -var-file

  • v7: Provide Input Variables using auto.tfvars files

  • v8-01: Implement complex type constructors like list

  • v8-02: Implement complex type constructors like maps

  • v9: Implement Custom Validation Rules in Variables

  • v10: Protect Sensitive Input Variables

  • v11: Understand about File function

0
Subscribe to my newsletter

Read articles from Deepak Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Deepak Kumar
Deepak Kumar