Learn how to resolve the "Interpolation-only expressions are deprecated" warning in Terraform and write cleaner, more maintainable infrastructure code.
In Terraform, you might encounter the warning "Interpolation-only expressions are deprecated." This warning signals that your code uses an outdated syntax for referencing variables. Previously, you had to enclose variables within ${...}
, even for direct usage. However, from Terraform 0.12 onwards, you can directly use the variable name without the ${...}
for cleaner, more readable code. To eliminate the warning, simply remove the ${...}
around variable names when you're directly using their value. For instance, replace ${var.region}
with var.region
. Remember that interpolation is still necessary when using variables within strings containing other content or when performing operations within the curly braces. Tools like terraform fmt
and linters like TFLint can assist in identifying and rectifying these deprecated syntax instances.
The warning "Interpolation-only expressions are deprecated" appears in Terraform when you're using an older syntax that's no longer recommended. Here's a breakdown:
The Old Way (causing the warning):
region = "${var.region}"
In earlier Terraform versions, you had to wrap variables inside ${...}
even if you were just directly using their value.
The New, Recommended Way:
region = var.region
From Terraform 0.12 onwards, you can directly reference variables without the ${...}
when you're simply using their value.
Why the Change?
How to Fix the Warning:
${variable_name}
and you're not performing any operations within the curly braces.${...}
around the variable name. For example, change ${var.region}
to var.region
.Special Cases:
"My server is in ${var.region}"
Tools to Help:
terraform fmt
: This command helps format your Terraform code, and it might automatically fix some of these warnings.This code snippet illustrates how to resolve the "Interpolation-only expressions are deprecated" warning in Terraform. The old code used "${...}" to reference a variable, which is no longer necessary. The new code directly uses the variable name without the interpolation syntax. However, interpolation is still required when embedding a variable within a string, as shown in the special case example.
This example demonstrates the "Interpolation-only expressions are deprecated" warning and how to fix it.
Scenario: We're defining an AWS S3 bucket and want to use a variable for the region.
Old Code (with warning):
resource "aws_s3_bucket" "example" {
bucket = "my-bucket-${random_id.bucket_suffix.hex}"
region = "${var.region}" # This line will trigger the warning
}
variable "region" {
default = "us-west-2"
}
resource "random_id" "bucket_suffix" {
byte_length = 8
}
Explanation:
region = "${var.region}"
uses the old interpolation syntax, even though we're just directly using the value of var.region
.New Code (fixed):
resource "aws_s3_bucket" "example" {
bucket = "my-bucket-${random_id.bucket_suffix.hex}"
region = var.region # Corrected syntax
}
variable "region" {
default = "us-west-2"
}
resource "random_id" "bucket_suffix" {
byte_length = 8
}
Explanation:
${...}
around var.region
, making the code cleaner and compliant with the recommended syntax.Special Case (interpolation still needed):
resource "aws_instance" "example" {
# ... other configurations ...
tags = {
Name = "Web Server - ${var.environment}" # Interpolation needed within the string
}
}
Explanation:
Remember:
terraform fmt
to help automatically format your code and potentially fix some of these warnings.name = "my-resource-${var.environment}"
terraform console
command can be helpful for testing expressions.Feature | Description |
---|---|
Warning Message | "Interpolation-only expressions are deprecated" |
Cause | Using the old syntax ${...} to reference variables directly, e.g., ${var.region}
|
Solution | Remove the ${...} when directly using a variable, e.g., var.region
|
Reason for Change | Improved readability and consistency in Terraform code |
Fixing the Warning | 1. Identify instances of ${variable_name} without operations inside the curly braces. 2. Replace ${...} with just the variable name. |
Special Cases | Interpolation is still required: - When using variables within strings with other content, e.g., "My server is in ${var.region}" - Inside loops and conditional statements, if performing operations on the variable |
Helpful Tools | - terraform fmt : Formats code and may automatically fix some warnings. - Linters (like TFLint): Scan code for potential issues, including deprecated syntax. |
The "Interpolation-only expressions are deprecated" warning in Terraform indicates the use of an outdated syntax for referencing variables. To modernize your code and eliminate this warning, remove the ${...}
surrounding variable names when directly accessing their values. However, remember that interpolation remains essential when embedding variables within strings containing additional content or when performing operations within the curly braces. Tools like terraform fmt
and linters such as TFLint can prove invaluable in identifying and rectifying these instances of deprecated syntax, ultimately contributing to cleaner, more maintainable Terraform code.