CodexBloom - Programming Q&A Platform

Terraform: implementing Managing Multiple Databases in Aurora Serverless with VPC Configuration

👀 Views: 72 💬 Answers: 1 📅 Created: 2025-06-11
terraform aws aurora vpc HCL

I'm having a hard time understanding Hey everyone, I'm running into an issue that's driving me crazy... I'm currently working with scenarios in configuring multiple Aurora Serverless databases within the same VPC using Terraform. I have defined a module for creating the databases, but I'm working with issues with the VPC settings, which seem to conflict when deploying. Specifically, I'm using Terraform version 1.4.0 and the AWS provider version 4.0.0. Here’s the relevant part of my Terraform configuration: ```hcl module "aurora_db" { source = "./modules/aurora_db" db_identifier = var.db_identifier engine = "aurora" engine_mode = "serverless" vpc_security_group_ids = [aws_security_group.db_sg.id] db_subnet_group_name = aws_db_subnet_group.main.name } resource "aws_db_subnet_group" "main" { name = "my-db-subnet-group" subnet_ids = var.subnet_ids } resource "aws_security_group" "db_sg" { name = "db_security_group" vpc_id = var.vpc_id ingress { from_port = 3306 to_port = 3306 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } } ``` I created two instances of the `aurora_db` module, but when I apply the configuration, I get an behavior stating: ``` behavior: InvalidParameterValue: VPC security group 'sg-0123456789abcdef0' must belong to the same VPC as the DB subnet group 'my-db-subnet-group'. ``` I’ve double-checked that the security group is indeed created in the correct VPC, and the subnet IDs referenced in the `aws_db_subnet_group` resource belong to the same VPC. I’ve also tested the configuration with different VPCs, but the scenario continues. I’ve tried separating the security group configuration into its own module, but it didn't resolve the scenario either. I’m not sure if there’s a deeper underlying scenario with how Terraform is handling resource dependencies or if I'm missing something in the way I’m structuring the module calls. Any guidance on resolving this would be greatly appreciated! I'm working on a API that needs to handle this. Thanks for taking the time to read this!