Terraform Apply: The Definitive Guide

Terraform Apply: The Definitive Guide

Terraform is an open-source infrastructure as a code software tool that enables you to safely and predictably create, change, and improve infrastructure.

One of the essential commands in Terraform is terraform apply. This command is used to create, update, or destroy infrastructure resources. When you run terraform apply, Terraform will first generate a plan of the changes that it will make. You can then review and approve the plan before Terraform makes any changes.

Why use Terraform Apply?

There are many reasons to use Terraform Apply. Here are just a few:

  • It is a safe and predictable way to manage infrastructure. Terraform uses declarative language to define infrastructure resources. This means that you can specify the desired state of your infrastructure, and Terraform will take the necessary steps to ensure that your infrastructure matches your desired state.
  • It is version control friendly. Terraform configuration files are version controlled alongside your code and configuration. This makes it easy to track changes to your infrastructure and roll back to previous versions if necessary.
  • It is easy to automate. Terraform can be easily integrated with your CI/CD pipeline. This makes it easy to deploy infrastructure changes to production in a repeatable and automated way.

When to use Terraform Apply?

You should use Terraform Apply whenever you want to change your infrastructure. This includes creating new resources, updating existing resources, and destroying resources.

How to use Terraform Apply

To use Terraform Apply, you must have the Terraform CLI installed. Once you have Terraform installed, you can run the terraform apply command.

The terraform apply the command takes several arguments. The most important argument is the path to the Terraform configuration file. For example, to apply the configuration file main.tf, you would run the following command:

terraform apply main.tf

Terraform will then generate a plan of the changes that it will make. You can then review and approve the plan before Terraform makes any changes.

To approve the plan, you can press Enter. To reject the plan, you can press Ctrl+C.

Once you have approved the plan, Terraform will make the necessary changes to your infrastructure.

Examples of using Terraform Apply

Here are a few examples of how you can use Terraform Apply:

  • To create a new EC2 instance, you would add the following configuration to your Terraform configuration file:
resource "aws_ec2_instance" "example" {
  ami = "ami-0123456789abcdef0"
  instance_type = "t2.micro"
}

This configuration file specifies that the provider is AWS and that a single EC2 instance should be created using the specified AMI and instance type.

Initialize Terraform

Before you can use Terraform Apply, you need to initialize the configuration file. This step downloads any required plugins and sets up the environment for Terraform to work. To initialize Terraform, run the following command:

terraform init

Plan the changes

Before applying the changes to the infrastructure, you can use the Terraform Plan command to see what changes will be made. This step allows you to preview the changes before they are applied. To plan the changes, run the following command:

terraform plan

Apply the changes

To update the name of an existing EC2 instance, you would update the name the attribute in the Terraform configuration file. For example, to change the name of the instance from example to my-instance, you would add the following configuration to your Terraform configuration file:

resource "aws_ec2_instance" "example" {
  ami = "ami-0123456789abcdef0"
  instance_type = "t2.micro"
  name = "my-instance"
}

Then, you would run the following command to update the name of the instance:

terraform apply
  • To destroy an existing EC2 instance, you would remove the corresponding configuration from your Terraform configuration file. For example, to destroy the instance that was created in the previous example, you would remove the following configuration from your Terraform configuration file:
resource "aws_ec2_instance" "example" {
  ami = "ami-0123456789abcdef0"
  instance_type = "t2.micro"
}

Then, you would run the following command to destroy the instance:

terraform apply

But alternatively, you can use terraform destroy to achieve the same goal.

How the ‘terraform apply’ works

Terraform Apply is a core command in the Terraform toolchain that allows you to apply infrastructure changes to a provider, such as AWS, Google Cloud, or Microsoft Azure. Behind the scenes, Terraform Apply compares the desired state of your infrastructure, as defined in your Terraform configuration file, with the current state of your infrastructure, as stored by the provider. It then applies necessary changes to bring the infrastructure into the desired state.

Here is a more detailed explanation of how Terraform Apply works:

Terraform reads the configuration file

When you run Terraform Apply, Terraform reads the configuration file to determine the desired state of the infrastructure. The configuration file specifies the resources that should be provisioned, their configuration parameters, and any dependencies between them.

Terraform reads the current state

Next, Terraform reads the current state of the infrastructure from the provider. This includes information about the resources already provisioned, their configuration parameters, and any dependencies between them.

Terraform compares the desired state and the current state

Terraform then compares the desired and current states to determine what changes must be made. It compares the configuration parameters of the existing resources with the desired configuration parameters specified in the configuration file.

Terraform creates a plan

Based on the comparison between the desired and current states, Terraform creates a plan that describes the changes needed to bring the infrastructure into the desired state. The plan includes details about which resources must be created, updated, or deleted and any dependencies between them.

Terraform executes the plan

Finally, Terraform executes the plan by making API calls to the provider to create, update, or delete the necessary resources. Terraform also updates its state file to reflect the changes made to the infrastructure.

In summary, Terraform Apply compares the desired state of the infrastructure, as defined in the configuration file, with the current state of the infrastructure, as stored by the provider. It then creates a plan to apply any necessary changes to bring the infrastructure into the desired state and executes the plan by making API calls to the provider. Terraform Apply offers consistency, predictability, and automation, making it an essential tool for managing infrastructure as code.

Conclusion

Terraform Apply is a powerful tool that can be used to create, update, and destroy infrastructure resources. By using Terraform Apply, you can ensure that your infrastructure is always in the desired state.

Leave a Comment

Your email address will not be published. Required fields are marked *

Free PDF with a useful Mind Map that illustrates everything you should know about AWS VPC in a single view.