Search code examples
terraformterraform-provider-aws

Having errors when initialize terraform


I have a issue to initialized terraform, can you please help me. I am doing some practices and use version like that: Win10 system subsystem: ubuntu/Terraform v1.4.4 on linux_amd64 AWS profile default in AWS CLI. Structure of code: infrastructure-prod.config/production.tfvars variables.tf vpc.tf

infrastructure-prod.config:

key="PROD/infrastructure.tfstate"
bucket="xxx-xxx-xxx-test-001"
region="XX-XXX-2"

production.tfvars:

vpc_cidr="10.0.0.0/16"
public_subnet_1_cidr= "10.0.1.0/24"
public_subnet_2_cidr= "10.0.2.0/24"
public_subnet_3_cidr= "10.0.3.0/24"
private_subnet_1_cidr= "10.0.4.0/24"
private_subnet_2_cidr= "10.0.5.0/24"
private_subnet_3_cidr= "10.0.6.0/24"

variables.tf:

variable "region" {
  default    ="xx-xxxx-2"
  description="AWS Region"
}

variable "vpc_cidr"{
  default = "10.0.0.0/16"
  description= "VPC CIDR Block"
}
variable "public_subnet_1_cidr"{
  description= "Public Subnet 1 Cidr"
}

variable "public_subnet_2_cidr"{
  description= "Public Subnet 2 Cidr"
}

variable "public_subnet_3_cidr"{
  description= "Public Subnet 3 Cidr"
}

variable "private_subnet_1_cidr"{
  description= "Private Subnet 1 Cidr"
}

variable "private_subnet_2_cidr"{
  description= "Private Subnet 2 Cidr"
}

variable "private_subnet_3_cidr"{
  description= "Private Subnet 3 Cidr"
}

vpc.tf:

terraform{
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">=4.0"
    }
  }
}
provider "aws" {
  region    = "${var.region}"
}

#Create S3
terraform{
  backend "s3"{}
}

#Create VPC
resource "aws_vpc" "production_vpc"{
  cidr_block           = "${var.vpc_cidr}"
  enable_dns_hostnames = true

  tag{
    Name="Production-VPC"
  }
}

#vpc subnet 1
resource "aws_subnet" "public-subnet-1" {
  cidr_block        = "${var.public_subnet_1_cidr}"
  vpc_id            = "${aws_vpc.production_vpc.id}"
  availability_zone = "xx-xxxx-2a"

  tag{
    Name= "Public-Subnet-1"
  }
}

#vpc subnet 2
resource "aws_subnet" "public-subnet-2" {
  cidr_block        = "${var.public_subnet_2_cidr}"
  vpc_id            = "${aws_vpc.production_vpc.id}"
  availability_zone = "xx-xxxx-2b"

  tag{
    Name= "Public-Subnet-2"
  }
}

#vpc subnet 3
resource "aws_subnet" "public-subnet-3" {
  cidr_block        = "${var.public_subnet_3_cidr}"
  vpc_id            = "${aws_vpc.production_vpc.id}"
  availability_zone = "xx-xxxx-2c"

  tag{
    Name= "Public-Subnet-3"
  }
}

#private subnet 1
resource "aws_subnet" "private-subnet-1" {
  cidr_block        = "${var.private_subnet_1_cidr}"
  vpc_id            = "${aws_vpc.production_vpc.id}"
  availability_zone = "xx-xxxx-2a"

  tag{
    Name= "Private-Subnet-1"
  }
}

#private subnet 2
resource "aws_subnet" "private-subnet-2" {
  cidr_block        = "${var.private_subnet_2_cidr}"
  vpc_id            = "${aws_vpc.production_vpc.id}"
  availability_zone = "xx-xxxx-2b"

  tag{
    Name= "Private-Subnet-2"
  }
}

#private subnet 3
resource "aws_subnet" "private-subnet-3" {
  cidr_block        = "${var.private_subnet_3_cidr}"
  vpc_id            = "${aws_vpc.production_vpc.id}"
  availability_zone = "xx-xxxx-2c"

  tag{
    Name= "Private-Subnet-3"
  }
}

