Terraform Apply: the magic command that turns your Infrastructure into reality

all about terraform apply

In the fast-paced world of cloud computing, Infrastructure as Code (IaC) has become a game-changer, allowing developers and operations teams to define and manage infrastructure through code. Terraform, one of the leading IaC tools, offers a powerful command called terraform apply, which brings your infrastructure blueprints to life. In this blog article, we will delve into what terraform apply is, why it’s essential, and how to use it effectively to provision and manage your cloud resources.

What is terraform apply?

Terraform apply is a critical Terraform command that takes your declared infrastructure configurations and applies them to your cloud provider, creating and managing the specified resources. It is one of the core commands in the Terraform workflow, alongside terraform init and terraform plan.

When you run terraform apply, Terraform performs the following actions:

  • Resource Creation: It creates the resources defined in your Terraform configurations and provisions them on your cloud provider.
  • State Management: Terraform stores the state of your infrastructure in a state file. This file contains the mapping of the resources managed by Terraform and their current state. The state file is critical for Terraform to understand the desired state and track changes.
  • Dependency Resolution: Terraform analyzes the resource dependencies defined in your configurations and creates resources in the correct order.
  • Idempotency: Terraform ensures that the configurations are applied in an idempotent manner. This means that if the desired state matches the current state, no changes will be made to the infrastructure.
terraform apply workflow

Why is terraform apply Essential?

terraform apply plays a crucial role in the Terraform workflow for the following reasons:

  • Infrastructure Provisioning: This command is responsible for turning your infrastructure blueprints into actual resources on your cloud provider. It creates, modifies, or destroys resources as per the changes defined in your configurations.
  • Repeatability and Consistency: Infrastructure as Code promotes consistency across environments. terraform apply ensures that the same configurations produce the same infrastructure every time, leading to repeatable deployments.
  • State Tracking: The state file generated by terraform apply is vital for managing the lifecycle of your infrastructure. It enables Terraform to understand the differences between your desired state and the current state when you make future changes.
  • Scalability and Collaboration: As your infrastructure evolves, using terraform apply allows you to scale your environment and collaborate with team members while keeping the infrastructure under version control.

Using terraform apply

Using terraform apply is straightforward, but there are essential steps to follow:

  • Initialize the Project: Before running terraform apply, make sure you have initialized your Terraform project using terraform init. This command sets up the necessary configurations, downloads providers, and prepares your project for execution.
  • Review the Plan: It’s always a good practice to run terraform plan before applying changes with terraform apply. This allows you to review the proposed changes and verify that they align with your intentions.
  • Execute the Apply Command: Run terraform apply in your project directory:

Terraform will prompt you to confirm that you want to apply the changes. Type yes and press Enter to proceed.

Best Practices

To ensure smooth and efficient usage of terraform apply, consider the following best practices:

  • Backup Your State File: Always back up your Terraform state file. It contains critical information about your infrastructure and is essential for disaster recovery.
  • Use Variables and Parameterization: Leverage input variables to parameterize your configurations. This makes your infrastructure more flexible and easier to adapt to different environments.
  • Workspace Management: Use Terraform workspaces to manage different deployment environments (e.g., development, staging, production) and avoid conflicts between states.
  • Version Control: Keep your Terraform configurations and state files under version control to track changes and collaborate with your team.

The Power of Auto-Approval for automation

Terraform’s apply command is designed to enact changes on your infrastructure according to the specifications outlined in your configuration files. By default, the command pauses during execution to present a detailed plan of the changes it intends to apply. This offers an opportunity for a human operator to review and confirm these modifications before they’re made live.

However, when automation is the priority and a high degree of confidence exists in the planned changes, the -auto-approve flag comes into play. This flag instructs Terraform to bypass the interactive approval step and immediately proceed with the deployment based on the pre-generated plan. While this might sound like a small change, it can significantly impact the efficiency of your deployment workflows.

To prevent encoding problems when running processes within certain CI/CD systems like Azure DevOps, it is beneficial to employ the terraform apply -no-color flag. This is because the display of colored output is not correctly handled by the build agents in such systems.

Ensuring optimal observability over Terraform apply actions

As you understand, Terrfaform Apply actions are quite powerful and have a direct and active role on your infra. Fortunately, a new tool, Holori, can help you ensure that you create, modify, and destroy resources in a controlled manner.

With Holori you can import a Terraform file and visualize the corresponding Terraform graph. There is nothing better than a diagram to understand the complexity of an infra and the dependency between elements. Moreover, you can highlight the modifications between versions with an easy to read color code to identify which resources were added, modified or deleted.

track terraform apply changes

This is particularly critical anytime you touch you prod infra as the tiniest mistake could bring your app or website down and possibly lead to a loss of data. By visualizing quickly on a graph the impact that your actions will have on the infra allows you to be aware of what your modifications imply.

You can sign up here to try the tool : https://app.holori.com/register

Key learnings

terraform apply is the heart of the Terraform workflow, responsible for transforming your Infrastructure as Code into tangible cloud resources. By understanding its significance and using it alongside terraform plan, you can provision, manage, and scale your infrastructure with confidence and efficiency. Embrace the power of Infrastructure as Code, and let terraform apply bring your cloud environment to life with ease and reliability. Happy provisioning!

Share This Post

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

GPU available within GCP

List of GPU available in the different GCP Zones

As artificial intelligence (AI) and machine learning (ML) continue to advance, the demand for powerful computing resources grows. Graphics Processing Units (GPUs) have become essential


List of all GCP regions and zones

As of June 2024 GCP has more than 40 regions available and 121 Zones worldwide. Typically there are 3 zones for each regions and 1