如果terraform脚本使用具有输出的模块,则可以使用命令-module选项访问这些模块输出terraform output:
$ terraform output --help
Usage: terraform output [options] [NAME]
Reads an output variable from a Terraform state file and prints
the value. If NAME is not specified, all outputs are printed.
Options:
-state=path Path to the state file to read. Defaults to
"terraform.tfstate".
-no-color If specified, output won't contain any color.
-module=name If specified, returns the outputs for a
specific module
-json If specified, machine readable output will be
printed in JSON format
Run Code Online (Sandbox Code Playgroud)
如果我将该状态文件存储在S3或其他类似文件中,我可以使用terraform_remote_state数据提供程序引用主脚本的输出.
data "terraform_remote_state" "base_networking" {
backend = "s3"
config {
bucket = "${var.remote_state_bucket}"
region = "${var.remote_state_region}"
key = "${var.networking_remote_state_key}"
}
}
resource "aws_instance" "my_instance" {
subnets = "${data.terraform_remote_state.base_networking.vpc_id}"
}
Run Code Online (Sandbox Code Playgroud)
是否可以访问状态文件中存在的模块输出?我正在寻找类似"${data.terraform_remote_state.base_networking.module.<module_name>.<output>}"或类似的东西.
小智 8
是的,您可以从自己的模块访问远程状态输出.您只需要"传播"输出.
例如,假设你有这样的东西,你的base_networking基础设施,其中包含一个用于创建你的VPC的模块,你希望通过远程状态访问该VPC ID:
base_networking/
main.tf
outputs.tf
vpc/
main.tf
outputs.tf
Run Code Online (Sandbox Code Playgroud)
在base_networking/main.tf您使用您的base_networking/vpc模块创建您的VPC :
module "vpc" {
source = "./vpc"
region = "${var.region}"
name = "${var.vpc_name}"
cidr = "${var.vpc_cidr}"
}
Run Code Online (Sandbox Code Playgroud)
在base_networking/vpc/outputs.tf您的模块中,您有一个id输出:
output "id" {
value = "${aws_vpc.vpc.id}"
}
Run Code Online (Sandbox Code Playgroud)
在base_networking/outputs.tf你也有vpc_id上传播的输出module.vpc.id:
output "vpc_id" {
value = "${module.vpc.id}"
Run Code Online (Sandbox Code Playgroud)
有了这个,您现在可以vpc_id使用以下内容访问:
data "terraform_remote_state" "base_networking" {
backend = "s3"
config {
bucket = "${var.remote_state_bucket}"
region = "${var.remote_state_region}"
key = "${var.networking_remote_state_key}"
}
}
[...]
vpc_id = "${data.terraform_remote_state.base_networking.vpc_id}"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5280 次 |
| 最近记录: |