· Guide  · 2 min read

Scaling Terraform - Module Composition

Reduce the noise in your modules.

Reduce the noise in your modules.
This is part of a series related to scaling Terraform.
  1. A GitOps Prelude
  2. Module Composition

Introduction

In the first part of this series, we covered how to structure a GitOps repository and apply Terraform configurations using a consistant, well-structured pattern and a powerful bit of shell scripting.

In this post, we’ll go beyond generic resource creation and focus on how to scale the creation and management of many resources—cleanly and consistently.

In this post, I’ll cover:

Requirements

What are we building?

We’ve been tasked with building an API for a chatbot. The conversations endpoints are defined below.



How are we building it?

We’re going to use Lambda and API Gateway and define each endpoint with its own Lambda. This helps us reduce the risk of impacting other endpoints if one is having a bad time or if we need to apply specific configurations to an endpoint (memory, runtime, cpu, etc).



Setting Defaults in Maps

Example of Untenable Implementation

We’re going to use for_each meta-argument to loop over a map defined in a local value called api_lambda_functions. Where I see teams begin to struggle is managing the confguraitons for multiple resources, passing in every possible key.

You generally don’t want to define each lambda individually within your variable map. You’ll end up with something that looks like this:

Implementation With Defaults

Using the try function when defining our lambda resources, we can set default values for keys. This allows us to significantly trim the maps in our local values. The only keys we need to pass in are the description and route_key.

Back to Blog

Related Posts

View All Posts »