Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

What is Terrafile?

Terrafile is a tool used to manage Terraform modules as dependencies. It simplifies the process of downloading and managing Terraform modules by automating the fetching of modules from different sources, ensuring that all the necessary modules are available in a consistent and reproducible manner.

The Terrafile approach simplifies managing modules. The advantage of using a Terrafile is centralization. You can centrally define, manage, and update modules. To define modules, add them to Terrafile:

How Terrafile Works

Terrafile works by reading a file named Terrafile, which specifies the dependencies your Terraform project needs. This file contains the list of Terraform modules, their sources, and versions. Terrafile then downloads these modules into a specific directory within your project, making it easier to manage module dependencies.

A module downloaded to vendor/modules/s3 is sourced same way as if they were defined in app/modules/s3. This is because Terraspace considers multiple lookup paths.

Usage of Terrafile

The main purpose of Terrafile is to automate the management of Terraform module dependencies. Here are some key uses:

  1. Consistent Module Management: Ensures that all team members and environments use the same versions of modules.
  2. Simplified Dependency Handling: Automates the fetching and updating of Terraform modules, reducing manual efforts and errors.
  3. Reproducibility: Helps in maintaining a consistent infrastructure setup by ensuring that the same module versions are used across different environments.
  4. Version Control: Allows you to specify exact versions of modules, making it easy to roll back or upgrade module versions.

Terrafile Workflow

The typical workflow for using Terrafile involves the following steps:

  1. Create a Terrafile: Define the modules you need in a Terrafile with their respective sources and versions.
  2. Run Terrafile: Execute the Terrafile command to download the specified modules into your project.
  3. Integrate with Terraform: Use the downloaded modules in your Terraform configurations.
  4. Update Modules: Modify the Terrafile and rerun Terrafile to update module versions as needed.

Here’s a demo stack that uses the downloaded vendor/modules/s3 module.

app/stack/demo/main.tf

resource "random_pet" "this" {
  length = 2
}

module "bucket" {
  source     = "../../modules/s3" # looks up either app/modules/s3 or vendor/modules/s3
  bucket     = "bucket-${random_pet.this.id}"
  acl        = var.acl
}

A module downloaded to vendor/modules/s3 is sourced same way as if they were defined in app/modules/s3. This is because Terraspace considers multiple lookup paths. 

Terrafile.lock

A Terrafile.lock file is also generated. This file can be committed to version control to ensure that everyone on the team uses the exact same version.

Terrafile Setup

Here's a step-by-step guide to setting up and using Terrafile:
1. Install Terrafile

Terrafile is typically installed using a package manager or directly from the source. Here's how you can install it:

Homebrew (macOS/Linux):

$ brew install terrafile

From Source:

    git clone https://github.com/coretech/terrafile.git
    cd terrafile
    make install

2. Create a Terrafile

Create a file named Terrafile in the root of your Terraform project directory. This file lists all the modules your project depends on. Here's an example of what a Terrafile might look like:

module_name_1:
  source: "git::https://github.com/terraform-aws-modules/terraform-aws-vpc.git"
  version: "v2.77.0"

module_name_2:
  source: "git::https://github.com/terraform-aws-modules/terraform-aws-ec2-instance.git"
  version: "v3.3.0"

3. Run Terrafile

Navigate to your project directory and run Terrafile to download the modules specified in the Terrafile:


$ terrafile

By default, Terrafile will download the modules into a directory named vendor/modules.
4. Use the Modules in Terraform

Update your Terraform configuration files to reference the modules from the vendor directory:


module "vpc" {
  source = "./vendor/modules/terraform-aws-vpc"
  ...
}

module "ec2" {
  source = "./vendor/modules/terraform-aws-ec2-instance"
  ...
}

5. Update Terrafile and Modules

To update modules, modify the Terrafile with the new version numbers and rerun the Terrafile command:


$ terrafile


Terrafile command line


# Run Terrafile with the default settings
terrafile

# Run Terrafile with a custom Terrafile and download path
terrafile --file CustomTerrafile --path custom/modules

# Run Terrafile with verbose output
terrafile --verbose

Difference between terraspace and terrafile

Terraspace and Terrafile are both tools designed to enhance the management of Terraform projects, but they serve different purposes and offer distinct functionalities. Here’s a comparison of the two to highlight their differences:

Terrafile

Purpose

  • Dependency Management: Terrafile is primarily focused on managing Terraform module dependencies. It simplifies the process of fetching and managing modules from various sources.

Functionality

  • Simple Module Management: Terrafile reads a Terrafile configuration file that lists the Terraform modules required for a project, along with their sources and versions.
  • Downloads Modules: It automatically downloads these modules into a specified directory within the project, ensuring consistency across different environments.
  • Minimal Configuration: Terrafile is lightweight and doesn’t require complex setup or configuration. It’s primarily concerned with pulling module dependencies.

Use Cases

  • Consistent Module Use: Ensures that all team members are using the same versions of Terraform modules.
  • Automated Fetching: Automates the manual process of downloading modules, reducing errors and effort.

Terraspace

Purpose

  • Infrastructure as Code Framework: Terraspace is a more comprehensive framework for managing Terraform projects, designed to simplify the organization, automation, and deployment of infrastructure as code.

Functionality

  • Project Organization: Provides a structured way to organize Terraform code, including support for multiple environments, stacks, and modularization.
  • Automation and Deployment: Facilitates automated workflows for deploying Terraform configurations, including support for CI/CD pipelines.
  • Convenience Features: Offers utilities for templating, managing Terraform state, and configuring provider-specific settings.
  • Multi-cloud Support: Supports deploying infrastructure across different cloud providers within the same framework.
  • Built-in Support for Terrafile: Terraspace can utilize Terrafile to manage module dependencies as part of its broader infrastructure management capabilities.

Use Cases

  • Complex Environments: Suitable for managing complex infrastructures with multiple environments and dependencies.
  • Automation: Useful for teams that require a comprehensive framework to automate the deployment and management of infrastructure.
  • Multi-cloud Deployments: Supports projects that span across different cloud providers.

Key Differences

AspectTerrafileTerraspace
Primary FunctionModule dependency managementComprehensive infrastructure as code management framework
ComplexityLightweight, minimal configurationMore complex, feature-rich, requires setup
Use CaseSimplifying module downloads and version controlManaging complex, multi-environment infrastructure projects
Project StructureNo specific project organizationProvides structured organization for projects
AutomationFocuses on module managementFacilitates automated workflows and deployment
Multi-cloudNot inherently designed for multi-cloud useSupports multi-cloud infrastructure deployment

Conclusion

  • Terrafile is ideal for simple use cases where managing Terraform module dependencies is the primary concern. It offers a straightforward way to ensure consistency in module versions across a team.
  • Terraspace, on the other hand, is suited for more complex infrastructure projects where automation, project organization, and multi-environment support are needed. It includes a variety of features to facilitate comprehensive infrastructure management, including but not limited to dependency management.
Rajesh Kumar
Follow me
Latest posts by Rajesh Kumar (see all)
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x