First time at Zeet?

19 Apr
2024
-
17
min read

Ultimate Guide on Terraform Stacks Setup, Use Cases, and Orchestration

Learn how to effectively set up and optimize Terraform Stacks. Explore various use cases and enhance your orchestration abilities with Terraform.

Jack Dwyer

Product
How To
Content
heading2
heading3
heading4
heading5
heading6
heading7

Share this article

What Is Terraform and How Does It Work?

What is Terraform? Terraform creates and manages resources on cloud platforms and other services through their application programming interfaces (APIs). Providers enable Terraform to work with virtually any platform or service with an accessible API.

The core Terraform workflow consists of three stages

Write

You define resources, which may be across multiple cloud providers and services. For example, you might create a configuration to deploy an application on virtual machines in a Virtual Private Cloud (VPC) network with security groups and a load balancer.

Plan

Terraform creates an execution plan describing the infrastructure it will create, update, or destroy based on the existing infrastructure and your configuration.

Apply

On approval, Terraform performs the proposed operations in the correct order, respecting any resource dependencies. For example, if you update the properties of a VPC and change the number of virtual machines in that VPC, Terraform will recreate the VPC before scaling the virtual machines.

Why Use Terraform

Terraform can manage low-level components like compute, storage, and networking resources, as well as high-level components like DNS entries and SaaS features. The tool is a popular choice for managing infrastructure as code for several reasons.

Why Terraform?

Manage any infrastructure

Find providers for many of the platforms and services you already use in the Terraform Registry. You can also write your own. Terraform takes an immutable approach to infrastructure, reducing the complexity of upgrading or modifying your services and infrastructure.

Track your infrastructure

Terraform generates a plan and prompts you for your approval before modifying your infrastructure. It also keeps track of your real infrastructure in a state file, which acts as a source of truth for your environment. Terraform uses the state file to determine the changes to make to your infrastructure so that it will match your configuration.

Automate changes

Terraform configuration files are declarative, meaning that they describe the end state of your infrastructure. You do not need to write step-by-step instructions to create resources because Terraform handles the underlying logic. Terraform builds a resource graph to determine resource dependencies and creates or modifies non-dependent resources in parallel. This allows Terraform to provision resources efficiently.

Standardize configurations

Terraform supports reusable configuration components called modules that define configurable collections of infrastructure, saving time and encouraging best practices. You can use publicly available modules from the Terraform Registry, or write your own.

Collaborate

Since your configuration is written in a file, you can commit it to a Version Control System (VCS) and use Terraform Cloud to efficiently manage Terraform workflows across teams. Terraform Cloud runs Terraform in a consistent, reliable environment and provides secure access to shared state and secret data, role-based access controls, a private registry for sharing both modules and providers, and more.

Related Reading

Understanding Terraform Stacks, their Components and Benefits

Terraform stacks are composed of several key components that work together to define, create, and manage infrastructure in a simple, scalable, and efficient manner. These components include:

Terraform Modules

Terraform modules are used to group resources together to form a reusable component that represents a piece of infrastructure. Modules are reusable, shareable, and can be applied multiple times in a configuration, making them a powerful tool for building complex infrastructure. By using modules, you can simplify your Terraform configurations, increase reusability, and promote consistency across your infrastructure.

State Files

The Terraform state file is critical for storing the current state of the infrastructure managed by Terraform. This JSON file maps the resources defined in your Terraform configurations to the real-world resources in your cloud provider. The state file allows Terraform to synchronize the infrastructure with the configuration, manage resource dependencies, store output values, and map resources in your code to the corresponding real-world resources.

Interactions Within the Stack

Terraform state files help synchronize infrastructure with configuration, resolve dependencies between resources, manage output values, and map resources between code and the cloud provider. Managing all infrastructure in a single large state file can lead to slow execution times, limited collaboration, dangerous changes, increased complexity, and challenges in testing and debugging. 

Terraform stacks provide a new approach to simplify infrastructure provisioning and management at scale. Stacks reduce manual efforts, improve productivity, and allow for rapid creation and modification of multiple consistent infrastructure configurations. Stacks aim to extend infrastructure as code to a higher layer using the same Terraform shared modules.

Zeet Terraform and Helm Product Overview

What Are the Common Use Cases for Terraform Stacks

Deploying an entire application with components like networking, storage, and compute as a single unit without worrying about dependencies, a stack configuration describes a full unit of infrastructure as code that can be handed to users who don’t have advanced Terraform experience, allowing them to easily stand up a complex infrastructure deployment with a single action. 

Deployments in a stack let you define multiple instances of the same configuration without needing to copy and paste configurations, or manage configurations separately. When a change is made to the stack configuration, it can be rolled out across all, some, or none of the deployments in a stack. Using Terraform stacks means I'm able to deploy across multiple regions, availability zones, and cloud provider accounts without duplicating effort or code.

Setting Up Your First Terraform Stack

