How to use local variables to simplify your Terraform code

Updated: February 3, 2024 By: Guest Contributor Post a comment

Introduction

Streamlining Terraform code is essential for developing robust, manageable, and scalable infrastructure as code (IaC) solutions. This tutorial delves into the strategic use of local variables to enhance code readability, maintainability, and reusability in Terraform. By integrating local variables, you can simplify complex expressions, reduce duplication, and make your configurations easier to understand and update.

Getting Started with Local Variables

Local variables in Terraform are named values that you can reference throughout a module. Unlike input variables that are set by users or external sources when Terraform is executed, local variables are defined within your Terraform configurations and used to simplify expressions and manage repeated values.

locals {
  common_tags = {
    Owner = "DevOps Team"
    Environment = "Production"
  }
}

Basic Usage of Local Variables

Let’s start with a simple example of how local variables can make your Terraform code cleaner. Imagine you have multiple resources that share common tags.

resource "aws_instance" "example" {
  ami = "ami-123456"
  instance_type = "t2.micro"
  tags = {
    Name = "ExampleInstance",
    Environment = "${local.common_tags.Environment}",
    Owner = "${local.common_tags.Owner}"
  }
}

This illustration shows how locals can reduce repetitiveness by centralizing shared values. Now, instead of duplicating the tag information for each resource, you can refer to the local variable common_tags.

Intermediate Examples

As you become more comfortable with local variables, you can start leveraging them for more complex scenarios.

locals {
  resource_prefix = "dev-${var.environment}"
}

resource "aws_s3_bucket" "example" {
  bucket = "${local.resource_prefix}-data"
  acl = "private"
}

This example demonstrates how locals can be used to construct resource names dynamically based on input variables, making your configurations more flexible and environment-specific.

Advanced Usage

For more advanced Terraform users, local variables can be instrumental in handling complex data structures and conditionals. Consider this scenario where you need to conditionally include certain resource properties.

locals {
  instance_settings = {
    instance_type = "t2.micro",
    ami = var.use_custom_ami ? var.custom_ami : "ami-123456"
  }
}

resource "aws_instance" "advanced_example" {
  ami = local.instance_settings.ami
  instance_type = local.instance_settings.instance_type
  tags = local.common_tags
}

This setup highlights the power of local variables in managing conditional logic and complex data structures, simplifying your Terraform configurations significantly.

Managing Complexity with Local Variables

Using locals is not just about reducing duplication; it’s about making your codebase manageable and understandable. By encapsulating complex logic and repeated values within local variables, you can make your Terraform code more readable and easier to maintain. Furthermore, locals encourage the DRY (Don’t Repeat Yourself) principle, minimizing the risk of errors and inconsistencies.

Conclusion

Incorporating local variables into your Terraform code can vastly simplify your configurations, making them more maintainable, readable, and flexible. Throughout this tutorial, we’ve seen how locals can transform your code from basic tag management to handling complex conditional logic and data structures. As your infrastructure grows in complexity, the strategic use of local variables will become increasingly valuable in your Terraform toolkit.