Terraform: Print range of resources

Created on 17 Nov 2017  ยท  2Comments  ยท  Source: hashicorp/terraform

Assuming a resource with a count of 5, is there a way I can get the last 3 or the first 2 only?

config question

Most helpful comment

Hi @jonathan-kosgei,

If you want _only_ the first or last elements of a particular count, you can use the slice function to achieve that:

  value = "${slice(example.foo.*.id, 0, 2)}"
  # ...or...
  value = "${slice(example.foo.*.id, length(example.foo.*.id) - 3, length(example.foo.*.id))}"

Another option is to use the chunklist function, which splits the list into chunks of a given length, thus giving you access to both the first set _and_ all of the subsequent sets:

  value = "${chunklist(example.foo.*.id, 2)}"

Unfortunately the chunklist function can only work relative to the _start_ of the list, so a "right-aligned" chunking isn't possible in this case.

Please note that we use GitHub issues for tracking bugs and enhancements rather than for questions. While we may be able to help with certain simple problems here it's generally better to use one of the community forums where there are far more people ready to help, whereas github issues here are generally monitored only by our few core maintainers.

All 2 comments

Hi @jonathan-kosgei,

If you want _only_ the first or last elements of a particular count, you can use the slice function to achieve that:

  value = "${slice(example.foo.*.id, 0, 2)}"
  # ...or...
  value = "${slice(example.foo.*.id, length(example.foo.*.id) - 3, length(example.foo.*.id))}"

Another option is to use the chunklist function, which splits the list into chunks of a given length, thus giving you access to both the first set _and_ all of the subsequent sets:

  value = "${chunklist(example.foo.*.id, 2)}"

Unfortunately the chunklist function can only work relative to the _start_ of the list, so a "right-aligned" chunking isn't possible in this case.

Please note that we use GitHub issues for tracking bugs and enhancements rather than for questions. While we may be able to help with certain simple problems here it's generally better to use one of the community forums where there are far more people ready to help, whereas github issues here are generally monitored only by our few core maintainers.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

c4milo picture c4milo  ยท  3Comments

shanmugakarna picture shanmugakarna  ยท  3Comments

larstobi picture larstobi  ยท  3Comments

rjinski picture rjinski  ยท  3Comments

thebenwaters picture thebenwaters  ยท  3Comments