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

Sum function for interpolation #17239

Closed
rf-vivanovs opened this issue Jan 30, 2018 · 13 comments
Closed

Sum function for interpolation #17239

rf-vivanovs opened this issue Jan 30, 2018 · 13 comments

Comments

@rf-vivanovs
Copy link

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.

@livmackintosh
Copy link

I concur that this would be useful @rf-vivanovs

@svyotov
Copy link

svyotov commented Jan 8, 2019

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.

@svangordon
Copy link

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?

@spaceygithub
Copy link

spaceygithub commented Jun 17, 2019

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.

@AlexTawse
Copy link

This would be a really useful feature to have 👍

@teamterraform
Copy link
Contributor

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 👍 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 👍 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.)

@dee-kryvenko
Copy link

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)
  ]))
}

@kino505
Copy link

kino505 commented Nov 5, 2019

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

@andrewsolomon
Copy link

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.

@pryankster
Copy link

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)]))

@dee-kryvenko
Copy link

Can someone review that please? #24551

@alisdair
Copy link
Contributor

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

@ghost
Copy link

ghost commented May 16, 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 and limited conversation to collaborators May 16, 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