Score:0

Terraform - Unable to get subnet id

th flag

I'm trying to create RDS instance via terraform and I'm having trouble getting subnet id from AWS to create aws_db_subnet_group.

I've tried many methods such as:

data.aws_subnet.test_subnet.id

aws_subnet.test_subnet.id

but everytime I'm getting error: "A managed resource "aws_subnet" "test_subnet" has not been declared in the root module."

In the root location I've defined output variable:

  output "output" {
   value = {
    vpc_id              = module.vpc.vpc_data.vpc_id
    test_subnet_id      = module.vpc.vpc_data.test_subnet.*.id
  }
}

and I've also tried to use variable var.test_subnet_id but I was getting similar error:

An input variable with the name "test_subnet_id" has not been declared. This variable can be declared with a variable "test_subnet_id" {} block.

Does anyone knows what I'm doing wrong?

Score:0
id flag

How you created VPC and subnets.

If you created manually you can get all IDs using following:

data "aws_subnet_ids" "subnet-ids" {
  vpc_id = {VPC-ID}
}

If you created by TF like:

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

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

Subnet IDs:

aws_subnet.subnet2.id
aws_subnet.subnet2.id

To create aws_db_subnet_group you can use:

resource "aws_db_subnet_group" "default" {
  name       = "main"
  subnet_ids = data.aws_subnet_ids.subnet-ids
}

or

resource "aws_db_subnet_group" "default" {
  name       = "main"
  subnet_ids = [ aws_subnet.subnet2.id, aws_subnet.subnet2.id]
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.