Terraform: Sum function for interpolation

Created on 30 Jan 2018  ยท  13Comments  ยท  Source: hashicorp/terraform

Hello,

I have been looking at various interpolation function in Terraform and were not able to find basic sum function.

What I have is a list of ints: [5, 0, 1, 2] which specify number of nodes I need to create (They are the same type). What I would like to have is just a function that can just sum the list of ints.

Example:

variable "key_list" {
  type = list
  default = [5, 0, 1, 2]
}
resource "instance" "vm" {
  count                       = "${sum(key_list)}"
}

Would be a really nice function to have.

config enhancement

Most helpful comment

I concur that this would be useful @rf-vivanovs

All 13 comments

I concur that this would be useful @rf-vivanovs

While there is not a sum function and terraform 0.12 is not yet released, an alternative can be to use external_data ans python:
https://pypi.org/project/terraform_external_data/
This thought requires the executors to have python and terraform_external_data installed.
When terraform 0.12 is released of course, this will no longer be required and having external dependencies is not recommended.

When terraform 0.12 is released of course, this will no longer be required and having external dependencies is not recommended.

v0.12 is out, and it looks like this was not included, is that correct?

Struggling to overcome this myself and it feels like it should be so simple. Is there really no other way of doing this in version 0.12.2 ??
Had hoped I could get something like this to work but have failed so far.

for loop, create new value, add value to new and repeat
local.new_value = local.new_value + mynext_number

Would welcome your guidance.

This would be a really useful feature to have :+1:

Hi all!

The Terraform team at HashiCorp won't be able to work on this in the near future due to our focus being elsewhere, but we'd be happy to review a pull request if you or someone else has the time and motivation to implement it. Alternatively, if others would also like to see this implemented we'd encourage adding a :+1: upvote reaction to the original issue comment (not to _this_ comment), which we use as one of the inputs to prioritize work for the Terraform team.

(Please don't leave new _comments_ with :+1: and similar upvote messages, because they just create noise for those who are subscribed to the issue, and don't create any signal that the Terraform team can report on.)

A little workaround that works in 0.12:

locals {
  list = [2, 3, 5, 2]
  list_sum = length(flatten([
    for e in local.list : range(e)
  ]))
}

A little workaround that works in 0.12:

locals {
  list = [2, 3, 5, 2]
  list_sum = length(flatten([
    for e in local.list : range(e)
  ]))
}

Failed if values will be more 1024

The 1024 limit makes this workaround inappropriate for my use case, the goal of which is to calculate the total budget of a subset of an AWS organization's accounts.

here's my egregious hack:

  # this is an egregious hack, because terraform doesn't have sum()...  
  # the largest number that can be added to the sum is the length of (long)
  # make a 16K long string -- this should be as long as your largest expected number
  long_str = format("%16384s","")
  nums = [ 1, 2, 3, 10 ]
  sum = length(join( "", [for n in local.nums: substr(local.long_str,0,n)]))

Can someone review that please? https://github.com/hashicorp/terraform/pull/24551

The sum function will be included with the future Terraform 0.13 release. Thanks to @noahmercado for working on the PR (#24666).

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