Day 1: Introduction to Terraform and Terraform Basics

Join me in the Terra Week Challenge, where we'll explore Terraform together and master its concepts step by step

Day 1: Introduction to Terraform and Terraform Basics

Concepts💭

What is Terraform?

  • Terraform is like a magical spellbook for managing your cloud infrastructure.

  • Imagine you're a wizard, and instead of casting spells manually (clicking buttons in a web interface), you write down your spells (infrastructure configurations) in a special language.

  • This special language is HCL, i.e., HarshiCorp Configuration Language, which is a configuration language designed to be both human and machine-readable for use in infrastructure automation.

  • Terraform then reads this spellbook and performs all the magic for you.

  • It helps you create, update, and delete your cloud resources in a predictable and repeatable way. So, you write your infrastructure as code, and Terraform does the heavy lifting.

    How does Terraform simplify infrastructure provisioning?

    • Terraform simplifies infrastructure provisioning by automating it. Traditionally, setting up servers, databases, and networks is a manual process prone to errors.

    • Terraform ensures consistency: Once you define your infrastructure in code, you can recreate it the same way every time.

    • Example: If you need to replicate your web application in a new region, you change a few lines of Terraform code, and it deploys everything for you without manual steps.

How can you install Terraform and set up the environment for AWS, Azure, or GCP?

  • To get started with Terraform, follow these steps:

    1. Download Terraform from the official website (https://www.terraform.io/downloads.html).

    2. Install it on your computer.

    3. Set up API credentials for your cloud provider (AWS, Azure, GCP).

    4. Configure Terraform to use these credentials.

5 crucial terminologies of Terraform with examples.

  • Resource: A resource is something you want to create in your cloud provider, like an AWS EC2 instance. Example:

      resource "aws_instance" "example" {
        ami           = "ami-0c55b159cbfafe1f0"
        instance_type = "t2.micro"
      }
    
  • Provider: The provider is the cloud platform you're using, like AWS, Azure, or GCP. Example:

      provider "aws" {
        region = "us-east-1"
      }
    
  • Variable: Variables allow you to parameterize your Terraform configurations. Example:

      variable "instance_count" {
        description = "Number of EC2 instances to create"
        type        = number
        default     = 2
      }
    
  • Module: Modules are reusable sets of Terraform configurations. They help organize your code. Example:

      module "web_server" {
        source = "./modules/web"
        instance_count = var.instance_count
      }
    
  • Output: Outputs allow you to expose values from your Terraform configuration. Example:

      output "instance_ip" {
        value = aws_instance.example.private_ip
      }
    

Hands-on Terraform

  • Suppose you wanted to create a file on your local and write something on it.

  • We will go through 3 step journey to do this task.

    Step-1: Creating the terraform file

  • Here we used: the resource block along with the resource type - local_file and resource name - sample_file

  • filename - The path where we wanted to create our sample_poem.txt file

Step-2: terraform init

  • The terraform init gets your Terraform environment ready by preparing the workspace, downloading necessary tools (providers), and configuring storage for your project. It's the first step you take when starting a new Terraform project or when working on an existing one.

Step-3: terraform plan

  • In essence, terraform plan is your way of double-checking your infrastructure changes before making them, helping you avoid unintended consequences. It's a critical step in the Terraform workflow.

Step-4: terraform apply

  • So, terraform apply is the command you use to make your infrastructure changes real, turning your blueprint into a functioning environment. It's a crucial step in the Terraform workflow but should be used with caution, especially in production environments, to avoid unintended changes.

Output

  • Our sample_poem.txt is created now along with terraform.tfstate.

  • terraform.tfstate is a crucial file that Terraform uses to keep track of your infrastructure's current state and to make accurate decisions when applying changes to your infrastructure as code. It's like the journal that helps Terraform understand what's already been built and what needs to be constructed or modified.

This task is straightforward. As we go on, tasks will get more challenging. But for now, we're done for the day.

If you're just starting with Terraform like me, don't worry. Keep learning, keep experimenting, and together, we'll become DevOps wizards!

Happy Terraforming!