🐶
Terraform

Fix Terraform "Interpolation-only expressions deprecated" Warning

By Filip on 10/07/2024

Learn how to resolve the "Interpolation-only expressions are deprecated" warning in Terraform and write cleaner, more maintainable infrastructure code.

Fix Terraform "Interpolation-only expressions deprecated" Warning

Table of Contents

Introduction

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.

Step-by-Step Guide

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?

  • Readability: The new syntax is cleaner and easier to understand.
  • Consistency: Terraform now uses a more unified approach to expressions and variables.

How to Fix the Warning:

  1. Identify: Look for lines in your Terraform code where you have ${variable_name} and you're not performing any operations within the curly braces.
  2. Simplify: Remove the ${...} around the variable name. For example, change ${var.region} to var.region.

Special Cases:

  • Inside Strings: If you're using a variable within a string that has other content, you still need interpolation. For example: "My server is in ${var.region}"
  • Loops and Conditionals: Even within loops or conditional statements, if you're directly using a variable's value, the new syntax applies.

Tools to Help:

  • terraform fmt: This command helps format your Terraform code, and it might automatically fix some of these warnings.
  • Linters (like TFLint): Linters can automatically scan your code for potential issues, including deprecated syntax, and help you fix them.

Code Example

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:

  • The line 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:

  • We've removed the unnecessary ${...} 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:

  • We still need interpolation here because we're embedding the variable's value within a larger string.

Remember:

  • Run terraform fmt to help automatically format your code and potentially fix some of these warnings.
  • Consider using a linter like TFLint to catch potential issues early on.

Additional Notes

  • Impact of Upgrading: When upgrading to Terraform 0.12 or later from an older version, expect to see these warnings if your code used the old interpolation style. It's a good practice to address them for code clarity.
  • Backward Compatibility: Terraform strives for backward compatibility. While the old syntax might still work in some cases, it's best to migrate to the new syntax to avoid potential issues in future Terraform versions.
  • String Concatenation: In situations where you need to combine variables with static text, you can use the new syntax for a cleaner look:
    name = "my-resource-${var.environment}" 
  • Debugging: If you encounter unexpected behavior after removing interpolation, double-check that you're not performing any operations that require it. The terraform console command can be helpful for testing expressions.
  • Best Practices: Adopting the recommended syntax not only eliminates warnings but also makes your Terraform code more maintainable and easier for others to understand.

Summary

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.

Conclusion

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.

References

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait