Terraform Made Easy: Learn from Scratch with Diagrams, Tricks, and Real Examples!
Start your Terraform journey with this beginner-friendly guide visuals, smart tricks, and zero fluff!
To understand what Terraform is and why it is used, you can read this blog. If you are already familiar with Terraform, then let's proceed with this blog.
Steps for installing Terraform:
- First, we will configure an EC2 instance on AWS, allocating a storage volume of 15 GB. Please refer to this blog for guidance on creating an EC2 Instance on AWS.
To establish an SSH connection with this instance, use the terminal:
To install Terraform, please visit the official Terraform documentation.
We will install all the necessary components on our Ubuntu Linux Server.
Please follow the steps below:
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
Install the HashiCorp GPG key.
- The GPG key is used to ensure the authenticity of the Terraform download. It is necessary to verify that the software is genuine and has not been tampered with.
wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null
- Verify the key's fingerprint.
gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint
- Now, we will add our key to the system so that the HashiCorp list is included in the sources list.
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list
- Download the package information from HashiCorp.
sudo apt update
- Now Install Terraform from the new repository.
sudo apt-get install terraform
terraform --version
You can now see the version of Terraform that has been downloaded and installed on your system.
What is HCL?
Terraform is a tool developed by HashiCorp, written in HashiCorp Configuration Language (HCL).
We will learn how to write in HashiCorp Configuration Language (HCL), which is JSON like object.
The extension for Terraform files is (.tf).
block —> resource, output, variable, data, etc.
Ex. resource*<block> <parameter>{*
args
}
parameter —> resource instance and resource name. The Resource Name is used by Terraform for its understanding and is not the same as our EC2 instance name. The instance name is specified in the argument section.
Ex. resource*<block>* aws_s3_bucket*<parameter> my_s3_bucket<resource name>{*
args
}
To identify a Terraform resource, the name assigned to it is referred to as the resource name.
arguments(configurations) —>
For example, when launching a new EC2 instance, a form appears for its creation. We fill out this form with details known as arguments or configurations, such as instance name, instance type, storage, keys, and more.
Lets take one example:
Terraform file creation:
- Create a Terraform file on our server:
mkdir terraform-practice
cd terraform-practice
vim main.tf #(Terraform File)
- Write code for file creation:
resource "local_file" "my_file" {
filename = "Devops.txt"
content = "This is Terraform auto generated file"
}
Terraform operates in several steps:
First, we create a
.tf
file.Next, we initialize the file. During initialization, Terraform scans the file, installs the necessary components, and creates the Terraform object.
Then, we plan the object. This step shows how the object will appear when executed.
The plan provides a preview, which is a rough output that doesn't yet exist but demonstrates how it will look. This is similar to a dry run.
If the preview is satisfactory, you can apply these changes to finalize them.
Steps to implement this file:
Terraform init: It will initialize Terraform in this particular directory.
Terraform init will initialize the folder with Terraform and install whatever the provider are required in your main.tf file.
terraform init
What is local here?
The term following hashicorp/"______" is referred to as providers.
When we specify the resource type "local_file," it indicates the local provider being used. For example, in "aws_s3_bucket," the provider is aws.
As soon as we run
terraform init
it will create.terraform
- If you look inside the .terraform directory, you will find all the providers listed there.
- Terraform plan: It provides a preview, which is a rough output that doesn't yet exist but demonstrates how it will look. This is similar to a dry run.
terraform plan
We have specified arguments such as content and filename. The elements we define ourselves are known as arguments, while the elements that are ( known after apply ) are referred to as attributes.
- Terraform apply: This command is used to implement the changes.
terraform apply
- Validations:
terraform state list #Terraform known file name
ls #File name in filesystem
local_file.my_file
: This is the Terraform reference name for the Devops.txt
file that we created.
- Terraform Destroy: This command allows us to remove our resources.
terraform destroy
Now, you can see that the Devops.txt file no longer exists after we run the destroy command.
In Terraform, you just need to understand blocks, parameters, and arguments. That’s it.
Interview Question:
Difference between arguments and attributes:
\==> Arguments are specified in the Terraform configuration file within a resource block. They define the desired state of the resource. Attributes, on the other hand, are details about the resource that become known after the changes are applied.
Happy Learning :)
Chetan Mohod ✨
For more DevOps updates, you can follow me on LinkedIn.