2

我最近将 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 = [
  {},
]

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

4

1 回答 1

0

这是一个对我来说很好的例子。

回购 1输出.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"]
}
于 2022-01-06T09:47:25.417 回答