How to troubleshoot Region Declaration and Outputs errors in Terraform
Tags: terraform, ec2
Published: 2023-10-19 22:12:43

# 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](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.** 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](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)**
**[Troubleshoot Terraform - Correct your outputs](https://developer.hashicorp.com/terraform/tutorials/configuration-language/troubleshooting-workflow)**
**[learn-terraform-troubleshooting](https://github.com/hashicorp/learn-terraform-troubleshooting/tree/main)**

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

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

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

- **4. terrafprm.tfvars**

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

## 4. Fix all the Configuration Errors

**Errors**

- **1. Correct the string variable interpolation error**

```
cd terraform_files
```

- Terraform format

```
terraform fmt
```

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

```
╷
│ 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.
```

- in **main.tf** Update line 42 as follows to correct the variable interpolation error
- replace 

```
Name = ${var.name}-my webapp
```

- with

```
Name = "${var.name}-my webapp"
```

```
terraform fmt
```
- successful


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


- Initiate the working directory

```
terraform init
```

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

- **2. Correct the Region Declaration Error**

- Validate the configuration

```
terraform validate
```

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

```
╷
│ 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"?
╵
```

- in **variables.tf**, Update line 1 as follows to correct the Region Declaration Error

- replace 

```
variable "regions" {
```

- with

```
variable "region" {
```


- **3. Correct the Outputs Error**

- Validate the configuration

```
terraform validate
```

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

```
╷
│ 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"?
```

- in **outputs.tf**, on line 8

- replace 

```
value       = aws_instance.web_app.public.ip
```

- with

```
value       = aws_instance.web_app.public_ip
```

- in **outputs.tf**, on line 13

- replace 

```
value       = aws_instance.web_app.tag.Name
```

- with

```
value       = aws_instance.web_app.tags.Name
```

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

**6.** Deploy Your Resources


```
terraform validate
```

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


```
terraform plan
```

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


```
terraform apply
```
- enter yes to confirm deployment

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

- EC2 instances - rev-mywebapp


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


# Cleanup


```
terraform destroy
```


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




# 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.