Day01-TerraWeek Challenge

Day01-TerraWeek Challenge

💥Introduction:

In this blog, we'll learn the fundamentals of Terraform, explore its key terms, discover its immense utility for Infrastructure as Code (IaC) ⚙️, and wrap things up with a step-by-step guide to installing Terraform and configuring the AWS environment, complete with creating an S3 bucket as an example.

💥Tasks:

🚀 What is Terraform?

  • Terraform, an open-source Infrastructure as Code (IaC) tool 🛠️, enables you to describe and deploy infrastructure assets by utilizing a declarative configuration language. In simpler words, it empowers you to use code 💻 to generate and oversee your infrastructure components, including virtual machines, databases, networks, and other resources.

How can it help you manage infrastructure as code?

  • Terraform simplifies managing infrastructure as code (IaC) by offering:

    • 📜 Declarative Configuration: You describe what you want, not how to get it.

    • 🔄 Reproducibility: Ensures consistent environments means You can apply the same configuration multiple times to create identical environments, reducing the risk of configuration drift.

    • 🧩 Modularity: Encourages reusable building blocks. You can break down your infrastructure into reusable modules(Terraform configurations packages), making it easier to manage and scale your infrastructure components.

    • 📦 State Management: Terraform maintains a state file that keeps track of the current state of your infrastructure. This allows it to calculate and apply only the necessary changes when you modify your configuration

    • 🧰 Plugin Ecosystem: Terraform supports a wide range of providers, including cloud platforms (AWS, Azure, GCP), on-premises systems, and third-party services. This extensive ecosystem enables you to manage diverse infrastructure resources from a single configuration.

🚀Why do we need Terraform?

  • We need Terraform to efficiently manage infrastructure because it automates provisioning, enforces consistency, and streamlines infrastructure operations, reducing manual effort and errors. ⚙️💻

✨How does it simplify infrastructure provisioning?

  • Terraform simplifies infrastructure provisioning with its declarative approach, represented by code 🏗️.

    1. Declarative Configuration: You specify what you want (desired state) rather than how to achieve it, making it intuitive and less error-prone.

    2. Resource Automation: Terraform automates the creation, modification, and deletion of infrastructure resources, saving time and effort 🤖.

    3. Dependency Management: It figures out the order to provision resources based on their dependencies, ensuring a smooth process 🧩.

    4. Workflow Clarity: The terraform apply process provides a clear, step-by-step execution, helping you understand and verify each change 🔄.

🚀Explain the important terminologies of Terraform with the example.

Below are some important terminologies in Terraform,

  • 🏗️ Resource: A resource is a declarative representation of an infrastructure object, such as an AWS EC2 instance

    • Example:

        resource "aws_instance" "example" {
          ami           = "ami-0c55b159cbfafe1f0"
          instance_type = "t2.micro"
        }
      
  • 📦 Provider: A provider is a plugin that manages the lifecycle of resources. It connects Terraform to a specific cloud or service.

    • Example:

        provider "aws" {
          region = "us-east-1"
        }
      
  • 🔌 Module: A module is a self-contained package of Terraform configurations that can be reused to create infrastructure resources.

    • Example:

        module "web_server" {
          source = "./modules/web"
        }
      
  • 🔐 Remote State: Storing the Terraform state remotely, often in a shared location or service, to enable collaboration and state locking.

    • Example: Storing state in an AWS S3 bucket.
  • 🔐 State Locking: Preventing multiple users or processes from modifying the same state file simultaneously to avoid conflicts.

    • Example: Using DynamoDB to lock the state file in AWS.
  • 🔄 State: Terraform keeps track of the state of your infrastructure. It's stored in a state file and used to plan and apply changes.

    • Example:

      • State file: terraform.tfstate

How can you install Terraform?

  • You will use these packages 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 and Verify the key's fingerprint.

      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
    
  • Add the official HashiCorp repository to your system. and Install the Terraform from the new repository

      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-get install terraform
    

Verify the terraform installation

ahmed@master:~$ terraform -v
Terraform v1.5.7
on linux_amd64

🚀Set up the environment for AWS and S3 demo example

  • First, we have to install the "awscli" package in our system

      sudo apt-get install awscli -y
    
  • Setup the "awscli" to your desired AWS account. Video Link is attached

    awscli-configure.

  • Now make a directory for terraform "mkdir terraform_practice" and create a file name "vi aws_provider.tf" inside it

    •             # inform terraform to fetch the aws plugin with versiion greater than 5 
                  terraform {
                    required_providers {
                      aws = {
                        source  = "hashicorp/aws"
                        version = "~> 5.0"
                      }
                    }
                  }
      
  • create another file named "vi main.tf" and paste the below code

    •             #intializing aws provider
                  provider "aws" {
                  # below mentioned region is N-virgina
                      region = "us-east-1"
      
                  }
      
                  #create a s3 bucket
                  resource "aws_s3_bucket" "test-s3" {
                  # chnage the name of bucket but it must be unique
                    bucket = "ahmed-tf-bucket001"
                  }
      
    • and last run the following commands to create a bucket named "ahmed-tf-bucket001".

    •             # install the plugins and modules
                  terraform init
      
                  # to see what terraform will do
                  terraform plan
      
                  # apply the configuration
                  terraform apply
      
    • terraform will create a bucket as follow

    •             ahmed@master:~/TWS-Project/Terraform$ terraform apply
      
                  Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
                    + create
      
                  Terraform will perform the following actions:
      
                    # aws_s3_bucket.test-s3 will be created
                    + resource "aws_s3_bucket" "test-s3" {
                        + acceleration_status         = (known after apply)
                        + acl                         = (known after apply)
                        + arn                         = (known after apply)
                        + bucket                      = "ahmed-tf-bucket001"
                        + bucket_domain_name          = (known after apply)
                        + bucket_prefix               = (known after apply)
                        + bucket_regional_domain_name = (known after apply)
                        + force_destroy               = false
                        + hosted_zone_id              = (known after apply)
                        + id                          = (known after apply)
                        + object_lock_enabled         = (known after apply)
                        + policy                      = (known after apply)
                        + region                      = (known after apply)
                        + request_payer               = (known after apply)
                        + tags_all                    = (known after apply)
                        + website_domain              = (known after apply)
                        + website_endpoint            = (known after apply)
                      }
      
                  Plan: 1 to add, 0 to change, 0 to destroy.
      
                  Do you want to perform these actions?
                    Terraform will perform the actions described above.
                    Only 'yes' will be accepted to approve.
      
                    Enter a value: yes
      
                  aws_s3_bucket.test-s3: Creating...
                  aws_s3_bucket.test-s3: Creation complete after 6s [id=ahmed-tf-bucket001]
      
    • you can confirm this by follow command or check the s3 via console

    •             ahmed@master:~/TWS-Project/Terraform$ aws s3 ls
                  2023-09-10 18:44:14 ahmed-tf-bucket001
      
  • This serves as a demonstration, and now, please use the provided command to remove the S3 bucket

    •             terraform destroy
      

💥Conclusion:

In this article, we've explored the basics of Infrastructure as Code (IaC) and why Terraform is a beloved tool for infrastructure handling. 🏗️We've also guided you through the crucial process of installing Terraform on your system and creating the AWS S3 bucket via Terraform.

By becoming proficient in Terraform, you'll simplify infrastructure management, boost collaboration, and guarantee that your infrastructure stays uniform, dependable, and primed for growth. 🚀🌐