Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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": [

            ]
        }
    ]
}
like image 990
carlitos081 Avatar asked Aug 22 '18 10:08

carlitos081


People also ask

How do I configure the Amazon RDS subnet group in TerraForm?

The Subnet Group in Amazon RDS can be configured in Terraform with the resource name aws_db_subnet_group. The following sections describe 3 examples of how to use the resource and its parameters. Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

What is AWS RDS dbsubnetgroup?

The AWS::RDS::DBSubnetGroup resource creates a database subnet group. Subnet groups must contain at least two subnets in two different Availability Zones in the same region. For more information, see Working with DB subnet groups in the Amazon RDS User Guide. What is AWS Amazon RDS Subnet Group?

What are the requirements for the DB subnet group name?

The name for the DB subnet group. This value is stored as a lowercase string. Must contain no more than 255 letters, numbers, periods, underscores, spaces, or hyphens. Must not be default. First character must be a letter. The description for the DB subnet group. The EC2 Subnet IDs for the DB subnet group. "string" "string" ...

Why is my terraform not working?

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. Thanks for contributing an answer to Stack Overflow!


Video Answer


2 Answers

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.

like image 106
nmarchini Avatar answered Oct 12 '22 23:10

nmarchini


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]
}
like image 25
pst Avatar answered Oct 12 '22 23:10

pst