Jananath Banuka
Jananath Banuka

Reputation: 3963

Terraform fix no matching subnet found for vpc with id vpc

I have the following terraform code and when I try to terraform apply --auto-approve it gives me this error

Terraform fix no matching subnet found for vpc with id vpc

I think this is because the data block is trying to get the Subnet ID's right after the subnets are being created. Because, after 1 minute or so, I can again do a terraform apply --auto-approve and it works fine.

How can I fix this problem?

# -------------------------------------------
# -------------------------------------------
# Create VPC
module "ecs_vpc" {
  source = "./modules/7_vpc"
}

# -------------------------------------------
# -------------------------------------------
# Create SUBNETS
module "ecs_subnets_public_1" {
  source = "./modules/8_subnet"
  SUBNET_CIDR = "10.0.128.0/18"
  VPC_ID = module.ecs_vpc.vpc_id
  SUBNET_TAGS = {"Name" : "terraform-subnet-public-1"}
}

module "ecs_subnets_private_1" {
  source = "./modules/8_subnet"
  SUBNET_CIDR = "10.0.192.0/18"
  VPC_ID = module.ecs_vpc.vpc_id  
  SUBNET_TAGS = {"Name" : "terraform-subnet-private-1"}
}

# -------------------------------------------
# -------------------------------------------
# Create IGW
module "ecs_igw" {
  source = "./modules/9_igw"
  IGW_TAGS = {"Name" : "terraform-igw"}
  VPC_ID = module.ecs_vpc.vpc_id
}

# -------------------------------------------
# -------------------------------------------
# Create EIP for NAT
module "nat_eip" {
  source = "./modules/10_eip"
  EIP_NETWORK_BRODER_GROUP_REGION = "us-east-2"
  EIP_TAGS = {"Name" : "terraform-nat-eip"}
}

# -------------------------------------------
# -------------------------------------------
# Create NAT
data "aws_subnet_ids" "public_1" {
  vpc_id = module.ecs_vpc.vpc_id

  tags = {
    Name = "*terraform-subnet-public-1" // or two filter by a unique word use; *private*
  }
}

output "public" {
  value = data.aws_subnet_ids.public_1.id
}

data "aws_subnet_ids" "private_1" {
  vpc_id = module.ecs_vpc.vpc_id

  tags = {
    Name = "*terraform-subnet-private-1" // or two filter by a unique word use; *private*
  }
}

output "private" {
  value = data.aws_subnet_ids.private_1.id
}

Thank you!

Upvotes: 2

Views: 3688

Answers (1)

Marcin
Marcin

Reputation: 238847

Since you are creating subnets in modules ecs_subnets_public_1 and ecs_subnets_private_1, you shoudn't use data source to get the information about these subnets. The ./modules/8_subnet module should return all the information that it wants to expose to the parent module through outputs as indicated in Module Composition of the TF docs. This is done using outout:

Output values to return results to the calling module, which it can then use to populate arguments elsewhere.

So your ./modules/8_subnet would have output for the subnet id. Something like this:

output "subnet_id" {
   value = aws_subnet.mysubnet.id
}

Then to access it in parent module you would use the following instead of the data source:

module.ecs_subnets_private_1.subnet_id

# and

module.ecs_subnets_public_1.subnet_id

Nevertheless, the likely reason why your data.aws_subnet_ids fail is because they probably run before the subnets are actually created. To fix that you would have to add depends_on:

data "aws_subnet_ids" "public_1" {
  vpc_id = module.ecs_vpc.vpc_id

  tags = {
    Name = "*terraform-subnet-public-1" // or two filter by a unique word use; *private*
  }

  depends_on = [module.ecs_subnets_public_1]
}

data "aws_subnet_ids" "private_1" {
  vpc_id = module.ecs_vpc.vpc_id

  tags = {
    Name = "*terraform-subnet-private-1" // or two filter by a unique word use; *private*
  }

  depends_on = [module.ecs_subnets_private_1]
}

Upvotes: 3

Related Questions