terraform 给出错误 Invalid value for module argument

terraform is giving error Invalid value for module argument

Vpc.tf 在 vpc 模块中

  resource "random_id" "generic" {
    
      byte_length = 32
      prefix= "${var.vpc_name}-"
    }

 
 resource "aws_vpc" "main"{

     cidr_block = var.vpc_cidr_block
     tags= {
        
        Name= "test_vpc"

     }
     
 }

Output.tf 在 VPC 模块中

 output "vpc_id" {

    value= aws_vpc.main
}

variable.tf 子网模块

 variable "vpc_id" {

    type = string
}

subnets.tf 子网模块

resource "aws_subnet" "terraform_pub_sub"{


    cidr_block              = var.public_subnet_cidr_block
    availability_zone       = "us-east-1a"
    map_public_ip_on_launch = true
    vpc_id=var.vpc_id


  tags = {
    Project = "terra_tutorial"
  }
}

地形 provider.tf

  provider "aws" {

    region = var.region
}

module "vpc_module"{

    source="./modules/network/vpc"
    vpc_name= "test_vpc"
    vpc_cidr_block="172.17.32.0/19"

}

module "subnet_module"{

    source = "./modules/network/subnets"
    public_subnet_cidr_block ="172.17.1.0/24"
    vpc_id= module.vpc_module.vpc_id
    
}

在 运行 terraform 计划之后出现以下错误

 Error: Invalid value for module argument

  on providers.tf line 18, in module "subnet_module":
  18:     vpc_id= module.vpc_module.vpc_id

The given value is not suitable for child module variable "vpc_id" defined at
modules\network\subnets\variables.tf:3,1-18: string required.

如何使用模块为 vpc_id 赋值?如果输出返回对象,我怎样才能得到 vpc_id?

您必须更改此输出:

output "vpc_id" {
    value= aws_vpc.main
}

对此:

output "vpc_id" {
    value= aws_vpc.main.id
}