DEV Community

Cover image for 3 Ways to Configure Resources in Terraform
Ijay
Ijay

Posted on

3 Ways to Configure Resources in Terraform

Infrastructure as Code has changed how engineers manage cloud infrastructure. Instead of manually creating resources in the cloud console, we can define everything in code and let tools handle the provisioning.

Terraform is one of the most popular tools for this purpose.

When working with Terraform, you often need to define values for your resources. For example, you may want to specify the following:

  • The instance type of a server

  • The region where resources will be deployed

  • The name of a resource

  • Tags used for identification

Terraform allows you to configure these values in different ways. Choosing the right approach helps make your infrastructure easier to manage, reuse, and maintain.

In this article, you will learn three common ways to configure resource values in Terraform:

  • Using variables

  • Using locals

  • Using auto.tfvars files

By the end of this guide, you will understand when to use each method and how they help improve your Terraform configuration.

starting tutorial

Prerequisites

Before reading along, make sure you have the following:

  • Basic understanding of any cloud provider infrastructure.

  • Terraform is installed on your machine.

You can verify that Terraform is installed by running the following command in your terminal:

terraform version
Enter fullscreen mode Exit fullscreen mode

If Terraform is installed correctly, you should see the version number above displayed.

checking the installation

Understanding Resource Configuration in Terraform

In Terraform, infrastructure is defined using resources.

A resource represents a cloud component such as a virtual machine, storage bucket, database, or load balancer.

Here is a simple example of a Terraform resource that creates an AWS EC2 instance:

resource "aws_instance" "example" {
  ami           = "ami-0xxxxxxxxxxxxxxx0"
  instance_type = "t2.micro"
}
Enter fullscreen mode Exit fullscreen mode

In this configuration, the values for ami and instance_type are written directly inside the resource block.

This works for simple cases, but it can become difficult to manage when your infrastructure grows. Hardcoding values makes configurations less flexible and harder to reuse.

Terraform provides several ways to make these values more dynamic and easier to manage.

How Do We Add Dynamic Values in Terraform?

When writing Terraform configurations, you may sometimes need values that can change. For example, the instance type of a server may be different in development and production environments.

If you write these values directly inside your resource blocks, your configuration becomes harder to reuse and maintain. Instead, Terraform allows you to define values outside the resource and reference them when needed.

Method 1: Using Variables

Variables allow you to define values outside of your resource blocks. This makes your Terraform configuration more flexible and easier to reuse.

Instead of hardcoding values inside a resource, you define the value once and reference it wherever it is needed.

Step 1: Define the variable.

First, define a variable. This is usually done in a file called variables.tf

variable "instance_type" {
  description = "The EC2 instance type"
  type        = string
  default     = "t2.micro"
}
Enter fullscreen mode Exit fullscreen mode

Above, we created a variable called instance_type.

This variable stores the value Terraform uses when creating the EC2 instance.

The default value is set to t2.micro, but this value can be changed later if needed.

Step 2: Next is to use the variable to create a resource.

After defining the variable, you can reference it inside your resource configuration.

resource "aws_instance" "example" {
  ami           = "ami-0xxxxxxxxxxxxxxx0"
  instance_type = var.instance_type
}
Enter fullscreen mode Exit fullscreen mode

So with this declaration Terraform does not use a hardcoded value for the instance type. Instead, it reads the value from the variable we defined earlier.

The prefix var. tells Terraform that the value is coming from a variable.

When Terraform runs, it will use the value stored in instance_type to configure the resource.

Why do you think variables are useful?

As mentioned earlier, hardcoding values can make your infrastructure difficult to manage, especially as your system grows or needs to scale.

Terraform variables help solve this problem by separating configuration values from the resource definitions.

Variables are useful in several situations.

  • When you want to reuse the same configuration in different environments

For example, you might use a small instance type in a development environment and a larger one in production. With variables, you can keep the same configuration and only change the value.

  • When you want to change values without modifying the resource block

Instead of editing the resource every time a value changes, you simply update the variable. This keeps your configuration cleaner and easier to maintain.

  • When you want to make your Terraform configuration easier for other engineers to use

Variables make it clear which values can be adjusted. This allows other engineers to work with the configuration without needing to modify the core infrastructure code.

Method 2: Using Locals

Terraform locals allow you to store values or expressions that are used multiple times in your configuration.

Locals are helpful when you want to simplify repeated values or calculations.

How do I use Local?

Step 1: Define the Local Values

Create a locals block in your Terraform configuration.

locals {
  instance_name = "example-server"
  instance_type = "t2.micro"
}
Enter fullscreen mode Exit fullscreen mode

The code block defines local values called instance_name and instance_type of the configuration.

Next, you define the local values inside the resource configuration

Step 2: Using Locals in a Resource

resource "aws_instance" "example" {
  ami           = "ami-0abcdef1234567890"
  instance_type = local.instance_type

  tags = {
    Name = local.instance_name
  }
}
Enter fullscreen mode Exit fullscreen mode

The resource configuration uses the value stored in instance_name and in instance_type above to create a resource

Why are Locals Useful in Terraform Declarations?

  • Locals are useful when you want to avoid repeating the same values in multiple places.

  • Locals can be used to simplify complex expressions.

  • The keyword "local" can be used to organize your configuration more clearly.

Method 3: Using auto.tfvars Files

A tfvars file is used to store values for Terraform variables. Instead of typing values in the command line or writing them directly in your configuration, you can place them inside a separate file.

Terraform has a special type of file called ".auto.tfvars."

When Terraform sees a file with this name, it automatically loads the values inside it. This means you do not need to manually pass the file when running Terraform commands.

How is it declared?

Step 1: Define the variable

First, define the variable in your Terraform configuration.

For example, in variable. tffile:

variable "instance_ec2" {
  description = "EC2 instance type"
  type        = string

 tags = {
    Create_By = var.created_by
  }
}
Enter fullscreen mode Exit fullscreen mode

Here, we created a variable called instance_ec2

Step 2: Create the auto.tfvars File

Next, create another file called, let's say, ec2.auto.tfvars

Inside this file, assign a value to the variable.

instance_type = "t3.micro"
created_by    = "dev"
Enter fullscreen mode Exit fullscreen mode

Why Should You Create a auto.tfvars File?

The file helps keep your Terraform configuration clean by separating variable values from the main infrastructure code.

Instead of placing all values directly inside your Terraform files, you can store them in an auto.tfvars file and let Terraform load them automatically.

auto.tfvars Files are useful when:

  • To keep configuration values separate from your Terraform code
    i.e., your resource definitions stay focused on infrastructure, while the variable values are stored in a separate file.

  • To manage different environment settings
    For example, a development environment might use smaller resources, while production may require larger ones.

  • To allow Terraform to automatically load variable values
    Terraform reads .auto.tfvars files automatically when you run commands like terraform plan or terraform apply, so you do not need to pass the file manually.

Because of the benefit of an auto.tfvars file, many teams use tfvars files to manage values for different environments, such as

  • development

  • staging

  • production

When Should You Use Each Method in Resource Configuration?

  • Variables are best when you want flexibility and reusable configurations.

  • Locals are useful for simplifying repeated values inside your Terraform code.

  • auto.tfvars files help supply variable values automatically without modifying the configuration.

In practice, most Terraform projects use a combination of these approaches.

Conclusion

Terraform provides several ways to configure values for your resources. Choosing the right approach can make your infrastructure easier to manage and maintain.


Other Helpful Resources


Stay updated with my projects by following me on Twitter, LinkedIn, and GitHub.

Thank you for reading

Top comments (0)