John Fox
John Fox

Reputation: 917

Terraform error creating subnet dependency

I'm trying to get a documentdb cluster up and running and have it running from within a private subnet I have created.

Running the config below without the depends_on i get the following error message as the subnet hasn't been created:

Error: error creating DocDB cluster: DBSubnetGroupNotFoundFault: DB subnet group 'subnet-0b97a3f5bf6db758f' does not exist.
status code: 404, request id: 59b75d23-50a4-42f9-99a3-367af58e6e16

Added the depends on setup to wait for the subnet to be created but are running into an issue.

  cluster_identifier      = "my-docdb-cluster"
  engine                  = "docdb"
  master_username         = "myusername"
  master_password         = "mypassword"
  backup_retention_period = 5
  preferred_backup_window = "07:00-09:00"
  skip_final_snapshot     = true
  apply_immediately       = true
  db_subnet_group_name    = aws_subnet.eu-west-3a-private
  depends_on = [aws_subnet.eu-west-3a-private]
}

On running terraform apply I an getting an error on the config:

Error: error creating DocDB cluster: DBSubnetGroupNotFoundFault: DB subnet group 'subnet-0b97a3f5bf6db758f' does not exist.
status code: 404, request id: 8b992d86-eb7f-427e-8f69-d05cc13d5b2d

on main.tf line 230, in resource "aws_docdb_cluster" "docdb":
230: resource "aws_docdb_cluster" "docdb" 

Upvotes: 5

Views: 7710

Answers (2)

Ashish Bhatia
Ashish Bhatia

Reputation: 629

It seems value in parameter is wrong. db_subnet_group_name created somewhere else gives the output id/arn. So u need to use id value. although depends_on clause looks okie.

db_subnet_group_name    = aws_db_subnet_group.eu-west-3a-private.id

So that would be correct/You can try to use arn in place of id.

Thanks,

Ashish

Upvotes: 1

ydaetskcoR
ydaetskcoR

Reputation: 56877

A DB subnet group is a logical resource in itself that tells AWS where it may schedule a database instance in a VPC. It is not referring to the subnets directly which is what you're trying to do there.

To create a DB subnet group you should use the aws_db_subnet_group resource. You then refer to it by name directly when creating database instances or clusters.

A basic example would look like this:

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

resource "aws_subnet" "eu-west-3a" {
  vpc_id            = aws_vpc.example.id
  availability_zone = "a"
  cidr_block        = "10.0.1.0/24"

  tags = {
    AZ = "a"
  }
}

resource "aws_subnet" "eu-west-3b" {
  vpc_id            = aws_vpc.example.id
  availability_zone = "b"
  cidr_block        = "10.0.2.0/24"

  tags = {
    AZ = "b"
  }
}

resource "aws_db_subnet_group" "example" {
  name       = "main"

  subnet_ids = [
    aws_subnet.eu-west-3a.id,
    aws_subnet.eu-west-3b.id
  ]

  tags = {
    Name = "My DB subnet group"
  }
}

resource "aws_db_instance" "example" {
  allocated_storage    = 20
  storage_type         = "gp2"
  engine               = "mysql"
  engine_version       = "5.7"
  instance_class       = "db.t2.micro"
  name                 = "mydb"
  username             = "foo"
  password             = "foobarbaz"
  parameter_group_name = "default.mysql5.7"
  db_subnet_group_name = aws_db_subnet_group.example.name
}

The same thing applies to Elasticache subnet groups which use the aws_elasticache_subnet_group resource.


It's also worth noting that adding depends_on to a resource that already references the dependent resource via interpolation does nothing. The depends_on meta parameter is for resources that don't expose a parameter that would provide this dependency information directly only.

Upvotes: 6

Related Questions