Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
791 views
in Technique[技术] by (71.8m points)

amazon web services - 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!

question from:https://stackoverflow.com/questions/65868672/terraform-fix-no-matching-subnet-found-for-vpc-with-id-vpc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...