github.com/hugorut/terraform@v1.1.3/website/docs/language/functions/range.mdx (about) 1 --- 2 page_title: range - Functions - Configuration Language 3 description: The range function generates sequences of numbers. 4 --- 5 6 # `range` Function 7 8 `range` generates a list of numbers using a start value, a limit value, 9 and a step value. 10 11 ```hcl 12 range(max) 13 range(start, limit) 14 range(start, limit, step) 15 ``` 16 17 The `start` and `step` arguments can be omitted, in which case `start` defaults 18 to zero and `step` defaults to either one or negative one depending on whether 19 `limit` is greater than or less than `start`. 20 21 The resulting list is created by starting with the given `start` value and 22 repeatedly adding `step` to it until the result is equal to or beyond `limit`. 23 24 The interpretation of `limit` depends on the direction of `step`: for a positive 25 step, the sequence is complete when the next number is greater than or equal 26 to `limit`. For a negative step, it's complete when less than or equal. 27 28 The sequence-building algorithm follows the following pseudocode: 29 30 ``` 31 let num = start 32 while num < limit: (or, for negative step, num > limit) 33 append num to the sequence 34 num = num + step 35 return the sequence 36 ``` 37 38 Because the sequence is created as a physical list in memory, Terraform imposes 39 an artificial limit of 1024 numbers in the resulting sequence in order to avoid 40 unbounded memory usage if, for example, a very large value were accidentally 41 passed as the limit or a very small value as the step. If the algorithm above 42 would append the 1025th number to the sequence, the function immediately exits 43 with an error. 44 45 We recommend iterating over existing collections where possible, rather than 46 creating ranges. However, creating small numerical sequences can sometimes 47 be useful when combined with other collections in collection-manipulation 48 functions or `for` expressions. 49 50 ## Examples 51 52 ``` 53 > range(3) 54 [ 55 0, 56 1, 57 2, 58 ] 59 60 > range(1, 4) 61 [ 62 1, 63 2, 64 3, 65 ] 66 67 > range(1, 8, 2) 68 [ 69 1, 70 3, 71 5, 72 7, 73 ] 74 75 > range(1, 4, 0.5) 76 [ 77 1, 78 1.5, 79 2, 80 2.5, 81 3, 82 3.5, 83 ] 84 85 > range(4, 1) 86 [ 87 4, 88 3, 89 2, 90 ] 91 92 > range(10, 5, -2) 93 [ 94 10, 95 8, 96 6, 97 ] 98 ``` 99 100 The `range` function is primarily useful when working with other collections 101 to produce a certain number of instances of something. For example: 102 103 ```hcl 104 variable "name_counts" { 105 type = map(number) 106 default = { 107 "foo" = 2 108 "bar" = 4 109 } 110 } 111 112 locals { 113 expanded_names = { 114 for name, count in var.name_counts : name => [ 115 for i in range(count) : format("%s%02d", name, i) 116 ] 117 } 118 } 119 120 output "expanded_names" { 121 value = local.expanded_names 122 } 123 124 # Produces the following expanded_names value when run with the default 125 # "name_counts": 126 # 127 # { 128 # "bar" = [ 129 # "bar00", 130 # "bar01", 131 # "bar02", 132 # "bar03", 133 # ] 134 # "foo" = [ 135 # "foo00", 136 # "foo01", 137 # ] 138 # } 139 ```