'Create aws_db_subnet_group with terraform throw Error creating DB Subnet Group: InvalidParameterValue

I'm using terraform to create an RDS instance, to do so I need to have an aws_db_subnet_group resource. I can create the RDS using an aws_db_subnet_group created by the Amazon website but when I try to create the aws_db_subnet_group from terraform script I receive the error InvalidParameterValue.

This is the terraform script:

resource "aws_db_subnet_group" "default" {
  name        = "cse-cr"
  description = "Private subnets for RDS instance"
  subnet_ids  = ["subnet-0c8764fcb28b04c8c", "subnet-0ca53ff9b621e2c89"]
}

and this is the error:

 Error: Error applying plan:

1 error(s) occurred:

* aws_db_subnet_group.default: 1 error(s) occurred:

* aws_db_subnet_group.default: Error creating DB Subnet Group: InvalidParameterValue: Some input subnets in :[subnet-0ca53ff9b621e2c89, subnet-0c8764fcb28b04c8c] are invalid.
        status code: 400, request id: 66166ec8-9b79-41d3-bdf7-a5cdb66f5f95

Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.

I have seen multiple example on internet and the only difference I can see is that I'm using 2 preexisting subnet created from someone else and not from my Terraform script.

result of the command:

aws ec2 describe-subnets --subnet-ids subnet-0ca53ff9b621e2c89 subnet-0c8764fcb28b04c8c

{
    "Subnets": [
        {
            "AvailabilityZone": "us-east-1a",
            "AvailableIpAddressCount": 250,
            "CidrBlock": "10.112.173.0/24",
            "DefaultForAz": false,
            "MapPublicIpOnLaunch": false,
            "State": "available",
            "SubnetId": "subnet-0ca53ff9b621e2c89",
            "VpcId": "vpc-0ec46ccebc8108670",
            "AssignIpv6AddressOnCreation": false,
            "Ipv6CidrBlockAssociationSet": [],
            "Tags": [

            ]
        },
        {
            "AvailabilityZone": "us-east-1b",
            "AvailableIpAddressCount": 251,
            "CidrBlock": "10.112.174.0/24",
            "DefaultForAz": false,
            "MapPublicIpOnLaunch": false,
            "State": "available",
            "SubnetId": "subnet-0c8764fcb28b04c8c",
            "VpcId": "vpc-0ec46ccebc8108670",
            "AssignIpv6AddressOnCreation": false,
            "Ipv6CidrBlockAssociationSet": [],
            "Tags": [

            ]
        }
    ]
}


Solution 1:[1]

Try using the aws_vpc data source first like this:

data "aws_subnet" "subnet1" {
  id = "subnet-0c8764fcb28b04c8c"
}

data "aws_subnet" "subnet2" {
  id = "subnet-0ca53ff9b621e2c89"
}

resource "aws_db_subnet_group" "default" {
  name        = "cse-cr"
  description = "Private subnets for RDS instance"
  subnet_ids  = [data.aws_subnet.subnet1.id, data.aws_subnet.subnet2.id]
}

Solution 2:[2]

You may want to check what region you have your terraform defaulting to, as it might be erroring if the code is executing against the wrong region.

Solution 3:[3]

Ensure all your resources are using the same provider in the region, the subnets should belong to that region

provider "aws" {
    region = "us-east-2"
}

resource "aws_db_subnet_group" "default" {
    name        = "cse-cr"
    description = "Private subnets for RDS instance"
    provider    = aws
    subnet_ids  = ["subnet-0c8764fcb28b04c8c", "subnet-0ca53ff9b621e2c89"]
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pst
Solution 2 Yunnosch
Solution 3 PELUKA Carlos Andres Valencia