hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
41.76k stars 9.42k forks source link

How to not create a Internet Gateway/ Load Balancer Resource with hcl2 syntax #20365

Closed sudhaku closed 5 years ago

sudhaku commented 5 years ago

Terraform Version

Terraform v0.12.0-alpha4 (2c36829d3265661d8edbd5014de8090ea7e2a076)
+ provider.oci v3.9.0-3-ga5859820-dev
+ provider.template v1.0.0-5-g317c9c9-dev

Terraform Configuration Files

# Internet Gateway
resource "oci_core_internet_gateway" "InternetGateway" {
  count          = "${var.environment == "unstable" ? 0 : 1}"
  compartment_id = "${var.compartment_id}"
  vcn_id         = "${var.vcn_id}"
  display_name   = "internet-gateway"
}

resource "oci_core_route_table" "RouteTable" {
  count          = "${var.environment == "unstable" ? 0 : 1}"
  compartment_id = "${var.compartment_id}"
  vcn_id         = "${var.vcn_id}"
  display_name   = "route-table"

  route_rules {
    destination       = "0.0.0.0/0"
    network_entity_id = "${oci_core_internet_gateway.InternetGateway.id}"
  }
}

I get this error on terraform plan:

Error: Unsupported attribute

  on gateway_routes/gateway_routes.tf line 38, in resource "oci_core_route_table" "RouteTable":
  38:     network_entity_id = "${oci_core_internet_gateway.InternetGateway.id}"
    |----------------
    | oci_core_internet_gateway.InternetGateway is tuple with 1 element

This value does not have any attributes.

Tried changing the template like below:

resource "oci_core_route_table" "RouteTable" {
  count          = "${var.environment == "unstable" ? 0 : 1}"
  compartment_id = "${var.compartment_id}"
  vcn_id         = "${var.vcn_id}"
  display_name   = "route-table"

  route_rules {
    destination       = "0.0.0.0/0"
    network_entity_id = "${oci_core_internet_gateway.InternetGateway[0].id}"
  }
}

Now see this error on terraform plan:

 on gateway_routes/gateway_routes.tf line 38, in resource "oci_core_route_table" "RouteTable":
  38:     network_entity_id = oci_core_internet_gateway.InternetGateway[0].id

Resource oci_core_internet_gateway.InternetGateway does not have either
"count" or "for_each" set, so it cannot be indexed.

How can I fix this issue?

sudhaku commented 5 years ago

If I create new InternetGateway resources, the same code works just fine.

resource "oci_core_internet_gateway" "InternetGateway1" {
  count          = "${var.environment == "unstable" ? 0 : 1}"
  compartment_id = "${var.compartment_id}"
  vcn_id         = "${var.vcn_id}"
  display_name   = "internet-gateway1"
}

resource "oci_core_route_table" "RouteTable" {
  count          = "${var.environment == "unstable" ? 0 : 1}"
  compartment_id = "${var.compartment_id}"
  vcn_id         = "${var.vcn_id}"
  display_name   = "route-table"

  route_rules {
    destination       = "0.0.0.0/0"
    network_entity_id = "${oci_core_internet_gateway.InternetGateway1[0].id}"
  }
}

So it seems like new syntax is not working for existing state. I faced same issue with resources like load_balancer and load_balancer_backendset.

jbardin commented 5 years ago

Hi @sudhaku,

Thanks for filing the issue. The alpha4 release is now quite out of date. If you're testing providers, it's recommended to use the latest master branch.

The error you show has been updated for this situation, and looks more like:

Error: Missing resource instance key

  on main.tf line 12, in resource "null_resource" "b":
  12:     network_entity_id = oci_core_nat_gateway.InternetGateway.id

Because oci_core_nat_gateway.InternetGateway has "count" set, its attributes must be accessed on
specific instances.

For example, to correlate with indices of a referring resource, use:
    oci_core_nat_gateway.InternetGateway[count.index]

And this issue of referencing a disabled resource is not reproducible on master. If you still have an problem with the current version, feel free to update this and we can re-open the issue.

ghost commented 4 years ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.