【问题标题】:terraform is giving error Invalid value for module argumentterraform 给出错误模块参数的值无效
【发布时间】:2021-06-07 13:48:27
【问题描述】:

vpc 模块中的 Vpc.tf

  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"

     }
     
 }

VPC 模块中的 Output.tf

 output "vpc_id" {

    value= aws_vpc.main
}

子网模块中的变量.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"
  }
}

Terraform 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?

【问题讨论】:

    标签: amazon-web-services terraform terraform-provider-aws


    【解决方案1】:

    你必须改变这个输出:

    output "vpc_id" {
        value= aws_vpc.main
    }
    

    到这里:

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

    【讨论】:

      猜你喜欢
      • 2021-01-30
      • 2021-01-20
      • 2020-12-22
      • 2021-02-19
      • 2021-01-16
      • 1970-01-01
      • 2021-10-09
      • 2021-07-03
      • 1970-01-01
      相关资源
      最近更新 更多