Terraform Map and Object Patterns

Terraform variables implement both a map and an object type. They mostly work the same. The docs even say, “The distinctions are only useful when restricting input values for a module or resource.” They can be defined and accessed in several ways. There’s some automatic conversion back and forth between them.

This article distills these details into patterns you can copy and paste, while highlighting some of the subtleties.

Here’s the main detail you need:

Maps contain many things of one type. Objects contain a specific set of things of many types.

This is a simplification. It doesn’t cover all the behavior of terraform’s maps and objects (like loss that can happen in conversions back and forth between them), but it’s enough for the patterns you’re likely to need day to day.

Table of Contents

Style

Key Names

You can quote the key names in map definitions.

variable "quoted_map" {
  default = {
    "key_1" = "value_1"
    "key_2" = "value_2"
  }
}

But you don’t have to.

variable "unquoted_map" {
  default = {
    key_1 = "value_1"
    key_2 = "value_2"
  }
}

We prefer the unquoted format. Partly because the syntax is lighter and partly because it only works if key names are valid identifiers, so it forces us to use ones that are. If the key names are identifiers, the interior of maps look similar to the rest of our terraform variables, and we can also use a dotted notation for referencing them.

Commas

You can separate key/value pairs with commas.

variable "comma_map" {
  default = {
    key_1 = "value_1",
    key_2 = "value_2",
  }
}

But you don’t have to.

variable "no_comma_map" {
  default = {
    key_1 = "value_1"
    key_2 = "value_2"
  }
}

We prefer no commas because the syntax is lighter.

References

You can reference values by attribute name with quotes and square brackets.

output "brackets" {
  value = var.unquoted_map["key_2"]
}

But you can also use the dotted notation.

output "dots" {
  value = var.unquoted_map.key_2
}

We prefer the dotted notation because the syntax is lighter. This also requires the key names to be identifiers, but they will be if you use the unquoted pattern for defining them.

Patterns

  • Each pattern implements a map containing a value_2 string that we’ll read into an output.
  • Examples set values with variable default values, but they work the same with tfvars, etc.
  • The types of values in these examples are known, so they’re set explicitly. There’s also an any keyword for cases where you’re not sure. We recommend explicit types whenever possible.

Untyped Flat Map

This is the simplest pattern. We don’t recommend it. Use a typed map instead.

variable "untyped_flat_map" {
  default = {
    key_1 = "value_1"
    key_2 = "value_2"
  }
}
output "untyped_flat_map" {
  value = var.untyped_flat_map.key_2
}

Typed Flat Map

This is sufficient for simple cases.

variable "typed_flat_map" {
  default = {
    key_1 = "value_1"
    key_2 = "value_2"
  }
  type = map(string)
}
output "typed_flat_map" {
  value = var.typed_flat_map.key_2
}

With the type set, if a module mistakenly passes a value of the wrong type that our code wasn’t expecting, terraform throws an error.

variable "typed_flat_map_bad_value" {
  default = {
    key_1 = []
    key_2 = "value_2"
  }
  type = map(string)
}
│ Error: Invalid default value for variable
│ 
│   on main.tf line 49, in variable "typed_flat_map_bad_value":
│   49:   default = {
│   50:     key_1 = []
│   51:     key_2 = "value_2"
│   52:   }
│ 
│ This default value is not compatible with the variable's type constraint: element "key_1": string required.

except when it doesn’t. If we set key_1 to a number or boolean, it’ll be automatically converted to a string. This is generic terraform behavior. It’s not specific to maps.

Untyped Nested Map

We don’t recommend this, either. Use a typed nested map instead.

variable "untyped_nested_map" {
  default = {
    key_1 = "value_1"
    key_2 = {
      nested_key_1 = "value_2"
    }
  }
}
output "untyped_nested_map" {
  value = var.untyped_nested_map.key_2.nested_key_1
}

Typed Nested Map, Values are Same Type

Like the flat map, this pattern protects us against types of inputs the code isn’t written to handle. This only works when the values of the keys within each map all share the same type.

variable "typed_nested_map_values_same_type" {
  default = {
    key_1 = {
      nested_key_1 = "value_1"
    }
    key_2 = {
      nested_key_2 = "value_2"
    }
  }
  type = map(map(string))
}
output "typed_nested_map_values_same_type" {
  value = var.typed_nested_map_values_same_type.key_2.nested_key_2
}

Typed Nested Map, Values are Different Types