#public route table
resource "aws_route_table" "public-route-table" {
  vpc_id = "${aws_vpc.production_vpc.id}"
  tag{
    Name= "Public-Route-Table"
  }
}

#private route table
resource "aws_route_table" "private-route-table" {
  vpc_id = "${aws_vpc.production_vpc.id}"
  tag{
    Name= "Private-Route-Table"
  }
}

# public subnet association 1,2,3:
resource "aws_route_table_association" "public-subnet-1-association" {
  route_table_id= "${aws_route_table.public-route-table.id}"
  subnet_id     = "${aws_subnet.public-subnet-1.id}"
}

resource "aws_route_table_association" "public-subnet-2-association" {
  route_table_id= "${aws_route_table.public-route-table.id}"
  subnet_id     = "${aws_subnet.public-subnet-2.id}"
}

resource "aws_route_table_association" "public-subnet-3-association" {
  route_table_id= "${aws_route_table.public-route-table.id}"
  subnet_id     = "${aws_subnet.public-subnet-3.id}"
}

# private subnet association 1,2,3:
resource "aws_route_table_association" "private-subnet-1-association" {
  route_table_id= "${aws_route_table.private-route-table.id}"
  subnet_id     = "${aws_subnet.private-subnet-1.id}"
}

resource "aws_route_table_association" "private-subnet-2-association" {
  route_table_id= "${aws_route_table.private-route-table.id}"
  subnet_id     = "${aws_subnet.private-subnet-2.id}"
}

resource "aws_route_table_association" "private-subnet-3-association" {
  route_table_id= "${aws_route_table.private-route-table.id}"
  subnet_id     = "${aws_subnet.private-subnet-3.id}"
}

# Create Elastic IP
resource "aws_eip" "elastic-ip-for-nat-gw"{
  vpc                        = true
  association_with_private_ip= "10.0.0.5"

  tags{
    Name= "Production-EIP"
  }
}

#create nat gateway
resource "aws_nat_gateway" "nat-gw" {
  #for EIP
  allocation_id = "${aws_eip.elastic-ip-for-nat-gw.id}"
  subnet_id     = "${aws_subnet.public-subnet-1.id}"

  tag{
    Name="Production-NAT-GW"
  }
  # depends on eip creation
  depends_on = [aws_eip.elastic-ip-for-nat-gw]
}

#create nat gateway route
resource "aws_route" "nat-gw-route" {
  route_table_id         = "${aws_route_table.private-route-table.id}"
  nat-gateway_id         = "${aws_nat_gateway.nat-gw.id}"
  destination_cidr_block = "0.0.0.0/0"
}

#create internet gateway
resource "aws-internet_gateway" "production-igw" {
  vpc_id = "${aws_vpc.production_vpc.id}"

  tag{
    Name = "Production-IGW"
  }
}

#create internet gateway
resource "aws_route" "public-internet-gw-route" {
  route_table_id         = "${aws_route_table.private-route-table.id}"
  gateway_id             = "${aws-internet_gateway.production-igw.id}"
  destination_cidr_block = "0.0.0.0/0"
}

Enter the command in CLI:

terraform init -backend-config="infrastructure-prod.config"

Error:

 Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider hashicorp/aws-internet: provider registry registry.terraform.io does not have a provider named
│ registry.terraform.io/hashicorp/aws-internet
│
│ All modules should specify their required_providers so that external consumers will get the correct providers when using a module. To see which modules are currently depending on    
│ hashicorp/aws-internet, run the following command:
│     terraform providers

Solution

  • You have named one of your resource types wrong. It should be

    #create internet gateway
    resource "aws_internet_gateway" "production-igw" {
      vpc_id = "${aws_vpc.production_vpc.id}"
    
      tag{
        Name = "Production-IGW"
      }
    }
    

    Note that you had a hyphen not an underscore in the resource type. Resource types have a naming convention that is <provider name>_<resource_type>. so for these resources it knows the provider is AWS. However in your resource you have typed as aws-internet_gateway terrform reads this as the provider is aws-internet and the resource type is gateway. By changing the hyphen to an underscore it correctly understands the provider is aws and the resource type is internet_gateway