0

我有一个问题,我需要创建多个 VCN。

我想将变量设置为 JSON 文件。像这样:

init_values.json

{
  "terraform": {
    "tenancy_ocid": "ocid1.ten.xxxxxxxxxxxxxxxxxx",
    "user_ocid": "ocid1.user..xxxxxxxxxxxxxxxxxx",
    "private_key_path": "/Users/user/.oci/oci_api_key.pem",
    "fingerprint": "a8:8e:.xxxxxxxxxxxxxxxxxx",
    "region": "eu-frankfurt-1"
  },
  "vcn": [
    {
      "name": "vcn_1",
      "cidr": "44.144.224.0/25"
    },
    {
      "name": "vcn_2",
      "cidr": "44.144.224.128/25"
    }
  ]
}

我的vcn.tf文件就像这样

locals {
  vcn_data = jsondecode(file("${path.module}/init_values.json"))

  all_vcn = [for my_vcn in local.vcn_data.vcn : my_vcn.name ]
  all_cidr = [for my_cidr in local.vcn_data.vcn : my_cidr.cidr ]
}


resource "oci_core_vcn" "these" {
  compartment_id  = local.json_data.COMPARTMENT.root_compartment
  display_name = local.all_vcn
  cidr_block = local.all_cidr
}

provider.tf是:

provider "oci" {
  //alias             = "home"
  tenancy_ocid      = local.json_data.TERRAFORM.tenancy_ocid
  user_ocid         = local.json_data.TERRAFORM.user_ocid
  private_key_path  = local.json_data.TERRAFORM.private_key_path
  fingerprint       = local.json_data.TERRAFORM.fingerprint
  region            = local.json_data.TERRAFORM.region
}

错误是下一个:

│ Error: Incorrect attribute value type
│ 
│   on vcn.tf line 39, in resource "oci_core_vcn" "these":
│   39:   display_name = local.all_vcn
│     ├────────────────
│     │ local.all_vcn is tuple with 2 elements
│ 
│ Inappropriate value for attribute "display_name": string required.
╵

什么可能是我的错误,我错了

谢谢

4

1 回答 1

0

可能,而不是:

all_cidr = [for my_cidr in local.vcn_data.vcn : my_vcn.cidr ]

它应该是:

all_cidr = [for my_cidr in local.vcn_data.vcn : my_cidr.cidr ]

更新

您必须使用countfor_each创建多个 vcn:

resource "oci_core_vcn" "these" {
  
  count = length(local.all_vcn)

  compartment_id  = local.json_data.COMPARTMENT.root_compartment

  display_name = local.all_vcn[each.index]
  cidr_block = local.all_cidr[each.index]
}
于 2021-12-02T01:19:10.027 回答