我正在尝试学习 terraform 模块的基础知识,这就是我在 terraform 中创建的资源、网络和计算模块,现在想将 vm_id 的输出传递给站点恢复模块,这是我目前正在使用的文件。
这是我目前正在关注的目录结构,
.
├── main.tf
├── variables.tf
├── terraform.tfvars
└── modules
├── instance
│ ├── main.tf
│ ├── outputs.tf
│ ├── variable.tf
在 main.tf 中,我创建 aws_vpc 资源,这是我的实例模块的输入 下面的 main.tf 工作正常,我在 terraform 模块块中明确定义值
# Configure the AWS Provider
provider "aws" {
region = "us-east-1"
}
# Create a VPC
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
tags = {
Name = "dev-vpc"
}
}
output aws_vpc_id {
value = aws_vpc.main.id
}
module myinstance {
source = "./modules/instance"
vpc_id = aws_vpc.main.id
cidr_block = "10.0.0.0/24"
map_public_ip_on_launch = true
instance_name = "calypso"
ami = "ami-09c92d3eb1db3a728"
instance_type = "t3.micro"
}
但是,当我尝试将这些值移动到“terraform.tfvars”时,我的 terrafom 计划无法加载 vpc id 所以下面的 main.tf 无法从同一个 main.tf 中查找 vpc id
# Configure the AWS Provider
provider "aws" {
region = "us-east-1"
}
# Create a VPC
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
tags = {
Name = "dev-vpc"
}
}
output aws_vpc_id {
value = aws_vpc.main.id
}
module "myinstance" {
source = "./modules/instance"
vpc_id = aws_vpc.main.id
cidr_block = var.cidr_block
map_public_ip_on_launch = var.map_public_ip_on_launch
instance_name = var.instance_name
ami = var.ami
instance_type = var.instance_type
}
我不太明白为什么第二种方法不起作用
---编辑1---所以这是我在运行有问题的main.tf terraform plan 或应用时得到的结果-由于某种原因,它无法检索在同一main.tf 中创建的vpc id
$ terraform plan
var.vpc_id
Enter a value:
这是我的变量.tf
variable "vpc_id" {
}
variable "cidr_block" {
type = string
description = "cidr block for subnet"
}
variable "map_public_ip_on_launch" {
type = bool
description = "if set to true, it will map public ip on launch"
default = true
}
variable "instance_name" {
type = string
description = "name of your instance"
}
variable "ami" {
type = string
description = "ami for your instance"
}
variable "instance_type" {
type = string
description = "type of your instance"
}
这是我的 terraform.tfvars
cidr_block = "10.0.0.0/24"
map_public_ip_on_launch = true
instance_name = "calypso"
ami = "ami-09c92d3eb1db3a728"
instance_type = "t3.micro"