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"