This is part of an ongoing series of posts documenting the built-in interpolation functions in Terraform. For more information, check out the beginning post. In this post I am going to cover the slice() function. The example file is on GitHub here.
Function name: slice(list, start, end)
Returns: Takes a list, a starting index, and an ending index for the list. Returns a list that starts with the element found at the start value index and ends with the last element found in the list preceding the end value index.
Example:
# Returns [two, three]
output "slice_output" {
value = "${slice(list("one","two","three","four"),1,3)}"
}
##############################################
# Function: slice
##############################################
##############################################
# Variables
##############################################
variable "string_list" {
default = ["One", "Two", "Three", "Four"]
}
variable "int_list" {
default = [41, 42, 43]
}
variable "empty_string_list" {
default = ["", "", ""]
}
variable "empty_list" {
default = []
}
variable "bool_list" {
default = [false, true]
}
##############################################
# Resources
##############################################
##############################################
# Outputs
##############################################
#Test a standard list
output "1_slice_list" {
value = "${slice(var.string_list,0,1)}"
}
#Test length
output "1b_slice_list" {
value = "${slice(var.string_list,1,length(var.string_list)-1)}"
}
#Test no items
output "1c_slice_list" {
value = "${slice(var.string_list,0,0)}"
}
#Test a standard int list
output "2_int_list" {
value = "${slice(var.int_list,1,3)}"
}
#Test list of empty strings
output "3_empty_string_list" {
value = "${slice(var.empty_string_list,0,2)}"
}
#Test what will happen with a list boolean values
output "4_bool_list" {
value = "${slice(var.bool_list,0,2)}"
}
Run the following from the slice folder to get example output for a number of different cases:
#All examples are in variables
terraform apply
Slice is something I’ve used when I only want a subset of values from a list. A good example is when provisioning resources in AWS AZs. You can get the full list of AZs, which might have six entries, but you only want to deploy in two of those. You can use the slice function to get a list of only the first two AZs and use those as arguments for other resources.
The slice function includes the value found at the starting index, but not the value found at the ending index. That can lead to an odd scenario where you have to give slice an index that doesn’t exist in the list. For instance, a list with three elements [1,2,3] has an maximum index of 2. If I want a slice to include the last element, I would need to use the following: slice(list(1,2,3),1,3))
And I would get the list [2,3] back. The index 3 doesn’t exist, but I need to submit it as a value so I get back the last element in the list. It actually makes things easier if you don’t know the list length. You can do something like this: slice(var.list,1,length(var.list))
To get the elements starting at index 1 through the remainder of the list. This was a design decision that someone at HashiCorp made, and while I don’t agree with it, I understand the choice. I think the start and end should both be inclusive, as that is what I would assume. The substr function works the same way. If you feed slice the same start and end value, then you get back nothing.
Coming up next is the sort() function. Sadly, there is no sprite function.
October 18, 2024
What's New in the AzureRM Provider Version 4?
August 27, 2024
Debugging the AzureRM Provider with VSCode
August 20, 2024
State Encryption with OpenTofu
August 1, 2024