locals {
values = [1, 2, 3, 4, 5]
indexes = range(length(local.values)) # [0, 1, 2, 3, 4]
cumulative_sum_list = [
for i in local.indexes :
# For each index, sum the numbers from the start of the list to the current index
sum(slice(local.values, 0, i + 1))
]
}
output "cumulative_sum_list" {
value = local.cumulative_sum_list
description = "The cumulative sum of the values in the list"
}
您可以使用范围和切片函数的组合来获得所需的结果:
range(max)
:使用起始值(从0
到max - 1
)生成数字列表slice
:从列表中提取一些连续的元素例子:
跑步
terraform plan
: