Day 60- 90DaysOfDevOps

Terraform

Day 60- 90DaysOfDevOps

Hello Learners! Welcome back. We understood the concept of Configuration Management (CM) and Infrastructure as Code (IaC). We discussed the definitions, benefits, and differences of both in the Day54 challenge. Before the advent of IaC infrastructure management was typically a manual and time-consuming process. Most administrators face challenges like Manually configuring servers, Lack of version control, Slow provisioning, Limited automation, etc. IaC addresses these challenges by providing a systematic, automated and code-driven approach to infrastructure.

Moving our 90DaysOfDevOps journey towards mastering the automation of Infrastructure provisioning and management. In this challenge, We'll dive deep into the world of Infrastructure as Code (IaC). Mostly we created our infrastructure manually, but now it's time to automate this process using the IaC called Terraform, which is a more powerful tool that enables us to automate our processes for creating and managing infrastructure resources in a consistent and scalable manner. We'll explore Terraform, What it is, how it works, and why it's a game-changer for managing infrastructure. Let's start...

What is Terraform?

Terraform is an Infrastructure as Code (IaC) tool that empowers you to define and manage your infrastructure using a declarative configuration language. It allows for the provisioning and management of various resources, such as virtual machines, networks, and storage, in a repeatable, automated, and scalable way.

Task 1- Install terraform

Refer to the official Terraform installation guide for step-by-step instructions on how to install Terraform on your specific operating system.

Link:- https://developer.hashicorp.com/terraform/downloads

I am using an EC2 instance of Ubuntu to install Terraform. Use the below commands to install Terraform on Ubuntu. Confirm the installation using the terraform --version command.

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
sudo apt install terraform
terraform --version

Connect to your instance via SSH and using above mentioned commands try to install Terraform.

Terraform was installed successfully. Use the terraform --version command to check the terraform version.

Task 2- Answer the below Questions

1- Why do we use Terraform?

Terraform is a powerful tool used for Infrastructure provisioning and management. it automates the creation, modification, and version control of infrastructure resources across various cloud providers and on-premises environments. Defining Infrastructure in a declarative configuration file simplifies maintenance, scalability, and collaboration on complex infrastructure setups.

It provides multi-cloud support. It has large community support. State management, Versioning and Collaboration, Immutable Infrastructure, etc are the key features.

2- What is Infrastructure as Code (IaC)?

IaC refers to the practice of defining and managing infrastructure, such as virtual machines, networks, and storage, through code and automation, rather than manual processes. With IaC, we can use code to create, deploy, and manage infrastructure, allowing for consistent, repeatable, and predictable deployments.

See the Day54 challenge to read more

3- What is Resources?

In Terraform, a resource is a fundamental building block used to define and manage a specific component or object within an infrastructure provider, such as a VM, network interface, DB, or any other resource offered by the provider.

  • Each resource has a type, which determines the kind of infrastructure object it manages, and a set of configuration arguments, which supply settings for the resource's attributes.

  • Resources can be modularized, allowing you to create reusable Terraform components. This is particularly useful for commonly used infrastructure patterns.

  • Resources can be dependent on other resources. Terraform understands these dependencies and ensures resources are created or destroyed in the correct order.

4- What is a Provider?

Provider is a plugin that interfaces with a specific infrastructure platform or service to manage its resources. Providers are a fundamental component of Terraform that allows you to interact with various cloud providers, on-premises systems, or other infrastructure services in a standardized and consistent way.

A provider in Terraform is responsible for understanding API interactions and exposing resources. Each provider configures a specific service or platform like AWS, Azure, Google Cloud, GitHub, and many more.

By leveraging providers, Terraform users can define their infrastructure requirements in a declarative manner using a consistent configuration language, regardless of the underlying infrastructure provider, making it a powerful tool for multi-cloud and hybrid cloud environments.

5- What is the State file in Terraform? What's the importance of it?

The Terraform state file is a crucial component that helps Terraform understand the current state of the infrastructure being managed. It contains essential information about the resources being managed, their attributes, dependencies, and other relevant metadata. This state is used to plan and execute changes to the infrastructure accurately and to keep track of the current state to manage it effectively.

Importance:-

  • State Representation:- It represents the current state of the real-world infrastructure managed by Terraform.

  • Synchronization with Real Infrastructure:- Terraform reads the configuration and compares it with the state to determine what changes need to be made to the infrastructure.

  • Resource Drift and Conflicts:- The state file helps detect drift changes made directly to the infrastructure outside of Terraform. It compares the state with the real infrastructure and alerts if there are discrepancies which helps prevent conflicts and unexpected changes.

  • Security and Sensitivity:- The state may contain sensitive information, like credentials or private IP addresses. It's important to secure the state file appropriately, using backends that support encryption and access controls, to avoid unauthorized access to sensitive data.

  • Collaboration and Teamwork:- The state file allows multiple team members to work on the same infrastructure collaboratively. It ensures that everyone is working with the most up-to-date information about the infrastructure and helps maintain consistency across the team.

6- What is the Desired and Current State?

Desired State: The desired state is the state you specify in your Terraform configuration files (typically written in HashiCorp Configuration Language or HCL). It represents the infrastructure configuration you want to achieve.

Current State: Definition: The current state is the actual state of your infrastructure as tracked by Terraform. It reflects the real-world state of your resources based on the last Terraform run.

Thank you so much for taking the time to read till the end! Hope you found this blog informative.

Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.

Find me on:- Hashnode LinkedIn Github

Happy Learning!