Search code examples
amazon-web-servicesinterpolationterraform

Modify simple list/array in Terraform


I have a next list:

azs = ["us-east-1a", "us-east-1b", "us-east-1c"]

And I am using it during subnets creation. In names of subnets I would like to use short names like a, b, c so I need a list ["a", "b", "c"]. Obviously I need to generate it dynamically (in locals block for example) when azs will be set manually.

How to create such list with Terraform?


Solution

  • You can use the formatlist function here to format a list.

    It uses the string format syntax, taking n lists and returning a single list.

    So in your case you probably want something like:

    locals {
      azs = [
        "a",
        "b",
        "c",
      ]
    }
    
    output "azs" {
      value = "${formatlist("us-east-1%s", local.azs)}"
    }