Day 60 - Terraform

Day 60 - Terraform

Day 60 of 90daysofdevops

What is Terraform?

Terraform is an infrastructure as code (IaC) tool that allows you to create, manage, and update infrastructure resources such as virtual machines, networks, and storage in a repeatable, scalable, and automated way.

Task 1

Install Terraform on your system

  • Launch an instance and connect via SSH.

  • Refer to this link and install Terraform.

  • To verify HashiCorp's GPG signature and install HashiCorp's Debian package repository.

      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
    
  • Add the official HashiCorp repository to your system.

      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
    

  • Install Terraform from the new repository.

      sudo apt-get install terraform
    

  • Verify the installation.

Task 2

Why do we use terraform?

Here are some key reasons why we use Terraform:

Infrastructure as Code (IaC) Approach

  • Terraform allows you to define your infrastructure resources, such as virtual machines, networks, storage, and more, using a simple and declarative configuration language (HCL).

  • This IaC approach makes your infrastructure configurations versionable, repeatable, and maintainable, similar to application code.

  • You can store your infrastructure configurations in version control systems like Git, enabling collaboration, code reviews, and change tracking.

Cloud Agnostic Provisioning

  • Terraform is cloud agnostic, meaning it can provision and manage infrastructure resources across various cloud providers (e.g., AWS, Azure, GCP), as well as on-premises environments.

  • It provides a consistent and unified interface to provision and manage resources, abstracting away the underlying cloud provider APIs and complexities.

  • This enables you to have a single set of infrastructure configurations that can be applied across multiple cloud platforms.

Infrastructure Consistency and Reproducibility

  • With Terraform, you can define infrastructure resources, dependencies, and configurations as code, ensuring consistency across environments.

  • You can provision and replicate entire infrastructure setups, including networks, security groups, and server configurations, with a single command.

  • Terraform's state management tracks the current state of your infrastructure, allowing you to make incremental changes and track drifts over time.

Plan, Apply, and Destroy Operations

  • Terraform provides a powerful planning phase that allows you to preview changes before actually applying them.

  • The "terraform plan" command shows the resources that will be created, modified, or destroyed, helping to identify potential issues or unintended changes.

  • Once satisfied, you can apply the changes using the "terraform apply" command, which provisions or modifies the infrastructure resources as defined in your configuration.

  • When infrastructure is no longer needed, Terraform's "terraform destroy" command allows you to tear down all provisioned resources, ensuring clean-up and cost optimization.

Community and Ecosystem

  • Terraform has a large and active community that contributes to the development of providers, modules, and extensions.

  • You can leverage a wide range of existing provider plugins and community-maintained modules to extend Terraform's functionality and manage additional resources.

  • The Terraform Registry provides a centralized repository for sharing and discovering modules and provider plugins, enhancing reusability and collaboration.

What is Infrastructure as Code (IaC)?

  • Infrastructure as Code (IaC) is an approach to provisioning and managing infrastructure resources using machine-readable configuration files or scripts.

  • It treats infrastructure configuration in the same way as application code.

  • IaC enables infrastructure to be defined, versioned, and deployed through automation.

  • Declarative configuration files describe the desired state of the infrastructure, specifying resources, properties, relationships, and dependencies.

  • Infrastructure configurations are stored in version control systems like Git, facilitating collaboration, change tracking, and rollback to previous versions.

  • IaC tools automate the provisioning and management of infrastructure resources, reducing manual effort and increasing repeatability.

  • Infrastructure setups can be consistently replicated across environments, ensuring identical configurations and minimizing configuration drift.

  • IaC enhances infrastructure scalability by providing the ability to create, modify, and destroy infrastructure through code-based commands.

  • IaC promotes collaboration, agility, and reliability in managing infrastructure deployments.

What is a Resource?

In Terraform, a resource refers to a specific infrastructure component or service that you want to manage. It represents an entity that Terraform can create, modify, or destroy to provision and manage your infrastructure. Resources can include virtual machines, storage containers, network interfaces, databases, load balancers, DNS records, and more.

What is Provider?

In Terraform, a provider is a plugin that acts as an interface between Terraform and a specific cloud platform, service, or infrastructure technology. Providers are responsible for managing the lifecycle of resources and handling interactions with the underlying APIs or infrastructure services.

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

In Terraform, a state file is a JSON-formatted file that keeps track of the current state of your infrastructure. It records the metadata and attributes of the resources managed by Terraform, including their dependencies and relationships. The state file is created and updated by Terraform during the execution of terraform apply or terraform import commands.

What is Desired and Current State?

In Terraform, the desired state refers to the configuration that you have defined in your Terraform files (*.tf) to describe the infrastructure resources you want to create and manage. It represents the ideal or intended state of your infrastructure. The desired state is defined in terms of resource types, attributes, dependencies, and other configurations.

On the other hand, the current state refers to the actual state of your infrastructure resources as tracked and recorded in the Terraform state file. It reflects the current state of the resources provisioned by Terraform, including their attributes, relationships, and dependencies. The current state is updated and managed by Terraform during the execution of the terraform apply or terraform import commands.

Thank you for reading!!
~Shreya Gupta

Great initiative by the #trainwithshubham community. Thank you Shubham Londhe

#devops #90daysofdevops #terraform