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