This is where the differences between maps and objects start to show up in implementations. Remembering our distillation of the docs from the start:

Maps contain many things of one type. Objects contain a specific set of things of many types.

variable "typed_nested_map_values_different_types" {
  default = {
    key_1 = "value_1"
    key_2 = {
      nested_key_1 = "value_2"
    }
  }
  type = object({
    key_1 = string,
    key_2 = map(string)
  })
}
output "typed_nested_map_values_different_types" {
  value = var.typed_nested_map_values_different_types.key_2.nested_key_1
}

In this nested map, one value is a string and the other is a map. That means we need an object to define the constraint. We can’t do it with just a map, because maps contain one type of value and we need two.

Flexible Number of Typed Nested Maps, Values are Different Types

This is the most complex case. It lets us read in a map that has an arbitrary number of nested maps like the ones above.

variable "flexible_number_of_typed_nested_maps" {
  default = {
    map_1 = {
      key_1 = "value_1"
      key_2 = {
        nested_key_1 = "value_2"
      }
    }
    map_2 = {
      key_1 = "value_3"
      key_2 = {
        nested_key_1 = "value_4"
      }
    }
  }
  type = map(
    object({
      key_1 = string,
      key_2 = map(string)
    })
  )
}
output "flexible_number_of_typed_nested_maps" {
  value = var.flexible_number_of_typed_nested_maps.map_1.key_2.nested_key_1
}

We could add a map_3 (or as many more as we wanted) without getting type errors. Again remembering our simplification:

Maps contain many things of one type. Objects contain a specific set of things of many types.

Inside, we use objects because their keys have values that are different types. Outside, we use a map because we want an arbitrary number of those objects.

The inside objects all have the same structure. They can be defined with the same type expression. That passes the requirement that maps contain all the same type of thing.

Happy automating!

Operating Ops

Need more than just this article? We’re available to consult.

You might also want to check out these related articles:

Terratest Good Practices: Table-Driven Tests

Hello!

Terratest is a common way to run integration tests against terraform modules. I use it on many of the modules I develop. If you haven’t used it before, check out its quickstart for an example of how it works.

For simple cases, the pattern in that quickstart is all you need. But, bigger modules mean more tests and pretty soon you can end up swimming in all the cases you have to define. Go has a tool to help: table-driven tests. Here’s what you need to get them set up for terratest (Dave Cheney also has a great article on them if you want to go deeper).

First, let’s look at a couple simple tests that aren’t table-driven:

package test

import (
	"testing"

	"github.com/gruntwork-io/terratest/modules/terraform"
	"github.com/stretchr/testify/assert"
)

func TestOutputsExample(t *testing.T) {
	terraformOptions := &terraform.Options{
		TerraformDir: ".",
	}
	defer terraform.Destroy(t, terraformOptions)
	terraform.InitAndApply(t, terraformOptions)

	one := terraform.Output(t, terraformOptions, "One")
	assert.Equal(t, "First.", one)
	two := terraform.Output(t, terraformOptions, "Two")
	assert.Equal(t, "Second.", two)
}

Easy. Just repeat the calls to terraform.Output and assert.Equal for each test and assert it’s what you expect. Not a problem, unless you have dozens or hundreds of tests. Then you end up with a lot of duplication.

You can de-duplicate the repeated calls by defining your test cases in a slice of structs (the “table”) and then looping over the cases. Similar to adaptive modeling. Like this:

package test

import (
	"testing"

	"github.com/gruntwork-io/terratest/modules/terraform"
	"github.com/stretchr/testify/assert"
)

func TestOutputsTableDrivenExample(t *testing.T) {
	terraformOptions := &terraform.Options{
		TerraformDir: ".",
	}
	defer terraform.Destroy(t, terraformOptions)
	terraform.InitAndApply(t, terraformOptions)

	outputTests := []struct {
		outputName    string
		expectedValue string
	}{
		{"One", "First."},
		{"Two", "Second."},
	}

	for _, testCase := range outputTests {
		outputValue := terraform.Output(t, terraformOptions, testCase.outputName)
		assert.Equal(t, testCase.expectedValue, outputValue)
	}
}

Now, there’s just one statement each for terraform.Output and assert.Equal. With only two tests it actually takes a bit more code to use a table, but once you have a lot of tests it’ll save you.

That’s it! That’s all table-driven tests are. Just a routine practice in Go that work as well in terratest as anywhere.

