Using subnets launched VPC in RDS

Hello all, this is my first question here. Apologize if something goes wrong, I’m also a newbie in Terraform,

Anyway, I have two modules in my AWS Terraform script, and it should be as simple as possible, just to help me to understand how it works.

In the vpc module, I create the VPC itself, subnets (let’s say 6 of them, 3 public, 3 private), internet gateway, route tables, and NAT gateway.

########################################
# starting private subnet - API
resource "aws_subnet" "api-subnets" {
  count = var.api_subnets_size
  vpc_id = aws_vpc.main-pub-vpc.id
  cidr_block = "10.0.${count.index+10}.0/24"
  availability_zone = data.aws_availability_zones.available.names[count.index]
  map_public_ip_on_launch = false
  tags = {
    "Name" = "${var.prefix}-subnet-api-${count.index+1}",
    "cliente" = var.client,
    "ambiente" = "dev"
  }
}

exposing them to other modules (I think they are exported as a list)

output "api_subnet_ids" {
  value = aws_subnet.api-subnets[*].id
}

In the rds module, I create a Postgres instance in the private subnets, so I have to get them from the vpc module to create the subnet group. Easy, right?

First I have to declare rds module in my main.tf and get that output from the vpc module.

module "vpc" {
  source = "./modules/vpc"
  prefix = var.prefix
  client = var.client
  api_subnets_size = var.api_subnets_size
  api_subnet_ids = module.vpc.api_subnet_ids <== HERE
}

Then the variable is used to set up the subnet ids list in the rds module:

module "rds" {
  source = "./modules/rds"
  prefix = var.prefix
  client = var.client
  api_subnet_ids = module.vpc.api_subnet_ids <== AND HERE
}

Finally, I use this variable to set the subnets in the rds subnets group:

resource "aws_db_subnet_group" "api-subnet-grp" {
  name = "${var.prefix}-api-subnet-grp"
  subnet_ids = api_subnet_ids  # -> why doesn't work here? It is a list in both source and destination parameters
  description = "subnet group to allow Multi AZ on this instance"
  tags = {
    "Name" = "${var.prefix}-api-subnet-grp",
    "cliente" = var.client,
    "ambiente" = "dev"
  }
}

Apparently, the subnet_ids property type of resource aws_db_subnet_group is different from subnets property type of resource aws_subnet list of subnets…

I am completely lost here. I really don’t know what I did wrong or how to proceed to move forward. It was supposed to be easy.

Thanks,
Renato

Assuming you are just talking about hcl (you posted in the CDK for Terraform section)…
Did you perhaps miss defining a api_subnet_ids variable within your rds module? At the very least, it’s passed as a variable but the reference is missing var. in your example.

1 Like

Hi @jsteinich , thanks for replying.

Yes, I did create the output in the vpc module for db-subnets:

output "db_subnet_ids" {
  value = aws_subnet.db-subnets[*].id
}

Also, I created the variable db-subnets in the rds module:

variable "db_subnet_ids" {}

Finally, in main.tf the information gets linked to each other:

module "rds" {
  source = "./modules/rds"
  db_subnet_ids = module.vpc.db_subnet_ids

  # other stuff
}

Did I miss something?

Regards

Do you have api_subnet_ids or var.api_subnet_ids when creating your aws_db_subnet_group resource?

What error do you get?

Hello @jsteinich

I deleted all tfstate files and ran terraform init again and voilá, it worked. Really don’t know why…