Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic block documentation bug - incorrect or unclear #23190

Closed
pgienger opened this issue Oct 25, 2019 · 2 comments
Closed

dynamic block documentation bug - incorrect or unclear #23190

pgienger opened this issue Oct 25, 2019 · 2 comments

Comments

@pgienger
Copy link

pgienger commented Oct 25, 2019

It would seem that the documentation around the dynamic blocks is wrong or at least confusing.

My specific case was using a vsphere_virtual_machine but I believe this is the core language doc at fault. There is a referenced bug terraform-provider-aws #8260 where it was stated to be fixed, with the proper format, but the current docs do not match.

In my environment, trying to run this block from the base module:

  extradisks  = [
    {
      name  = "disk10"
      size  = 16
      eager = false
      thin  = true
    }
  ]

which is passed into another module and used within a vm provision as so:

  dynamic "disk" {
    for_each = var.extradisks
    content {
      label            = disk.name
      size             = disk.size
      eagerly_scrub    = disk.eager
      thin_provisioned = disk.thin
      unit_number      = disk.unit_number
    }
  }

Would throw an error like in the referenced bug.

Error: Unsupported attribute

  on ..\..\modules\cent7\cent7.tf line 115, in resource "vsphere_virtual_machine" "tfdeploy":
 115:       thin_provisioned = disk.thin

This object does not have an attribute named "thin".

The way to make it work was this:

  dynamic "disk" {
    for_each = var.extradisks
    content {
      label            = disk.value.name
      size             = disk.value.size
      eagerly_scrub    = disk.value.eager
      thin_provisioned = disk.value.thin
      unit_number      = disk.value.unit_number
    }
  }

The official documentation bit here (https://www.terraform.io/docs/configuration/expressions.html) seems out of date, or at best confusing, as follows. In order to follow the fix as described above, this block in the documentation

resource "aws_security_group" "example" {
  name = "example" # can use expressions here

  dynamic "ingress" {
    for_each = var.service_ports
    content {
      from_port = ingress.value
      to_port   = ingress.value
      protocol  = "tcp"
    }
  }
}

should be something like this, making assumptions about the var.service_ports object:

resource "aws_security_group" "example" {
  name = "example" # can use expressions here

  dynamic "ingress" {
    for_each = var.service_ports
    content {
      from_port = ingress.value.from_port
      to_port   = ingress.value.to_port
      protocol  = "tcp"
    }
  }
}
@apparentlymart
Copy link
Contributor

Hi @pgienger! Thanks for reporting this, and sorry for the long delay in responding.

It seems like in the meantime someone has updated the current documentation with a different example that doesn't have the bug you mentioned here, so I'm going to close this. Thanks again!

@ghost
Copy link

ghost commented Dec 18, 2020

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.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants