Terraform for AWS init validate plan apply destroy

Paul Ravvich
Tarraform using Amazon Web Services
3 min readMay 12, 2024

Hi, this is Paul, and welcome to the #3 part of my Terraform guide. Today we will discuss Terraform Workflow.

Hight Level Terraform Workflow

Let’s start by exploring the basic workflow of Terraform commands at a high level. Initially, we’ll focus on mastering the fundamental commands before diving into the language syntax and further complexities. This foundational knowledge will enable us to provision a simple EC2 instance, even without prior insight into the specific Terraform configuration.

Moving on, the core workflow of Terraform consists of five essential steps. These steps are critical when provisioning infrastructure using Terraform, regardless of whether it’s with a cloud provider or on-premise solutions that Terraform supports. The five key commands in the Terraform workflow are: terraform init, terraform validate, terraform plan, terraform apply, and terraform destroy. This workflow is essential for implementing and managing any infrastructure with Terraform.

Terraform Init Command

$ terraform init

In Terraform, this is the initial command that must be executed whenever Terraform configuration files are created. You’ve written a Terraform configuration file using the Terraform language, which is currently unfamiliar to us. What we’ll do is initialize the working directory where these configuration files are located. This step will download plugins, which are essentially the providers set up in your configuration files. For now, remember that initializing in the working directory of your configuration files is crucial because it will download some providers, which you will see during the practical session.

Terraform Validate Command

$ terraform validate

The second aspect to consider is the use of “Terraform validate.” This function is designed to check the Terraform configuration files located in the specified directory. Its main purpose is to ensure that these files are syntactically correct and internally consistent. Therefore, after writing your configuration files, you can employ “Terraform validate” to confirm their validity.

Terraform Plan Command

$ terraform validate

This discussion focuses on the execution plan and its significance. Essentially, the execution plan outlines the infrastructure you intend to create or provide to your chosen cloud provider. This will be displayed in the command line interface. For instance, the plan might specify the creation of an EC2 instance or an Elastic IP followed by an EC2 instance. Whether the operation involves creating, updating, editing, deleting, or destroying resources, the execution plan will detail every action during the plan phase.

Terraform Apply Command

$ terraform apply

In the apply phase of Terraform, when you execute the `terraform apply` command, it processes the changes specified in your configuration files. These changes are then applied to your chosen cloud provider, which subsequently provisions the infrastructure according to those specifications.

Terraform Destroy Command

$ terraform destroy

If you need to delete any resources, such as an EC2 instance, you can use Terraform to target that specific instance or manage a broader scope. With Terraform, you have the flexibility to either destroy a single resource or handle multiple resources across your configuration files. If you decide to remove everything, simply execute terraform destroy. This command allows you to eliminate all infrastructure, whether it's hosted on the cloud or on-premises, by referring to the relevant configuration files.

Thank you for reading until the end. Before you go:

Paul Ravvich

--

--

Paul Ravvich
Tarraform using Amazon Web Services

Software Engineer with over 10 years of XP. Join me for tips on Programming, System Design, and productivity in tech! New articles every Tuesday and Thursday!