The Terraform Journey: Installation, IaC, Resources, and State Unveiled

The Terraform Journey: Installation, IaC, Resources, and State Unveiled

Day 60 of #90DaysOfDevOps

Task 1

  • Install Terraform on your system.

Task 2

  • Why do we use terraform?

  • What is Infrastructure as Code (IaC)?

  • What is a Resource?

  • What is a Provider?

  • What is a State file in Terraform? What’s the importance of it?

  • What is the Desired and Current State?


Task 1: Installation of Terraform

Visit this official link for the installation of terraform: https://developer.hashicorp.com/terraform/install

For Ubuntu, follow the steps below:

Paste the below commands in the terminal line by line:

wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
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
sudo apt update

If you are using Ubuntu 23.10 then as of now while writing this blog, the version of Terraform for Mantic(Ubuntu 23.10) is corrupted or has bugs.

If you got an error, then try this command:

echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com lunar main" | sudo tee /etc/apt/sources.list.d/hashicorp.list

sudo apt update

sudo apt install terraform -y

Verify the installation by writing version command:

terraform --version

Terraform has been installed in your system.

Task 2

  1. Why do we use Terraform?

    • Terraform is a tool that allows users to describe their infrastructure as code.

    • It can be used to build, change, and version resources across cloud and on-prem environments.

  2. What is Infrastructure as Code (IaC)?

    • Infrastructure as Code (IaC) is a method of managing and supporting IT infrastructure using code instead of manual processes and settings. IaC is a key component of the DevOps software development approach.

    • We use Infrastructure as Code (IaC) to harden and maintain the state of the infrastructure.

  3. What is the Resource?

    • Resources are the most important element in the Terraform language.

    • Each resource block describes one or more infrastructure objects, such as virtual networks, compute instances, or higher-level components such as DNS records.

    • Resource Blocks document the syntax for declaring resources.

  4. What is the Provider?

    • A provider in Terraform is a plugin that enables interaction with an API.

    • This includes Cloud providers and Software-as-a-service providers.

    • The providers are specified in the Terraform configuration code. They tell Terraform which services it needs to interact with.

  5. What is a State file in Terraform? What’s the importance of it?

    • A Terraform state file is a JSON file that stores information about your infrastructure in a state file.

    • This state file keeps track of resources created by your configuration and maps them to real-world resources.

    • It's named terraform.tfstate and is created after running terraform apply.

    • Terraform state is also used to enforce idempotency. Idempotency is the ability to apply a configuration multiple times and achieve the same result every time.

  6. What is the Desired and Current State?

    • In Terraform, the desired state is the state you want your infrastructure to be in, as defined in your Terraform configuration files.

    • The current state is the actual state of the infrastructure, as represented in the Terraform state file.