Home > Back-end >  Cannot provide RDS subnet through different terraform modules
Cannot provide RDS subnet through different terraform modules

Time:01-04

I am unable to create an RDS due to failure in creating a subnet. I have different modules that I use to create an AWS infrastructure.

The main ones that i am having trouble with is RDS an VPC, where in the first one i create the database:

rds/main.tf

resource "aws_db_parameter_group" "education" {
  name   = "education"
  family = "postgres14"

  parameter {
    name  = "log_connections"
    value = "1"
  }
}

resource "aws_db_instance" "education" {
  identifier             = "education"
  instance_class         = "db.t3.micro"
  allocated_storage      = 5
  engine                 = "postgres"
  engine_version         = "14.1"
  username               = "edu"
  password               = var.db_password
  db_subnet_group_name   = var.database_subnets
  vpc_security_group_ids = var.rds_service_security_groups
  parameter_group_name   = aws_db_parameter_group.education.name
  publicly_accessible    = false
  skip_final_snapshot    = true
}

rds/variables.tf

variable "db_username" {
  description = "RDS root username"
  default = "someusername"
}

variable "db_password" {
  description = "RDS root user password"
  sensitive   = true
}

variable "vpc_id" {
  description = "VPC ID"
}

variable "rds_service_security_groups" {
  description = "Comma separated list of security groups"
}

variable "database_subnets" {
  description = "List of private subnets"
}

And the latter where i create the subnets and etc.

vpc/main.tf

resource "aws_subnet" "private" {
  vpc_id            = aws_vpc.main.id
  cidr_block        = element(var.private_subnets, count.index)
  availability_zone = element(var.availability_zones, count.index)
  count             = length(var.private_subnets)

  tags = {
    Name        = "${var.name}-private-subnet-${var.environment}-${format("d", count.index 1)}"
    Environment = var.environment
  }
}

resource "aws_subnet" "public" {
  vpc_id                  = aws_vpc.main.id
  cidr_block              = element(var.public_subnets, count.index)
  availability_zone       = element(var.availability_zones, count.index)
  count                   = length(var.public_subnets)
  map_public_ip_on_launch = true

  tags = {
    Name        = "${var.name}-public-subnet-${var.environment}-${format("d", count.index 1)}"
    Environment = var.environment
  }
}

resource "aws_subnet" "database" {
  vpc_id            = aws_vpc.main.id
  cidr_block        = element(var.database_subnets, count.index)
  availability_zone = element(var.availability_zones, count.index)
  count             = length(var.database_subnets)

  tags = {
    Name        = "Education"
    Environment = var.environment
  }
}

vpc/variables.tf

variable "name" {
  description = "the name of the stack"
}

variable "environment" {
  description = "the name of the environment "
}

variable "cidr" {
  description = "The CIDR block for the VPC."
}

variable "public_subnets" {
  description = "List of public subnets"
}

variable "private_subnets" {
  description = "List of private subnets"
}

variable "database_subnets" {
  description = "Database subnetes"
}

variable "availability_zones" {
  description = "List of availability zones"
}

Then in the root directory i have a main.tf file where i create everything. In there i call the rds module

main.tf

module "rds" {
  source                              = "./rds"
  vpc_id                              = module.vpc.id
  database_subnets                    = module.vpc.database_subnets
  rds_service_security_groups         = [module.security_groups.rds]
  db_password                         = var.db_password
}

The error that i keep getting is this

Error: Incorrect attribute value type

│

│   on rds\\main.tf line 19, in resource "aws_db_instance" "education":

│   19:   db_subnet_group_name   = var.database_subnets

│     ├────────────────

│     │ var.database_subnets is tuple with 2 elements

│

│ Inappropriate value for attribute "db_subnet_group_name": string required.

Any idea how i can fix it?

CodePudding user response:

You are trying to pass a list of DB Subnets into a parameter that takes a DB Subnet Group name.

You need to modify your RDS module to create a DB Subnet Group with the given subnet IDs, and then pass that group name to the instance:

resource "aws_db_subnet_group" "education" {
  name       = "education"
  subnet_ids = var.database_subnets
}

resource "aws_db_instance" "education" {
  identifier             = "education"
  db_subnet_group_name   = aws_db_subnet_group.education.name
  ...
}
  • Related