1

I'm trying to create a new AWS RDS instance thru Terraform, but I'm has been receiving a strange error as below snippet:

resource "aws_db_subnet_group" "sng_marketplace_stg" {
  name     = "sng-marketplace-stg"

  tags = { Name = "Marketplace Staging" }

  subnet_ids = [
    "${aws_subnet.sbn_1_us.id}",
    "${aws_subnet.sbn_2_us.id}"
  ]
}

resource "aws_db_instance" "marketplace_staging" {
  identifier                  = "db-marketplace-stg-staging"
  engine                      = "postgres"
  engine_version              = "12.4"
  # ...
  # omitted
  # ...
  db_subnet_group_name        = "${aws_db_subnet_group.sng_marketplace_stg.id}"
}

    1. The 1st group creates a subnet group called sng-marketplace-stg this step is ok, and TF can apply.
    1. The 2nd step raise error a Error creating DB Instance: DBSubnetGroupNotFoundFault: DBSubnetGroup 'sng-marketplace-stg' not found.

Well, when I access AWS Console RDS the subnet group has been created, as expected, but if I rerun the TF apply (or plan) the terraform show the correct change (create new RDS instance), but when applied, the error is raised again, like if the subnet group not exist.

Anybody saw this error before or any suggestion to fix it?

Thank 4 any help or direction to solution.


The complete error trace here:

Error: Error applying plan:

1 error occurred:
        * aws_db_instance.marketplace_staging: 1 error occurred:
        * aws_db_instance.marketplace_staging: Error creating DB Instance: DBSubnetGroupNotFoundFault: DBSubnetGroup 'sng-marketplace-stg' not found.
        status code: 404, request id: da567898-...
2
  • 1
    The code you've provided is correct. I don't see a reason why it would not work. Are you sure that the snippets you've given are representative of your actual code? Commented Dec 4, 2020 at 23:49
  • Yeah, the code are correct, but a reaseon (i don't know) the AWS <> Terraform not recognize the resource. Commented Dec 8, 2020 at 13:50

2 Answers 2

1

I had the same problem with my code, and I realized that you have to add the resource aws_db_parameter_group in your main.tf file.

Here's an example extracted from the official website of terraform

resource "aws_db_parameter_group" "education" {
  name   = "education"
  family = "postgres13"

  parameter {
    name  = "log_connections"
    value = "1"
  }
}

Then, I run terraform plan and terraform apply and everything works ok (make sure your IAM role has all permissions it needs, you can add them when appears error messages in terraform console related to permissions).

Good luck!

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

Comments

0

You have to create db parameter group.

resource "aws_db_parameter_group" "education" {
  name   = "education"
  family = "postgres13"

  parameter {
    name  = "log_connections"
    value = "1"
  }
}

Also you have to reference it while creating the db instance.

In your case.

resource "aws_db_instance" "marketplace_staging" {
  identifier                  = "db-marketplace-stg-staging"
  engine                      = "postgres"
  engine_version              = "12.4"
  # ...
  # omitted
  # ...
  db_subnet_group_name        = "${aws_db_subnet_group.sng_marketplace_stg.id}"
  parameter_group_name        = aws_db_parameter_group.education.name
}

Further more if you want to allow the traffic. Add a security group with referencing your vpc id.

resource "aws_security_group" "rds" {
  name   = "education_rds"
  vpc_id = module.vpc.vpc_id

  ingress {
    from_port   = 5432
    to_port     = 5432
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 5432
    to_port     = 5432
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "education_rds"
  }
}

and also reference that in creation of your database. In your case.

resource "aws_db_instance" "marketplace_staging" {
  identifier                  = "db-marketplace-stg-staging"
  engine                      = "postgres"
  engine_version              = "12.4"
  # ...
  # omitted
  # ...
  db_subnet_group_name        = "${aws_db_subnet_group.sng_marketplace_stg.id}"
  parameter_group_name        = aws_db_parameter_group.education.name
  vpc_security_group_ids      = [aws_security_group.rds.id]
}

Reference: https://github.com/hashicorp/learn-terraform-rds/blob/main/main.tf

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.