Exploring Terraform: An Introduction and Essential Basics

Exploring Terraform: An Introduction and Essential Basics

Introduction:)

This is Day 54 of #90DaysOfDevOps, and today we embark on an exciting exploration into the world of Terraform. In this blog, we've navigated the essentials of Infrastructure as Code (IaC) and delved deep into Terraform, an open-source tool that empowers you to define, provision, and manage infrastructure seamlessly. From understanding key concepts to installing Terraform on your EC2 instance, we've laid the groundwork for a transformative journey into efficient and scalable infrastructure management.

🌐What is Infrastructure as Code (IaC)?

Infrastructure as Code (IaC) is an approach to managing and provisioning computing infrastructure through code and automation rather than manual configuration

It involves using declarative or imperative code to define and configure infrastructure components, such as servers, networks, databases, and more.

🌐What is Terraform?

Terraform is an open-source infrastructure as code (IAC) tool developed by HashiCorp. It allows users to define and provision infrastructure in a declarative configuration language, and then create, modify, and version that infrastructure efficiently.

With Terraform, you can define your infrastructure in code using HashiCorp Configuration Language (HCL) or JSON. This code can be versioned, stored, and shared using source control systems like Git. Terraform then interprets this configuration and uses it to provision and manage the specified infrastructure components, such as virtual machines, networks, databases, and more.

🛠️ How does Terraform help you manage infrastructure as code?

Terraform helps manage infrastructure as code by providing a declarative configuration language that allows you to define and provision infrastructure resources. Here's how it works:

  1. Declarative Configuration: Write configuration files in HCL or JSON to define the desired infrastructure state, including resources, properties, and relationships.

  2. Resource Provisioning: Once you've defined your infrastructure in the configuration files, Terraform compares this configuration against the current state of your infrastructure and identifies the necessary changes.

  3. Execution Plan: Generate a plan outlining actions needed to achieve the desired state, specifying resource creation, modification, or deletion.

  4. Apply Changes: After reviewing the plan, apply it to execute actions that align the infrastructure with the desired state.

  5. State Management: Utilize a state file to keep track of the infrastructure state, facilitating change tracking and resource management.

  6. Version Control Integration: Store Terraform configuration files in version control systems like Git for collaborative development, code review, and versioning.

  7. Multi-Cloud Support: Manage resources across various cloud providers (AWS, Azure, Google Cloud) as well as on-premises and third-party services.

  8. Modularization: Use Terraform modules to organize and reuse code for common infrastructure patterns, enhancing code maintainability and reusability.

Important terminology

  1. Variables: Terraform has input and output variables; these are key-value pairs. Input variables are used as parameters to input values at runtime to customize our deployments. Output variables are return values of a Terraform module that can be used by other configurations. Read our blog on Terraform Variables.

  2. Provider: Terraform allows users to provision their infrastructure on major cloud providers such as AWS, Azure, OCI, and others. A provider is a plugin that interacts with the necessary APIs to create, update, and delete various resources.

  3. Module: Any set of Terraform configuration files in a folder is a module. Every Terraform configuration has at least one module, known as its root module.

  4. State: Terraform records information about what infrastructure is created in a Terraform state file. With the state file, Terraform can find the resources it created previously, supposed to manage and update them accordingly.

  5. Resources: Cloud Providers provide various services in their offerings, they are referenced as Resources in Terraform. Terraform resources can be anything from compute instances, and virtual networks to higher-level components such as DNS records. Each resource has its attributes to define that resource.

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

The Terraform state file is like memory for Terraform. It remembers what resources it created and their current settings. This helps Terraform make the right decisions when you want to make changes to your infrastructure. It's like a blueprint that Terraform follows to keep everything in order.

Important aspects of Terraform state file:-

  • State Tracking

  • Dependency Resolution

  • Prevents Drift

  • Synchronization in Team Environments

  • Locking for Safety

  • Sensitive Data Handling

What is Desired and Current State?

Desired State and Current State in Terraform refer to the configurations of your infrastructure.

  • Desired State: This is what you define in your Terraform configuration files. It outlines how you want your infrastructure to look like - which resources should exist, their properties, and how they relate to each other.

  • Current State: This is the actual state of your infrastructure as tracked by Terraform in the state file. It reflects what resources currently exist in your cloud provider account, their current configurations, and their relationships.

🚀 Setting Up Terraform on Ubuntu

Here are the steps to follow when installing Terraform on an EC2 instance:

sudo apt-get update && sudo apt-get install -y gnupg software-properties-common

# Install the HashiCorp GPG key.
wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg

# Verify the key's fingerprint.
gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint

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

# update packages
sudo apt update

# install terraform
sudo apt-get install terraform

Conclusion:)

Congratulations on successfully installing Terraform on your EC2 instance! This marks the beginning of your journey into Infrastructure as Code. With Terraform, you now have the power to define, modify, and manage your infrastructure efficiently.


Thank you for diving into this blog with me! I trust you found the information both helpful and enlightening. To stay updated on the latest in DevOps 🚀, make sure to follow me. Remember, staying informed means staying ahead in the dynamic world of DevOps!

Feel free to connect with me on:

LinkedIn

Twitter

GitHub

For more updates and engaging discussions on DevOps, let's connect! 🚀 #DevOpsCommunity