Typorithm · medium

Input Variables

terraform-variablesterraformvariabletypedefault
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Input Variables

0%

HCL Basics

A variable block declares a typed, optionally defaulted input; you read it elsewhere with var.<name>, like var.region inside a resource.

Input variables parameterize a configuration so the same code can be reused across environments without editing it.

A variable block sets a type constraint (string, number, bool, list, map, object) and an optional default; with a default the variable is optional, without one it is required.

Anywhere in the configuration you read the value with var.<name>, for example var.region, and Terraform substitutes the resolved value.

01

variable "name" { } declares an input.

02

type constrains accepted values.

03

Read it with var.<name>.

variable "region" {
  type    = string
  default = "us-east-1"
}

resource "aws_s3_bucket" "this" {
  bucket = "my-bucket"

  tags = {
    Region = var.region
  }
}

Read the implementation

Connect each part of the code to the concept before you type.

1. Variable declaration

A variable block declares an input named region that callers can override.

variable "region" {

2. Type constraint

type constrains the accepted value; string rejects numbers, lists, and maps.

type    = string

3. Default value

default makes the variable optional; if no value is passed, this is used.

default = "us-east-1"

4. Using the variable

var.region reads the variable's value inside a resource.

Region = var.region

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Input Variables: Lesson & Practice · Typorithm | ApplyDjinn