How to fix a Terraform String Variable Interpolation Error
Tags: terraform, ec2
Published: 2023-10-17 00:03:48

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](https://github.com/awsmine/Terraform_projects)** 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](https://registry.terraform.io/providers/hashicorp/aws/latest/docs).**
**[Terraform documentation for AMI](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami).**
**[Interpolation Syntax](https://developer.hashicorp.com/terraform/language/v1.1.x/configuration-0-11/interpolation)**
**[Troubleshoot Terraform - Correct a variable interpolation error](https://developer.hashicorp.com/terraform/tutorials/configuration-language/troubleshooting-workflow)**

# 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8b9dd65ogd32pu7eaecj.png)


## 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
  }
}
```

- **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"
}
```

- **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
}
```

- **4. terrafprm.tfvars**

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


## 4. Interpolation Error

- cd terraform-files

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

```
terraform fmt
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ct8h5gjbqq70f67tmk5i.png)


## 5. Fix Interpolation Error

- **main.tf**

- Edit and correct the tags block section by adding this:
- From 

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

- To

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


- Check the format

```
terraform fmt
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0jh6od4l42x09w2wgfbo.png)


- Initialize Terraform

```
terraform init
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bppyvu5o9hbt3l4lungt.png)


- Check syntax

```
terraform validate
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wxcttibs1gd31xcafeea.png)


- Preview changes

```
terraform plan
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ody6f8imfyixecfoqye.png)


- Execute changes
- Type yes to confirm.

```
terraform apply
```

- The apply should be completed successfully.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kpvhv14yp98qs4v3r54z.png)

# Cleanup


```
terraform destroy
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fjjgynhiyxs350x8o0y7.png)


# What we have done so far

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