DEV Community

Revathi Joshi for AWS Community Builders

Posted on

How to troubleshoot Region Declaration and Outputs errors in Terraform

How do you troubleshoot Terraform Configuration errors?

  • The primary method for interacting with Terraform is the HashiCorp Configuration Language (HCL).

  • When Terraform encounters an error in your configuration, it will report an error including line numbers and the type of issue found in the configuration.

I am going to show you how to troubleshoot the Terraform configuration that contains intentional errors which deploys an EC2 instance with underlying networking.

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. Fix all the Configuration Errors

5. Modified main.tf, variables.tf, and outputs.tf files, after fixing the configuration errors

6. Deploy Your Resources

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
Troubleshoot Terraform - Correct your outputs
learn-terraform-troubleshooting

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_app" {
  ami           = data.aws_ami.linux.id
  availability_zone = var.az_1a
  instance_type = var.instance_type
  user_data              = <<-EOF
              #!/bin/bash
              echo "Hello, World" > index.html
              nohup busybox httpd -f -p 8080 &
              EOF
  tags = {
    Name = $var.name-mywebapp
  }
}
Enter fullscreen mode Exit fullscreen mode
  • 2. variables.tf
variable "regions" {
  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_app.id
}

output "instance_public_ip" {
  description = "Public IP address of the EC2 instance"
  value       = aws_instance.web_app.public.ip
}

output "instance_name" {
  description = "Tags of the EC2 instance"
  value       = aws_instance.web_app.tag.Name
}
Enter fullscreen mode Exit fullscreen mode
  • 4. terrafprm.tfvars
name = "rev"
region = "us-east-1"
Enter fullscreen mode Exit fullscreen mode

4. Fix all the Configuration Errors

Errors

  • 1. Correct the string variable interpolation error
cd terraform_files
Enter fullscreen mode Exit fullscreen mode
  • Terraform format
terraform fmt
Enter fullscreen mode Exit fullscreen mode

Image description

╷
│ Error: Invalid character
│ 
│   on main.tf line 42, in resource "aws_instance" "web_app":
│   42:     Name = $var.name-mywebapp
│ 
│ This character is not used within the language.
╵

╷
│ Error: Invalid expression
│ 
│   on main.tf line 42, in resource "aws_instance" "web_app":
│   42:     Name = $var.name-mywebapp
│ 
│ Expected the start of an expression, but found an invalid expression token.
Enter fullscreen mode Exit fullscreen mode
  • in main.tf Update line 42 as follows to correct the variable interpolation error
  • replace
Name = ${var.name}-my webapp
Enter fullscreen mode Exit fullscreen mode
  • with
Name = "${var.name}-my webapp"
Enter fullscreen mode Exit fullscreen mode
terraform fmt
Enter fullscreen mode Exit fullscreen mode
  • successful

Image description

  • Initiate the working directory
terraform init
Enter fullscreen mode Exit fullscreen mode

Image description

  • 2. Correct the Region Declaration Error

  • Validate the configuration

terraform validate
Enter fullscreen mode Exit fullscreen mode

Image description

╷
│ Error: Reference to undeclared input variable
│ 
│   on main.tf line 14, in provider "aws":
│   14:   region = var.region
│ 
│ An input variable with the name "region" has not been declared. Did you mean "regions"?
╵
Enter fullscreen mode Exit fullscreen mode
  • in variables.tf, Update line 1 as follows to correct the Region Declaration Error

  • replace

variable "regions" {
Enter fullscreen mode Exit fullscreen mode
  • with
variable "region" {
Enter fullscreen mode Exit fullscreen mode
  • 3. Correct the Outputs Error

  • Validate the configuration

terraform validate
Enter fullscreen mode Exit fullscreen mode

Image description

╷
│ Error: Unsupported attribute
│ 
│   on outputs.tf line 8, in output "instance_public_ip":
│    8:   value       = aws_instance.web_app.public.ip
│ 
│ This object has no argument, nested block, or exported attribute named "public".
╵
╷
│ Error: Unsupported attribute
│ 
│   on outputs.tf line 13, in output "instance_name":
│   13:   value       = aws_instance.web_app.tag.Name
│ 
│ This object has no argument, nested block, or exported attribute named "tag". Did you mean "tags"?
Enter fullscreen mode Exit fullscreen mode
  • in outputs.tf, on line 8

  • replace

value       = aws_instance.web_app.public.ip
Enter fullscreen mode Exit fullscreen mode
  • with
value       = aws_instance.web_app.public_ip
Enter fullscreen mode Exit fullscreen mode
  • in outputs.tf, on line 13

  • replace

value       = aws_instance.web_app.tag.Name
Enter fullscreen mode Exit fullscreen mode
  • with
value       = aws_instance.web_app.tags.Name
Enter fullscreen mode Exit fullscreen mode

5. Modified main.tf, variables.tf, and outputs.tf files, after fixing the configuration errors

  • 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_app" {
  ami               = data.aws_ami.linux.id
  availability_zone = var.az_1a
  instance_type     = var.instance_type
  user_data         = <<-EOF
              #!/bin/bash
              echo "Hello, World" > index.html
              nohup busybox httpd -f -p 8080 &
              EOF
  tags = {
    Name = "${var.name}-mywebapp"
  }
}
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_app.id
}

output "instance_public_ip" {
  description = "Public IP address of the EC2 instance"
  value       = aws_instance.web_app.public.ip
}

output "instance_name" {
  description = "Tags of the EC2 instance"
  value       = aws_instance.web_app.tag.Name
}
Enter fullscreen mode Exit fullscreen mode

6. Deploy Your Resources

terraform validate
Enter fullscreen mode Exit fullscreen mode

Image description

terraform plan
Enter fullscreen mode Exit fullscreen mode

Image description

terraform apply
Enter fullscreen mode Exit fullscreen mode
  • enter yes to confirm deployment

Image description

  • EC2 instances - rev-mywebapp

Image description

Cleanup

terraform destroy
Enter fullscreen mode Exit fullscreen mode

Image description

What we have done so far

We have successfully troubleshooted a string variable interpolation error, Region Declaration Error and output errors when formatting our configuration before we have deployed our infrastructure.

Top comments (0)