terraform 数据源输出到文件

terraform data source output to file

我想尝试一下 terraform 数据源是否能够将输出放置到文本文件中。 我在网上查看但找不到任何内容,我计划执行获取负载均衡器名称,然后我们的自动化脚本将执行 aws-cli 命令并将使用数据源采用的负载均衡器名称

如果您的俱乐部名称是由 TF 自动生成的,您可以使用 local_file:

将其保存在文件中
resource "aws_elb" "clb" {
  availability_zones = ["ap-southeast-2a"]

  listener {
    instance_port     = 8000
    instance_protocol = "http"
    lb_port           = 80
    lb_protocol       = "http"
  }
  
}

resource "local_file" "foo" {
    content     = <<-EOL
    ${aws_elb.clb.name}
    EOL
    filename = "${path.module}/clb_name.txt"
}

output "clb_name" {
  value = aws_elb.clb.name
}

但也许直接获取输出值会更容易 json:

clb_name=$(terraform output -json clb_name | jq -r)
echo ${clb_name}