【问题标题】:Use Terraform state output from another project使用来自另一个项目的 Terraform 状态输出
【发布时间】:2020-08-22 03:34:15
【问题描述】:

我最近将 Terraform 从 11.11 升级到 12.24,运行 0.12upgrade 后,访问远程 terraform_state 输出不再有效。

我得到的错误是: 此对象没有名为“subnet_id”的属性

这是我的配置:

Repo 1(创建网络、子网等)

network 
| main.tf 
| output.tf

output.tf 的内容(从上面):

output "subnet_ids" {
  value = openstack_networking_subnet_v2.openstack-subnet.*.id
}

output "network_ids" {
  value = openstack_networking_network_v2.openstack-networks.*.id
}

在上面的 repo 中运行 Terraform 输出,给了我以下输出(修改后的 id):

network_ids = [
  "08adfe73-dfg5-404d-958e-e8db73121531",
  "c0b561e5-320c-46b3-b328-98723f54ef82",
  "200eb570-b734-4b18-9250-6ckae90ea0e1",
  "84c43fc5-771c-4c79-8670-3d858788661e",
]
subnet_ids = [
  "f5df394f-d542-492a-a224-eefb998536ac",
  "f0f5d2fe-e83c-4041-971a-bba34870d5de",
  "89e966b1-826e-483d-b312-bb7aa9893a02",
  "76d6dfda-8161-4961-89a6-39aeeb82db3c",
]

Repo 2(创建计算平台)

infrastructure
| main.tf
  | modules 
    | compute
      | main.tf

计算的 main.tf 的内容:

...
data "terraform_remote_state" "base_networking_a" {
  backend = "s3"
  config = {
    bucket               = "terraform-state"
    workspace_key_prefix = "network-terraform-state/${var.environment}a"
    key                  = "terraform.tfstate"
    region  = "ap-southeast-2"
    profile = "aws_profile"
  }
}
...
resource "openstack_lb_loadbalancer_v2" "k8s-loadbalancer-a" {
  name          = var.loadbalancer_name
  vip_subnet_id = "data.terraform_remote_state.base_networking_a.outputs.subnet_ids[2]"
}

作为一种附加方法,当我使用具有以下内容的 terraform 文件输出内容时:

output "net" {
    value = data.terraform_remote_state.base_networking_a.outputs.*
}

然后我得到以下输出:

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

net = [
  {},
]

任何想法如何访问存储在远程状态中的这些值,或者我做错了什么?

【问题讨论】:

  • 你可以试试data.terraform_remote_state.base_networking_a.outputs吗?另外,您是否查看了远程状态文件以确认输出确实正确?
  • 谢谢安迪。是的 - 我已经尝试过(没有subnet_ids),但没有运气。是的,远程状态文件看起来很完美(带有适当的输出数组),如所列。
  • 您的vip_subnet_id 分配了一个名为data.terraform_remote_state.base_networking_a.outputs.subnet_ids[2] 的字符串,没有该输出的实际值。使用不带引号。

标签: terraform terraform-remote-state


【解决方案1】:

这是一个适合我的示例。

回购 1 output.tf

output "route53_zone_com_example_public_id" {
  description = "The Route53 Zone ID."
  value       = aws_route53_zone.com_example_public.id
}

回购 2 main.tf

data "terraform_remote_state" "route53_zones" {
  backend = "s3"
  config = {
    bucket = "tf-state-route53"
    key    = "zones/terraform.tfstate"
    region = "eu-west-1"
  }
}

resource "aws_route53_record" "com_example_template_A_1" {
  zone_id = data.terraform_remote_state.route53_zones.outputs.route53_zone_com_example_public_id
  name    = "template-example.example.com"
  type    = "A"
  ttl     = "300"
  records = ["127.0.0.1"]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多