1. Components in a Terraform Stack Configuration

  • Components help to define the infrastructure pieces that you want to include in a stack.
  • Compose multiple modules that share a lifecycle together using components. Add a component block to the configuration for each module you would like to include in the stack.

2. Deployments in a Terraform Stack Configuration

  • Deployments define where and how many times you want to deploy the infrastructure within the stack.
  • For each instance of infrastructure, add a deployment block with the necessary input values. Terraform will handle repeating that infrastructure accordingly.

3. Example Scenario

  • Consider deploying three Kubernetes clusters, each with one or more namespaces, into three different geographies.
  • In a stack, one component can reference a module for deploying the Kubernetes cluster, while another component can create a namespace in it.
  • To repeat this Kubernetes cluster across three geographies, define a deployment for each geography and pass in the appropriate inputs (e.g., region identifiers).

4. Phased Support for Terraform Stacks

  • Basic language support will include syntax highlighting for new keywords, schema additions to terraform-schema, and support for completion, hover, and semantic tokens of new language keywords.
  • Complex language support will provide various advanced features and functionalities, including reference target and origin resolution, inputs and outputs completion between stacks, components, and deployments.

Maximize Your Cloud Investments with Zeet

Zeet helps you to get more from your cloud, Kubernetes, and Terraform investments and helps your engineering team become strong individual contributors through our CI/CD & deployment platform. Contact Zeet to learn more about how Zeet help you get seamless cloud deployments every time, and helps your team to become a top-performing engineering team.

Related Reading

Create and orchestrate Terraform Stacks with Terramate

Description of Terramate

Terramate is a game-changer in the realm of Infrastructure as Code (IaC) Management Platforms. It empowers teams to assemble, deploy, manage, and observe cloud infrastructure with top-notch IaC tools such as Terraform, OpenTofu, Terragrunt, Kubernetes, and more. Terramate offers a free and open-source command-line interface (CLI) that can be effortlessly integrated with its completely managed Cloud service, Terramate Cloud, in an automated fashion.

Terramate is here to make your life easier. It integrates seamlessly with any pre-existing architecture and the entire range of third-party tooling in under 5 minutes, without any prerequisites, lock-ins, or requiring modifications to any existing Terraform configuration. You can easily onboard Terramate to your existing Terraform or Terragrunt projects with just a single command. It plays nice with all the tools you already love, such as GitHub or Slack, in a completely non-intrusive manner.

Creating and Orchestrating Terraform Stacks

Creating Terraform stacks with Terramate is a structured process that enhances efficiency and minimizes the impact scope of changes, known as the "blast radius". Stacks provide a structured approach to organizing Terraform and OpenTofu into manageable units.

They enable independent deployment and management, reducing execution runtimes and the impact of changes. You can think of a stack as an amalgamation of infrastructure code, state, and configuration needed to deploy a specific service easily across different environments.

Prerequisites

Before we dive into creating Terraform stacks, make sure you have Terraform and Terramate installed and configured for your project. It's important to have the right tools in place before getting started.

Initializing a New Repository

To kick things off, initialize a new repository with the following commands:

git init -b main terramate-stacks
cd terramate-stacks

Creating Example Stacks

Now that the repository is set up, let's create a few example stacks to get a feel for how things work:

bash terramate create --name "Example Stack A" --description "This is an awesome first example stack" stacks/a terramate create --name "Example Stack B" --description "This is an awesome second example stack" stacks/b terramate create --name "Example Stack C" --description "This is an awesome third example stack" stacks/c


With these commands, three distinct example stacks are created for experimentation and learning purposes.

Exploring Stack Information

To view all the available stacks in your project, use the following command:

bash terramate list


This command will display all the stacks you've created, making it easy to keep track of your projects. 

Detailed Stack Metadata

For a more comprehensive look at stack metadata, you can run the following command:

bash terramate debug show metadata



This command provides a wealth of information about the available stacks and their metadata, giving you a deeper understanding of your infrastructure.

Working with Terraform in Stacks

In Terramate, stacks initially consist only of directories with a stack.tm.hcl file defining the metadata and configuration of a stack. To make use of stacks for managing infrastructure with Terraform or OpenTofu, we need to create actual Terraform configurations in each stack.

Related Reading

Zeet Contact Us

Have Successful Releases Every Time With Zeet's CI/CD & Deployment Platform for Kubernetes and Terraform

Zeet is an industry leader that can help you get the most out of your cloud, Kubernetes, and Terraform investments. With Zeet's CI/CD & deployment platform, your engineering team can become strong individual contributors, helping to streamline cloud deployments and establish a high-performance engineering team. 

By using Zeet, you can ensure seamless cloud deployments every time, which can significantly boost your productivity. Contact Zeet today to learn more about how their innovative solutions can help your team become top-performing experts in the field.

Subscribe to Changelog newsletter

Jack from the Zeet team shares DevOps & SRE learnings, top articles, and new Zeet features in a twice-a-month newsletter.

Thank you!

Your submission has been processed
Oops! Something went wrong while submitting the form.