Skip to main content

Command Palette

Search for a command to run...

Terraform Apply Command

Updated
Terraform Apply Command
Y

Tech Lead & Architect | 13+ Years in Cloud, Backend, and AI - Experienced software engineer with expertise in Java, Spring Boot, Microservices, Angular, React, Kafka, DevOps, Python, PySpark, Databricks, and Generative AI. Certified in TOGAF, AWS, and Google Cloud. Passionate about building scalable, secure, and high-performance systems. Enthusiast in Data Engineering & Agentic AI. Author of 1,200+ technical articles sharing insights across diverse tech stacks.

Date: 2022-01-25

Understanding Terraform and the apply Command: A Comprehensive Guide

Terraform, an open-source tool developed by HashiCorp, has revolutionized infrastructure management. It provides a way to build, modify, and version infrastructure efficiently and safely, regardless of whether that infrastructure resides in popular cloud services or custom-built internal systems. This powerful tool allows engineers to manage both low-level components (like computing power, storage, and networking) and high-level components (such as Software as a Service integrations and DNS configurations) with a unified approach. The core principle behind Terraform is declarative infrastructure as code, meaning you define the desired state of your infrastructure, and Terraform figures out how to achieve it.

Terraform's functionality is built around its own configuration language, designed specifically for automating infrastructure. This language isn't about programming in the traditional sense; instead, it focuses on describing the desired infrastructure. The language uses a structure consisting of blocks, arguments, and expressions to define resources and their relationships. A group of related resources can be bundled into a module, creating a more manageable unit for larger, more complex infrastructures. Essentially, you're writing a description of what your infrastructure should look like, and Terraform acts as the translator, transforming that description into reality.

The process of deploying infrastructure using Terraform involves a sequence of commands. While the specific steps can vary depending on the workflow, the apply command serves as the crucial final step, bridging the gap between the description and the actual infrastructure. Before the apply command is executed, Terraform needs to understand the desired state of the infrastructure. This is typically done by defining resources within configuration files. These files essentially act as blueprints, detailing the specific components needed, their configurations, and how they interact with each other. Think of it like an architect's detailed plans for a building – the configuration files are the plans, and Terraform is the construction crew.

The apply command is the cornerstone of Terraform's deployment process. This command initiates the process of creating or modifying the infrastructure based on the configuration files. It begins by scanning the current working directory to locate the configuration files. These files contain the complete definition of the desired infrastructure. Once located, the command analyzes the configuration and compares it against the current state of the infrastructure. This comparison is crucial, as it allows Terraform to determine what changes need to be made.

A key aspect of the apply command is its interaction with a plan file. Before actually making any changes to the real infrastructure, Terraform can generate a plan. This plan acts as a preview, outlining the proposed modifications. This is a critical safety measure, allowing users to review the changes before they are implemented, preventing unintended consequences. If a plan file already exists in the working directory, the apply command uses this existing plan. However, if a plan file is absent, the command generates a new plan, presenting it to the user for review and approval before proceeding. This approval step is an important safeguard, ensuring the user is fully aware of the impending changes.

The nature of the plan itself is important to understand. It doesn't contain the actual code for making the changes; instead, it’s a detailed description of what changes will be made. This description includes the resources that will be created, modified, or deleted, along with the specific alterations to their configurations. This allows for a comprehensive understanding of the impact of the upcoming changes without actually altering the infrastructure. If the plan indicates no changes are needed—either because the infrastructure already matches the desired state or because the configuration files haven't changed—the apply command exits cleanly without making any modifications.

Once the plan is approved (or if a pre-existing plan is used), the apply command proceeds to execute the changes in the real infrastructure environment. This involves interacting with the relevant cloud provider or internal systems to provision or modify the specified resources. The process might involve creating virtual machines, configuring networks, setting up storage, or managing other infrastructure elements, all depending on what is defined in the configuration files. Throughout this process, Terraform keeps track of every change, ensuring that the final state of the infrastructure precisely matches the description in the configuration files.

The apply command is not a one-off action; it's typically used repeatedly throughout the lifecycle of an infrastructure. As the infrastructure evolves and requirements change, the configuration files are updated to reflect these changes. The apply command then ensures that the real infrastructure is brought into line with these updated configurations. This iterative process allows for continuous integration and continuous deployment (CI/CD) of infrastructure, streamlining the development and management of complex systems. The ability to version control the configuration files further enhances this capability, allowing for easy rollback to previous states if necessary.

In summary, the Terraform apply command is the linchpin of Terraform’s infrastructure management capabilities. It bridges the gap between a descriptive definition of infrastructure and its actual implementation, acting as a reliable and safe way to deploy, modify, and maintain complex systems. The command's emphasis on planning and user approval minimizes the risk of unintended consequences, while its integration with version control systems enables efficient management and rollback capabilities. The combination of these features makes Terraform a powerful and indispensable tool for modern infrastructure management.

Read more

More from this blog

The Engineering Orbit

1174 posts

The Engineering Orbit shares expert insights, tutorials, and articles on the latest in engineering and tech to empower professionals and enthusiasts in their journey towards innovation.