How to use variables in Terraform

This article explains how to use Terraform variables to parameterize configuration. To follow steps described in this article you need have below prerequisites configured.
  • Terraform Installation
  • AWS CLI Installation
  • AWS Credentials Configuration

Follow Set up Terraform with AWS from scratch if you don't have these prerequisites configured.

Input Variables in Terraform

Input Variables serve as parameters for a Terraform module, so users can customize behavior without editing the source. In this tutorial we will learn how to use variables and will create an EC2 instance by using input variables.

Parameterize EC2 instance provisioning using variables

Define Variable


  • Create directory for Terraform configuration.
  •    
    mkdir learn-tf-variables
           
  • Navigate to learn-tf-variables
  •    
    cd learn-tf-variables
           
  • Create file variables.tf.
  • Add below block declaring a variable named aws_region to variables.tf
  •    
    variable "aws_region" {
        description = "AWS region"
        type        = string
    }
         
  • Add below block declaring a variable named ami_id to variables.tf
  •    
    variable "ami_id" {
        description = "AMI ID"
        type        = string
    }
              
         
  • Add below block declaring a variable named instance_type to variables.tf
  •    
    variable "instance_type" {
        description = "EC2 Instance Type"
        type        = string
    }
                    
         
  • Add below block declaring a variable named instance_tags to variables.tf
  •    
    variable "instance_tags" {
        description = "Tags to set for instances"
        type        = map(string)
    }
         
  • After these updates variables.tf should look like as shown below.
  •    
    variable "aws_region" {
        description = "AWS region"
        type        = string
    }
        
    variable "ami_id" {
        description = "AMI ID"
        type        = string
    }
        
    variable "instance_type" {
        description = "EC2 Instance Type"
        type        = string
    }
        
    variable "instance_tags" {
        description = "Tags to set for instances"
        type        = map(string)
    }
         

Assign Values


We have defined variables to Parameterize aws_region, ami_id, instance_type and instance_tags, lets assign values to variables. To assign values create a file named terraform.tfvars and write below content.

   
aws_region    = "us-east-1"
instance_type = "t2.micro"
ami_id        = "ami-033b95fb8079dc481"
instance_tags = {
    Name        = "Test instance",
    Environment = "Test"
}
   

Create EC2 Instance


Create a file name main.tf and write below configuration.

   
terraform {
    required_providers {
        aws = {
        source  = "hashicorp/aws"
        version = "~> 3.27"
        }
    }
    
    required_version = ">= 0.14.9"
}
    
provider "aws" {
    profile = "default"
    region  = var.aws_region
}
    
resource "aws_instance" "test_instance" {
    ami           = var.ami_id
    instance_type = var.instance_type
    tags          = var.instance_tags
}      
      
   

Run commands terraform fmt, terraform validate and finally terraform apply, respond to the prompt with yes to apply changes.

   
terraform apply
 

Category: AWS