0
0
Terraformcloud~5 mins

Why expressions add logic in Terraform - Why It Works

Choose your learning style9 modes available
Introduction
Sometimes you want your infrastructure to change based on conditions. Expressions let you add simple decisions inside your Terraform files. This helps you create flexible setups that adjust automatically.
When you want to create a resource only if a certain condition is true.
When you want to set a value differently depending on an environment, like production or testing.
When you want to avoid repeating code by using one configuration that changes based on input.
When you want to enable or disable features without changing the whole file.
When you want to choose between two options based on a true or false check.
Config File - main.tf
main.tf
variable "environment" {
  type    = string
  default = "dev"
}

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

This Terraform file uses a variable called environment to decide which Amazon Machine Image (AMI) to use.

The ami value uses a conditional expression (the question mark and colon) to pick one AMI for production and another for development.

This way, the same resource changes behavior based on the environment without rewriting the whole file.

Commands
This command sets up Terraform in the current folder. It downloads necessary plugins and prepares to work with your configuration.
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!
This command shows what Terraform will do before making any changes. It helps you check if your logic and expressions work as expected.
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-0a91cd140a1fc148a" + instance_type = "t2.micro" + tags = { + "Name" = "example-instance-dev" } } Plan: 1 to add, 0 to change, 0 to destroy.
This command applies the plan and creates the resources. The -auto-approve flag skips the confirmation step for faster execution.
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 manual approval to apply changes immediately
This command shows any outputs defined in your Terraform configuration. Here, it confirms the current state after applying.
Terminal
terraform output
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from this pattern, remember: conditional expressions let you make your infrastructure change automatically based on simple true or false checks.

Common Mistakes
Using '=' instead of '==' for comparison in expressions
Terraform treats '=' as assignment, not comparison, causing syntax errors.
Use '==' to compare values inside expressions.
Not providing both true and false parts in a conditional expression
Terraform requires both options after '?' and ':', missing one causes errors.
Always include both parts: condition ? value_if_true : value_if_false.
Using expressions outside of allowed places like resource arguments or variables
Terraform only supports expressions in specific places; using them elsewhere causes failures.
Use expressions inside resource arguments, variables, or outputs where supported.
Summary
Use conditional expressions to make decisions inside Terraform files.
Run 'terraform init' to prepare your environment before applying changes.
Use 'terraform plan' to preview changes and 'terraform apply' to create resources.