DEV Community

Revathi Joshi for AWS Community Builders

Posted on

How to fix a Terraform String Variable Interpolation Error

In this article. I am going to show you how fix the string variable interpolation error.

Embedded within strings in Terraform, whether you're using the Terraform syntax or JSON syntax, you can interpolate other values. These interpolations are wrapped in ${}, such as ${var.name}.

The interpolation syntax is powerful and allows you to reference variables, attributes of resources, call functions, etc.

Use the var. prefix followed by the variable name. For example, ${var.name} will interpolate the name variable value.

Please visit my GitHub Repository for Terraform articles on various topics being updated on constant basis.

Let’s get started!

Objectives:

1. Login to AWS Management Console

2. Create infrastructure for resources block

3. Under terraform_files resources directory - Create 4 files - main.tf, variables.tf, outputs.tf and terrafprm.tfvars.

4. Interpolation Error

5. Fix Interpolation Error

Pre-requisites:

  • AWS user account with admin access, not a root account.
  • Cloud9 IDE with AWS CLI.

Resources Used:

Terraform documentation.
Terraform documentation for AMI.
Interpolation Syntax
Troubleshoot Terraform - Correct a variable interpolation error

Steps for implementation to this project:

1. Login to AWS Management Console

  • Make sure you're in the N. Virginia (us-east-1) region

2. Create infrastructure for resources block

  • Let’s create the following organizational structure as shown below.

Image description

3. Under terraform_files resources directory - Create 4 files - main.tf, variables.tf, outputs.tf and terrafprm.tfvars.

  • 1. main.tf


terraform {

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.23"
    }
  }

  required_version = ">= 0.14.9"
}

provider "aws" {
  region  = var.region
}

data "aws_ami" "linux" {
   most_recent = true
   owners      = ["amazon"]

  filter {
    name   = "name"
    values = ["amzn2-ami-hvm-*-x86_64-gp2"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
}

resource "aws_instance" "web_server" {
  ami           = data.aws_ami.linux.id
  instance_type = var.instance_type
  availability_zone = var.az_1a
  user_data              = <<-EOF
              #!/bin/bash
              echo "Hello, World" > index.html
              nohup busybox httpd -f -p 8080 &
              EOF

  tags = {
    Name = $var.name-myweb
  }
}


Enter fullscreen mode Exit fullscreen mode
  • 2. variables.tf


variable "region" {
  description = "region"
}

variable "name" {
  description = "Value of the Name tag for the EC2 instance"
}

variable "az_1a" {
  description = "availability zone 1"
  type        = string
  default     = "us-east-1a"
}

variable "instance_type" {
  description = "Value of the Name tag for the EC2 instance type"
  type        = string
  default     = "t2.micro"
}


Enter fullscreen mode Exit fullscreen mode
  • 3. outputs.tf


output "instance_id" {
  description = "ID of the EC2 instance"
  value       = aws_instance.web_server.id
}

output "instance_ip" {
  description = "Public IP address of the EC2 instance"
  value       = aws_instance.web_server.public_ip
}

output "instance_name" {
  description = "Name of the EC2 instance"
  value       = aws_instance.web_server.tags.Name
}


Enter fullscreen mode Exit fullscreen mode
  • 4. terrafprm.tfvars


name = "rev"
region = "us-east-1"


Enter fullscreen mode Exit fullscreen mode

4. Interpolation Error

  • cd terraform-files

  • An error is expected after running the terraform fmt command.



terraform fmt


Enter fullscreen mode Exit fullscreen mode

Image description

5. Fix Interpolation Error

  • main.tf

  • Edit and correct the tags block section by adding this:

  • From



tags = {
    Name = $var.name-myweb
  }


Enter fullscreen mode Exit fullscreen mode
  • To


tags = {
  Name = "${var.name}-myweb"


Enter fullscreen mode Exit fullscreen mode
  • Check the format


terraform fmt


Enter fullscreen mode Exit fullscreen mode

Image description

  • Initialize Terraform


terraform init


Enter fullscreen mode Exit fullscreen mode

Image description

  • Check syntax


terraform validate


Enter fullscreen mode Exit fullscreen mode

Image description

  • Preview changes


terraform plan


Enter fullscreen mode Exit fullscreen mode

Image description

  • Execute changes
  • Type yes to confirm.


terraform apply


Enter fullscreen mode Exit fullscreen mode
  • The apply should be completed successfully.

Image description

Cleanup



terraform destroy


Enter fullscreen mode Exit fullscreen mode

Image description

What we have done so far

We have successfully fixed the string variable interpolation error and deployed our resources.

Top comments (0)