Ano*_*ing 3 infrastructure amazon-web-services terraform
我正在尝试使用AWS上的Hashicorp Terraform为新项目设置一些IaC.我正在使用模块,因为我希望能够在多个环境(staging,prod,dev等)中重用东西.
我很难理解在模块中设置输出变量的位置,以及我如何在另一个模块中使用它.任何指向这一点将非常感谢!
在创建EC2机器时,我需要使用在我的VPC模块中创建的一些东西(子网ID).我的理解是你不能从另一个模块中引用某些东西,所以我试图使用VPC模块中的输出变量.
我的网站上有以下内容 main.tf
module "myapp-vpc" {
source = "dev/vpc"
aws_region = "${var.aws_region}"
}
module "myapp-ec2" {
source = "dev/ec2"
aws_region = "${var.aws_region}"
subnet_id = "${module.vpc.subnetid"}
}
Run Code Online (Sandbox Code Playgroud)
dev/vpc 只需设置一些值并使用我的vpc模块:
module "vpc" {
source = "../../modules/vpc"
aws_region = "${var.aws_region}"
vpc-cidr = "10.1.0.0/16"
public-subnet-cidr = "10.1.1.0/24"
private-subnet-cidr = "10.1.2.0/24"
}
Run Code Online (Sandbox Code Playgroud)
在我的vpc main.tf中,我aws_vpc和最后的aws_subnet资源(显示子网资源)之后有以下内容:
resource "aws_subnet" "public" {
vpc_id = "${aws_vpc.main.id}"
map_public_ip_on_launch = true
availability_zone = "${var.aws_region}a"
cidr_block = "${var.public-subnet-cidr}"
}
output "subnetid {
value = "${aws_subnet.public.id}"
}
Run Code Online (Sandbox Code Playgroud)
当我运行时,terraform plan我收到以下错误消息:
错误:模块'vpc':"subnetid"不是模块"vpc"的有效输出
yda*_*coR 10
每次都需要明确地向上传递每个模块的输出.
例如,如果你想从嵌套在另一个模块下面的模块向屏幕输出一个变量,你需要这样的东西:
output "child_foo" {
value = "foobar"
}
Run Code Online (Sandbox Code Playgroud)
module "child" {
source = "path/to/child"
}
output "parent_foo" {
value = "${module.child.child_foo}"
}
Run Code Online (Sandbox Code Playgroud)
module "parent" {
source = "path/to/parent"
}
output "main_foo" {
value = "${module.parent.parent_foo}"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4818 次 |
| 最近记录: |