القائمة الرئيسية

الصفحات

Terraform workflow explained

Terraform Workflow

In this lecture, we will take a closer look at the Terraform workflow


the Terraform workflow consist basically of four main steps


1. creating the configuration file 

2. running the terraform init command 

3. running the terraform plan command 

4. running the terraform apply command.


Terraform workflow


let's see what happens in each step in more details


1. creating the configuration file 


- So, The first step to work with terraform, is to create a directory for your terraform project and then write the terraform configuration file usually called main.tf 


- In this configuration file, you will define the desired state of your infrastructure using HCL which stands for Hashicorp configuration language


- if you want to split the configuration into multiple files, you can do that by creating multiple files, all of them should be with the .tf extension

Terraform workflow - Create the configuration file


2. running the terraform init command 


- Once the configuration file are ready, now you have to run the terraform init command in your project directory.


- this command will initialize the working directory and install the required providers. in our situation, only one single provider is required which is the local provider


- these providers will enable terraform to interact with the infrastructure and let terraform create, update and destroy resources when needed


- By running this command, a folder called .terraform will be created in your terraform project directory, where the required providers will be stored.


- an important note to mention here: as long as you still work with the same providers, there no need to run the terraform init command again. the terraform init command should be run only if you start using a new provider and you have to run this command to install this new provider


We will learn more about providers in the upcoming lecture

Terraform workflow - Running the terraform init command


3. running the terraform plan command 


- after initializing the working directory and installing the required providers, now you can run the terraform plan command to generate the execution plan.


- this command will compare the current state of your infrastructure with the desired state defined in your configuration file, and shows you the changes that terraform will make to the infrastructure.


- it will tell you about the resources that will be either created, updated or destroyed. here one resource will created which a file and there is no resource that will be changed or destroyed


- The terraform plan command is not strictly required, so you can skip this step if you want


- But it is highly recommended as a best practice in the Terraform workflow. The primary purpose of the terraform plan command is to provide you with an execution plan before you actually apply the changes to your infrastructure 

Terraform workflow - Running the terraform plan command


4. running the terraform apply command


-Now, if the plan looks good and you are happy with that, you can apply the changes by running the terraform apply command. the terraform apply command will make the infrastructure up to date as you mention in the configuration file


- and by running this command, a file called terraform.tfstate will be created. this file contains information about the resources that Terraform has created and their current configuration


- we will learn about state in terraform in more details in a separate lecture

Terraform workflow - Running the terraform apply command


You can see more about that in this video





So that's an overview of the terraform workflow, 

Comments