0
0
Terraformcloud~5 mins

Arguments and expressions in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Terraform uses arguments and expressions to set values and calculate results in configuration files. This helps you customize resources and reuse values without repeating yourself.
When you want to set a resource property using a fixed value like a name or size.
When you need to calculate a value based on other variables or resource attributes.
When you want to reuse a value multiple times without typing it again.
When you want to use conditional logic to choose between values.
When you want to combine strings or lists to create complex configurations.
Config File - main.tf
main.tf
variable "environment" {
  type    = string
  default = "dev"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = var.environment == "prod" ? "t3.large" : "t3.micro"
  tags = {
    Name = "example-${var.environment}"
  }
}

This file defines a variable environment with a default value.

The aws_instance resource uses an expression to choose the instance type based on the environment.

The tag Name uses string interpolation to include the environment name.

Commands
Initializes the Terraform working directory and downloads necessary provider plugins.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized!
Shows the planned actions Terraform will take based on the configuration and expressions.
Terminal
terraform plan
Expected OutputExpected
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_instance.example will be created + resource "aws_instance" "example" { + ami = "ami-0c55b159cbfafe1f0" + instance_type = "t3.micro" + tags = { + "Name" = "example-dev" } } Plan: 1 to add, 0 to change, 0 to destroy.
Applies the planned changes to create the resource using the arguments and expressions defined.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Creation complete after 15s [id=i-0abcd1234efgh5678] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skips interactive approval to apply changes immediately
Shows any outputs defined in the configuration (none in this example, so empty).
Terminal
terraform output
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from this pattern, remember: arguments set values and expressions calculate or choose values dynamically in Terraform configurations.

Common Mistakes
Using quotes around variables like "${var.environment}" inside expressions unnecessarily.
Terraform 0.12+ supports direct expressions without quotes, so quotes can cause errors or unexpected strings.
Use expressions directly like var.environment without extra quotes.
Trying to use expressions outside of allowed places like resource blocks or variables.
Expressions must be inside argument values; placing them incorrectly causes syntax errors.
Always put expressions as values for arguments or variables.
Not initializing Terraform before planning or applying.
Without initialization, Terraform cannot download providers and will fail.
Run terraform init first to set up the working directory.
Summary
Use arguments to assign fixed or calculated values to resource properties.
Use expressions to add logic like conditionals and string interpolation.
Run terraform init to prepare, terraform plan to preview, and terraform apply to create resources.