Terraform Error: Duplicate output definition

Error

│ Error: Duplicate output definition
│
│   on aws.tf line 14:
│   14: output "instance_public_ip" {
│
│ An output named "instance_public_ip" was already defined at aws.tf:10,1-28. Output names must be unique within a
│ module.


output "instance_public_ip" {
  value = aws_instance.web.public_ip
}

output "instance_public_ip" {
  value = aws_instance.web.security_groups[0]
}

Solution

to 


output "instance_public_ip" {
  value = aws_instance.web.public_ip
}

output "instance_public_sg" {
  value = aws_instance.web.security_groups[0]
}

Terraform Error: Each argument may be set only once

Error


output "instance_public_ip" {
  value = aws_instance.web.public_ip
  value = aws_instance.web.private_ip
}
C:\Users\Rajesh Kumar\Desktop\Terarform\proj1>terraform validate
╷
│ Error: Attribute redefined
│
│   on aws.tf line 13, in output "instance_public_ip":
│   13:   value = aws_instance.web.private_ip
│
│ The argument "value" was already set at aws.tf:12,3-8. Each argument may be set only once.
╵


Solution

output "instance_public_ip" {
  value = aws_instance.web.public_ip
  value = aws_instance.web.private_ip
}

INTO

output "instance_public_ip" {
  value = aws_instance.web.public_ip
}

output "instance_public_ip" {
  value = aws_instance.web.private_ip
}

Terraform Error: Error: Invalid referenceTerraform Error:

Error

C:\Users\Rajesh Kumar\Desktop\Terarform\proj1>terraform validate
╷
│ Error: Invalid reference
│
│   on aws.tf line 2, in resource "aws_instance" "web":
│    2:   ami           = ami-053b0d53c279acc90
│
│ A reference to a resource type must be followed by at least one attribute access, specifying the resource name.

Bad Code

resource "aws_instance" "web" {
  ami           = ami-053b0d53c279acc90
  instance_type = "t3.micro"

  tags = {
    Name = "HelloWorld"
  }
}

Good Code

resource "aws_instance" "web" {
  ami           = "ami-053b0d53c279acc90"
  instance_type = "t3.micro"

  tags = {
    Name = "HelloWorld"
  }
}

Missing the String Quote. Check the list of Datatypes in terraform

The following is a list of data types in Terraform:

  • Primitive types:
    • String: A sequence of Unicode characters representing some text, such as “hello”.
    • Number: A numeric value. The number type can represent both whole numbers like 15 and fractional values such as 6.28318.
    • Bool: Either true or false.
  • Collection types:
    • List: A sequence of values, like [“us-west-1a”, “us-west-1c”].
    • Set: A collection of unique values that do not have any secondary identifiers or ordering.
    • Map: A collection of key-value pairs, like { “key1”: “value1”, “key2”: “value2” }.
    • Object: A collection of named attributes, like { “name”: “Terraform”, “version”: “1.2.3” }.
  • Special type:
    • Null: A value that represents absence or omission.

Terraform Error : validating provider credentials

Error

C:\Users\Rajesh Kumar\Desktop\Terarform\proj1>terraform plan

Planning failed. Terraform encountered an error while generating this plan.

╷
│ Error: validating provider credentials: retrieving caller identity from STS: operation error STS: GetCallerIdentity, https response error StatusCode: 403, RequestID: 0604bfc9-6495-4ab4-8a3a-5c9261c747b5, api error InvalidClientTokenId: The security token included in the request is invalid.
│
│   with provider["registry.terraform.io/hashicorp/aws"],
│   on providers.tf line 18, in provider "aws":
│   18: provider "aws" {
│
╵

Answer

You must set AWS Provider credentials as per the cloud specification. Check below doc

https://registry.terraform.io/providers/hashicorp/aws/latest/docs

RIGHT CODE CAN BE LIKE THIS

provider "aws" {
  region     = "us-east-1"
  access_key = "SSSSSSSSSSSSSSSS"
  secret_key = "oSSSSSSSSSSSSSSSSSSSSSSS"
}

Terraform: Duplicate required providers configuration

Error

Initializing the backend…
Terraform encountered problems during initialisation, including problems
with the configuration, described below.

The Terraform configuration must be valid before initialization so that
Terraform can determine which modules and providers need to be installed.

│ Error: Duplicate required providers configuration

│ on providers.tf line 15, in terraform:
│ 15: required_providers {

│ A module may have only one required providers configuration. The required providers were previously configured at
│ providers.tf:2,3-21.


│ Error: Duplicate required providers configuration

│ on providers.tf line 15, in terraform:
│ 15: required_providers {

│ A module may have only one required providers configuration. The required providers were previously configured at
│ providers.tf:2,3-21.

Answer

YOU SHOULD NOT HAVE MORE THAN TWO “terraform” block in terraform code

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "5.17.0"
    }
	azurerm = {
      source = "hashicorp/azurerm"
      version = "3.73.0"
    }
	github = {
      source = "integrations/github"
      version = "5.37.0"
    }
  }
}

provider "aws" {
  # Configuration options
}


provider "azurerm" {
  # Configuration options
}


provider "github" {
  # Configuration options
}