2 identical VPC's with same CIDR blocks being created

Hi everyone

I am creating a simple VPC and PostgreSQL DB in RDS with terraform scripts, but it keeps creating 2 x VPC’s with the same CIDR block.

main.tf:
provider “aws” {

region = var.region

shared_credentials_file = var.creds

}

module “vpc” {

source = “./vpc”

}

module “dbserver” {

source = “./db”

}

vpc.tf:

module “settings” {
source = “./…/settings”
}

VPC RESOURCE

resource “aws_vpc” “xxxVpcDev” {
cidr_block = “10.0.0.0/16”
tags = {
Name = “xxx Dev VPC”
}
}

SUBNET RESOURCES

resource “aws_subnet” “Public_subnet” {
vpc_id = aws_vpc.xxxVpcDev.id
cidr_block = “10.0.1.0/24”
availability_zone = “af-south-1a”
map_public_ip_on_launch = true

tags = {
Name = “Public Subnet”
}

depends_on = [aws_vpc.xxxVpcDev]
}

resource “aws_subnet” “Private_subnet1” {
vpc_id = aws_vpc.xxxVpcDev.id
cidr_block = “10.0.2.0/24”
availability_zone = “af-south-1b”

tags = {
Name = “Private Subnet1”
}

depends_on = [aws_vpc.xxxVpcDev]
}

resource “aws_subnet” “Private_subnet2” {
vpc_id = aws_vpc.xxxVpcDev.id
cidr_block = “10.0.3.0/24”
availability_zone = “af-south-1c”

tags = {
Name = “Private Subnet2”
}

depends_on = [aws_vpc.xxxVpcDev]
}

Create DB Subnet Group in VPC

resource “aws_db_subnet_group” “default” {
#name = “fh-sn-grp”
name = module.settings.sn_grp_name
subnet_ids = [aws_subnet.Private_subnet1.id, aws_subnet.Private_subnet2.id]
tags = {
Name = “My DB subnet group”
}

#depends_on = [aws_vpc.xxxVpcDev]
}

IGW RESOURCE

resource “aws_internet_gateway” “fh-gw” {
vpc_id = aws_vpc.xxxVpcDev.id

tags = {
Name = “FHIGW”
}

depends_on = [aws_vpc.xxxVpcDev]
}

db. tf:

module “vpc” {
source = “./…/vpc”
}

module “settings” {
source = “./…/settings”
}

resource “aws_db_instance” “fhpgb1” {
allocated_storage = 20 # gigabytes (min 20)
#backup_retention_period = 7 # in days
#db_subnet_group_name = “fh-sn-grp”
db_subnet_group_name = module.settings.sn_grp_name
#db_subnet_group_name = “{var.rds_private_subnet_group}" engine = "postgres" engine_version = "13.3" identifier = "fhpgdb1" instance_class = "db.t3.micro" multi_az = false name = "mydb1" #parameter_group_name = "mydbparamgroup1" # if you have tuned it password = "null" #password = "{trimspace(file(”{path.module}/secrets/mydb1-password.txt"))}" port = 5432 publicly_accessible = false storage_encrypted = true # you should always do this storage_type = "gp2" username = "mydb1" #vpc_security_group_ids = ["{aws_security_group.mydb1.id}"]
skip_final_snapshot = true

#depends_on = [module.vpc.fhvpc]
depends_on = [module.settings.sn_grp_name]

}

settings.tf:
output sn_grp_name {

value = “fh-sn-grp”

}

I cannot for the life of me figure out why it is doing this - any help will be appreciated!

It is difficult to see as it currently stands. Would you be able to reformat the code and show the directory structure?

It’s pretty straightforward:

main.tf is in root
vpc.tf is in a directory under root called \vpc
db.tf is in a directory under root called \db

I’d also appreciate proper code formatting :slight_smile:

As per my understanding the VPC module is called twice, so it is created twice.