Happy testing,

Adam

Need more than just this article? We’re available to consult.

You might also want to check out these related articles:

CloudWatch Logs: Preventing Orphaned Log Groups

Hello!

When you need to publish logs to CloudWatch (e.g. from a lambda function), you need an IAM role with access to CloudWatch. It’s tempting to use a simple policy like the one in the AWS docs. You might write a CloudFormation template like this:

# Don't use this!

AWSTemplateFormatVersion: '2010-09-09'

Resources:
  DemoRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            Service:
            - lambda.amazonaws.com
          Action:
          - sts:AssumeRole
      Path: '/'
      Policies:
      - PolicyName: lambda-logs
        PolicyDocument:
          Version: '2012-10-17'
          Statement:
          - Effect: Allow
            Action:
            - logs:CreateLogGroup
            - logs:CreateLogStream
            - logs:DescribeLogStreams
            - logs:PutLogEvents
            Resource: arn:aws:logs:*:*:*

  DemoFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        ZipFile: |
          def handler(event, context):
              print('Demo!')
      FunctionName: demo-function
      Handler: index.handler
      Role: !GetAtt DemoRole.Arn
      Runtime: python3.7

Obviously, the role is too permissive: arn:aws:logs:*:*:*

But, there’s another problem: it grants logs:CreateLogGroup.

Here’s what happens:

  1. Launch a stack from this template
  2. Run demo-function
  3. Because we granted it permission, demo-function automatically creates /aws/lambda/demo-function log group in CloudWatch Logs
  4. Delete the stack
  5. CloudFormation doesn’t delete the /aws/lambda/demo-function log group

CloudFormation doesn’t know about the function’s log group because it didn’t create that group, so it doesn’t know anything needs to be deleted. Unless an operator deletes it manually, it’ll live in the account forever.

It seems like we can fix that by having CloudFormation create the log group:

DemoLogGroup:
  Type: AWS::Logs::LogGroup
  Properties:
    LogGroupName: /aws/lambda/demo-function
    RetentionInDays: 30

But, if the function still has logs:CreateLogGroup I’ve seen race conditions where the stack deletes the group before the lambda function and the function recreates that group before it gets deleted.

Plus, there aren’t any errors if you forget to define the group in CF. The stack launches. The lambda function runs. We even get logs, they’ll just be orphaned if we ever delete the stack.

That’s why it’s a problem to grant logs:CreateLogGroup. It allows lambda (or EC2 or whatever else is logging) to log into unmanaged groups.

All resources in AWS should be managed by CloudFormation (or terraform or whatever resource manager you use). Including log groups. So, you should never grant logs:CreateLogGroup except to your resource manager. Nothing else should need that permission.

And that’s the other reason: lambda doesn’t need logs:CreateLogGroup because it should be logging to groups that already exist. You shouldn’t grant permissions that aren’t needed.

Here’s the best practice: always manage your CloudWatch Logs groups and never grant permission to create those groups except to your resource manager.

Happy automating!

Adam

Need more than just this article? We’re available to consult.

You might also want to check out these related articles:

Terraform: Get Data with Python

Update 2017-12-26: There’s now a more complete, step-by-step example of how to use terraform’s data resource, pip, and this decorator in the source.

Good morning!

Sometimes I have data I need to assemble during terraform’s apply phase, and I like to use Python helper scripts to do that. Awesomely, terraform natively supports using Python to populate the data resource:

data "external" "cars_count" {
  program = ["python", "${path.module}/get_cool_data.py"]

  query = {
    thing_to_count = "cars"
  }
}

output "cars_count" {
  value = "${data.external.cars_count.result.cars}"
}

A slick, easy way to drop out of terraform and use Python to grab what you need (although it can get you in to trouble if you abuse it).

The Python script has to follow a protocol that defines formats, error handling, etc. It’s minimal but it’s fiddly, plus if you need more than one external data script it’s better to modularize than copy and paste, so I wrote a pip-installable decorator that implements the protocol for you. The source is also an example you can follow if you’d rather implement it yourself than add a dependency. Here’s how you use it:

from terraform_external_data import terraform_external_data

@terraform_external_data
def get_cool_data(query):
    return {query['thing_to_count']: '3'}

if __name__ == '__main__':
    get_cool_data()

It’s available on PyPI, just pip install terraform_external_data.

Happy terraforming!

Adam

Need more than just this article? We’re available to consult.