Error Reference to undeclared resource in terraform code

+2 votes
I have created one terraform code for creating vpc, subnets, nat gateway and internet gateway. When i trying to run its thrown error like below

Error: Reference to undeclared resource

  on create-vpc.tf line 77, in resource "aws_subnet" "public-subnet-1":
  77:     vpc_id = "${aws_vpc.production-vpc.id}"

A managed resource "aws_vpc" "production-vpc" has not been declared in the
root module.

I have pasted my terraform code. Kindly valid it

provider"aws" {
access_key="xxxxxxxx"
secret_key="yyyyyyyy"
region="us-east-1"
}

##VPC CIDR Blocks

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

##VPC Variables

variable "region" {
    default    = "us-east-1"
    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"
}

##AWS Provider

#provider "aws" {
#    region = "${var.region}"
#}

terraform {
    backend "s3" {}
}

##

resource "aws_vpc" "production_vpc" {
    #cidr_block = "${var.vpc_cidr}"
    cidr_block = "10.0.0.0/16"
    enable_dns_hostnames = true
        tags = {
             Name = "Production-VPC"
              }
}

resource "aws_subnet" "public-subnet-1" {
    cidr_block = "${var.public_subnet_1_cidr}"
    vpc_id = "${aws_vpc.production-vpc.id}"
    availability_zone = "us-east-1a"
        tags = {
             Name = "Public-subnet-1"
               }
}

resource "aws_subnet" "public-subnet-2" {
        cidr_block = "${var.public_subnet_2_cidr}"
        vpc_id = "${aws_vpc.production-vpc.id}"
        availability_zone = "us-east-1b"
        tags = {
                Name = "Public-subnet-2"
                    }
}

resource "aws_subnet" "public-subnet-3" {
        cidr_block = "${var.public_subnet_3_cidr}"
        vpc_id = "${aws_vpc.production-vpc.id}"
        availability_zone = "us-east-1c"
        tags = {
                Name = "Public-subnet-3"
                      }
}

resource "aws_subnet" "private-subnet-1" {
        cidr_block = "${var.private_subnet_1_cidr}"
        vpc_id = "${aws_vpc.production-vpc.id}"
        availability_zone = "us-east-1a"
        tags = {
                Name = "Private-subnet-1"
                    }
}

resource "aws_subnet" "private-subnet-2" {
        cidr_block = "${var.private_subnet_2_cidr}"
        vpc_id = "${aws_vpc.production-vpc.id}"
        availability_zone = "us-east-1b"
        tags = {
                Name = "Private-subnet-2"
                  }
}

resource "aws_subnet" "private-subnet-3" {
        cidr_block = "${var.private_subnet_3_cidr}"
        vpc_id = "${aws_vpc.production-vpc.id}"
        availability_zone = "us-east-1c"
    tags = {
            Name = "Private-subnet-3"
                 }
}

resource "aws_route_table" "public-route-table" {
    vpc_id = "${aws_vpc.production-vpc.id}"
    tags {
        Name = "Public-Route-Table"
        }
}

resource "aws_route_table" "private-route-table" {
        vpc_id = "${aws_vpc.production-vpc.id}"
        tags = {
                Name = "Private-Route-Table"
                }
}

##Associating Route Tables with Subnets

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

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

##Creating An Elastic IP for NAT Gateway

resource "aws_eip" "elastic-ip-for-nat-gw" {
    vpc = true
    associate_with_private_ip = "10.0.0.5"
        tags = {
            Name = "Production-EIP"
            }
}

##Creating the NAT GateWay and Adding to Route Table

resource "aws_nat_gateway" "nat-gw" {
    allocation_id = "${aws_eip.elastic-ip-for-nat-gw.id}"
    subnet_id = "${aws_subnet.public-subnet-1.id}"
        tags = {
            Name = "Production-NAT-GW"
            }
}

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 An Internet Gateway(IGW) and Adding to Route Table

resource "aws_internet_gateway" "production-igw" {
    vpc_id = "${aws_vpc.production-vpc.id}"
        tags = {
            Name = "Production-IGW"
            }
}

resource "aws_route" "public-internet-gw-route" {
    route_table_id = "${aws_route_table.public-route-table.id}"
    gateway_id = "${aws_internet_gateway.production-igw.id}"
    destination_cidr_block = "0.0.0.0/0"
}

##I have separate  file for subnet cidr values named prod-vpc-cidr.tfvars

I can terraform init

when i run #terraform plan -var-file="prod-vpc-cidr.tfvars"
Sep 13, 2019 in Other DevOps Questions by Lakshminarayanan
• 1,370 points
24,287 views
Hi ,
You have done small mistake,
While creating vpc resource you declared production_vpc.
But while you using or reference that in subnets you are using production-vpc.

That is mistake. Correct it code will work

1 answer to this question.

+1 vote
Check the vpc declaration in subnets, internet gateway etc.
while defining you have used vpc resource name to be "production_vpc",
while declaring in subnets n all you have used "production-vpc"
answered Nov 17, 2019 by ahmed

Related Questions In Other DevOps Questions

0 votes
0 answers
0 votes
1 answer

How to get issues count based on rules in a sonar project?

There are API docs in the footer ...READ MORE

answered May 4, 2018 in Other DevOps Questions by DareDev
• 6,890 points
2,808 views
+1 vote
2 answers

When do we use Chef or Azure SDK to create VM and deploy in automation

The solution to the automated deployment in ...READ MORE

answered Aug 21, 2018 in Other DevOps Questions by Priyaj
• 58,100 points
1,103 views
0 votes
1 answer

salt error : “TypeError: coercing to Unicode: need string or buffer, bool found”

You can check the discussion regarding same ...READ MORE

answered Jun 17, 2018 in Other DevOps Questions by shubham
• 7,340 points
2,395 views
+1 vote
2 answers

Should I commit Terraform State files to the git repository?

Its better not to commit it to ...READ MORE

answered Aug 3, 2018 in DevOps & Agile by Nilesh
• 7,060 points
3,491 views
+3 votes
3 answers

Terraform AWS Cognito App Client

This feature is not currently supported by ...READ MORE

answered Aug 28, 2018 in AWS by eatcodesleeprepeat
• 4,710 points
3,571 views
0 votes
1 answer

How do I scale in Docker Swarm Mode W/Terraform Digital Ocean Load Balancing

The solution you could build for Digital ...READ MORE

answered Jun 19, 2018 in Docker by shubham
• 7,340 points
1,471 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP