Terraform is a powerful infrastructure-as-code tool that allows you to define and provision infrastructure resources. In some scenarios, you might need to transform a list of strings into a map of objects for your Terraform configurations. This article will guide you through the problem and its solution using Terraform’s for loop and local variables.

The Problem

Imagine you have a list of property names in Terraform, represented as strings, and you need to transform this list into a map of objects. Each item in your list should become a key-value pair in the map, where the key is the property name, and the value is another map or object with a specific structure, such as {"type": "string"}.

Your initial attempt, using a for loop, might result in a list of map objects, but your goal is to have a map of properties. Let’s explore the solution step by step.

The Solution

To achieve your goal of converting a list of strings into a map of objects in Terraform, follow these steps:

Step 1: Initialize list

Start by initializing your list of properties (strings) in Terraform. We’ll call this list local.prop-list.

locals {
  prop-list = ["prop1", "prop2", "prop3"]  # Add your list of property names here

In this example, we have a list prop-list containing property names: “prop1,” “prop2,” and “prop3.” You can modify this list according to your requirements.

Step 2: Use For Loop to Transform List into Map

Now, use a for loop to transform this list into a map of objects. Each object in the map will have the property name as the key and a map representing the property’s structure as the value.

locals {
  prop-map = { 
    for prop in local.prop-list : 
      prop => { "type" = "string" }

In this code, we are iterating through local.prop-list, and for each property name (prop), we create a key-value pair in the local.prop-map. The key is the property name, and the value is a map with a “type” key set to “string.”

After executing this code, local.prop-map will look like this:

  "prop1" = {
    "type" = "string"
  "prop2" = {
    "type" = "string"
  "prop3" = {
    "type" = "string"

Now, you have successfully converted the list of property names into a map of objects with the desired structure.

Usage Example

The resulting map of objects can then be used in various ways within your Terraform configurations. For example, you can use it to set resource property configurations or combine it with other variables and Terraform code.

resource "example_resource" "example" {
  property_map = local.prop-map

In this example, we’re using local.prop-map as a property within a resource configuration.


In summary, by leveraging Terraform’s for loop and local variables, you can easily transform a list of strings into a map of objects. This capability is particularly useful when you need to generate configurations dynamically or work with structured data in your Terraform code.

Microsoft MVP

Chris Pietschmann is a Microsoft MVP, HashiCorp Ambassador, and Microsoft Certified Trainer (MCT) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to large enterprises. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.
HashiCorp Ambassador Microsoft Certified Trainer (MCT) Microsoft Certified: Azure Solutions Architect