Revathi Joshi
Posted on October 17, 2023
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.
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
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
- Initialize Terraform
terraform init
- Check syntax
terraform validate
- Preview changes
terraform plan
- Execute changes
- Type yes to confirm.
terraform apply
- The apply should be completed successfully.
Cleanup
terraform destroy
What we have done so far
We have successfully fixed the string variable interpolation error and deployed our resources.
Posted on October 17, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.