Terraform giving subnet error while creating AWS RDS instance

Viewed 888

I am creating a AWS RDS read replica through terraform and while running apply I am getting an error:

Error: Error creating DB Subnet Group: InvalidParameterValue: Some input subnets in :[subnet-0****a, subnet-0****d] are invalid.
        status code: 400, request id: 6c*****

  on .terraform/modules/rds_replica/main.tf line 140, in resource "aws_db_subnet_group" "db_subnet_group":
 140: resource "aws_db_subnet_group" "db_subnet_group" {

The two subnets are my public subnets in my vpc. They are in 2 different AZ's 1a and 1b

This is what comes when I run terraform plan:

  # module.rds_replica.aws_db_subnet_group.db_subnet_group[0] will be created
  + resource "aws_db_subnet_group" "db_subnet_group" {
      + arn         = (known after apply)
      + description = "Database subnet group for app-replica"
      + id          = (known after apply)
      + name        = (known after apply)
      + name_prefix = "app-replica-"
      + subnet_ids  = [
          + "subnet-0****a",
          + "subnet-0****d",
        ]
    }

This is part of my rds.tf code:

module "rds_replica" {
  source = "git@github.com:*****"

  providers = {
    aws = aws.west
  }

  read_replica = "true"
  # point to the main instance's ARN
  source_db = "arn****"

  name           = var.rds_name_app_replica
  engine         = var.rds_engine_app_replica
  engine_version = var.rds_engine_version_app_replica
  family         = var.rds_family_app_replica
  instance_class = var.rds_instance_class_app_replica

  # NOTE: Using same password as primary 'rds_app' instance generated above
  password                   = ""
  port                       = var.rds_port_app_replica
  security_groups            = [aws_security_group.rds_app.id]
  subnets                    = [module.vpc.public_subnets]
  auto_minor_version_upgrade = var.rds_auto_minor_version_upgrade_app_replica
  backup_retention_period    = var.rds_backup_retention_period_app_replica

This is part of module used:

resource "aws_db_subnet_group" "db_subnet_group" {
  count = var.create_subnet_group ? 1 : 0

  description = "Database subnet group for ${var.name}"
  name_prefix = "${var.name}-"
  subnet_ids  = var.subnets[0]
  tags        = merge(var.tags, local.tags)

  lifecycle {
    create_before_destroy = true
  }
}

This is part of the vpc module:

################
# Public subnet
################
resource "aws_subnet" "public" {
  count = var.create_vpc && length(var.public_subnets) > 0 && (false == var.one_nat_gateway_per_az || length(var.public_subnets) >= length(var.azs)) ? length(var.public_subnets) : 0

  vpc_id                          = local.vpc_id
  cidr_block                      = element(concat(var.public_subnets, [""]), count.index)
  availability_zone               = length(regexall("^[a-z]{2}-", element(var.azs, count.index))) > 0 ? element(var.azs, count.index) : null
  availability_zone_id            = length(regexall("^[a-z]{2}-", element(var.azs, count.index))) == 0 ? element(var.azs, count.index) : null
  map_public_ip_on_launch         = var.map_public_ip_on_launch
  assign_ipv6_address_on_creation = var.public_subnet_assign_ipv6_address_on_creation == null ? var.assign_ipv6_address_on_creation : var.public_subnet_assign_ipv6_address_on_creation

  ipv6_cidr_block = var.enable_ipv6 && length(var.public_subnet_ipv6_prefixes) > 0 ? cidrsubnet(aws_vpc.this[0].ipv6_cidr_block, 8, var.public_subnet_ipv6_prefixes[count.index]) : null

  tags = merge(
    {
      "Name" = format(
        "%s-${var.public_subnet_suffix}-%s",
        var.name,
        element(var.azs, count.index),
      )
    },
    var.tags,
    var.public_subnet_tags,
  )
}

These I have verified that the subnets are valid and in my AWS console and also associated with the master db! I do not understand why?

1 Answers

To fix the problem:

  • I had to create a new VPC in us-west-1 with new subnet.. then reference them to the new read replica I wanted to create in us-west-1.
Related