terraform: use replace() function in a list

1 min read | by Jordi Prats

To be able to replace substring in terraform we have the replace() function, but this function can only be applied to a string, not a list of strings. How do we replace, for example, the http for https in the following list of strings?

input = [ "http://systemadmin.es", "http://pet2cattle.com" ]

Even though HCL is a declarative language, we can use for to apply the replace() function to each item un out list:

output = [
            for item in var.input:
                replace(item, "http://", "https://")
        ]

The contents of the output variable are going to be:

output = [ "https://systemadmin.es", "https://pet2cattle.com" ]

Posted on 15/02/2021

Categories