0

I am trying to create an RDS instance on AWS using terraform, and getting the following error

Is this code ok?

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}


resource "aws_subnet" "private-subnet1" {
  vpc_id = "${aws_vpc.main.id}"
  cidr_block = "10.0.1.0/24"
}

resource "aws_subnet" "private-subnet2" {
  vpc_id = "${aws_vpc.main.id}"
  cidr_block = "10.0.2.0/24"
}

resource "aws_db_subnet_group" "db-subnet" {
  name       = "DB subnet group"
  subnet_ids = ["${aws_subnet.private-subnet1.id}", "${aws_subnet.private-subnet2.id}"]
}


resource "aws_db_instance" "db" {
  allocated_storage    = "20"
  storage_type         = "gp2"
  engine               = "mysql"
  engine_version       = "5.7.22"
  instance_class       = "db.t2.micro"
  name                 = "mydb"
  username             = "admin"
  password             = "admin"
  parameter_group_name = "db-mysql"
  db_subnet_group_name = "db-subnet"
}

I get this error while I terraform apply

Error: Error creating DB Subnet Group: DBSubnetGroupDoesNotCoverEnoughAZs: DB Subnet Group doesn't meet availability zone coverage requirement. Please add subnets to cover at least 2 availability zones. Current coverage: 1
    status code: 400, request id: 1bc7f2db-3ad7-41d9-93d0-6cbe3c0adfec

  on terraform.tf line 24, in resource "aws_db_subnet_group" "db-subnet":
  24: resource "aws_db_subnet_group" "db-subnet" {



Error: Error creating DB Instance: DBSubnetGroupNotFoundFault: DBSubnetGroup 'db-subnet' not found.
    status code: 404, request id: a4264af9-c9ac-4241-993f-e8c62e348247

  on terraform.tf line 30, in resource "aws_db_instance" "db":
  30: resource "aws_db_instance" "db" {

1 Answer 1

5

The errors are pretty clear about the problems, so let's break them down.

The first error has to do with the fact that you aren't specifying an availability zone when you create each of your subnets. A db subnet group must span at least 2 az's for high availability and you're not specifying az's for your subnets so they're getting created in the same one. Add an availability_zone parameter to both subnets using different az's from whatever region you're using.

resource "aws_subnet" "private-subnet1" {
  vpc_id = "${aws_vpc.main.id}"
  cidr_block = "10.0.1.0/24"
  availability_zone = "<an az from your region>"
}

resource "aws_subnet" "private-subnet2" {
  vpc_id = "${aws_vpc.main.id}"
  cidr_block = "10.0.2.0/24"
  availability_zone = "<a different az from your region>"
}

The second error is related to the fact that in your aws_db_instance, you don't reference the subnet group resource that you are creating and instead you're specifying a hardcoded subnet group by name. Terraform isn't aware of the dependency and uses the non-existent one in parallel while trying to create the one you've defined. Change it to

db_subnet_group_name = "${aws_db_subnet_group.db-subnet.name}"

and that 2nd error will go away, then you can resolve the first issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.