TerraWeek Challenge

TerraWeek Challenge

Day 1: Introduction to Terraform and Terraform Basics

What is Terraform?

Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It enables you to define and provision infrastructure resources using declarative configuration files.

Why do we need Terraform?

🚀 Efficiency: Terraform allows you to automate infrastructure provisioning, making it faster and more consistent.

🔄 Reproducibility: By defining your infrastructure as code, you can easily recreate and scale your environments.

🔒 Consistency: Terraform ensures your infrastructure is always in the desired state, reducing configuration drift.

📚 Collaboration: Store your infrastructure code in version control for team collaboration and change tracking.

The Merits of Infrastructure as Code

  1. Automation: IaC streamlines the deployment of infrastructure, substantially reducing the need for manual interventions and the associated risks of human errors. This bolsters the dependability and efficiency of your infrastructure.

  2. Uniformity: By expressing infrastructure configurations through code, you ensure a consistent setup across diverse environments. The issue of configuration deviations, where environments stray from the intended state, becomes a thing of the past.

  3. Scalability: IaC simplifies the process of adjusting resource capacities. Through code adjustments, you can effortlessly expand or shrink your infrastructure to accommodate evolving demands, ensuring optimal resource allocation.

  4. Reproducibility: Your infrastructure can be systematically deployed across various environments, such as development, staging, and production. This consistency streamlines testing and deployment procedures.

  5. Collaboration: IaC encourages collaboration among team members. Infrastructure configurations can be version-controlled, shared, and developed collaboratively, fostering the sharing of knowledge and teamwork.

  6. Documentation: Infrastructure code serves as executable documentation, simplifying the comprehension and long-term maintenance of your infrastructure.

  7. Agility: Infrastructure modifications can be swiftly and efficiently executed, facilitating rapid iterations and deployments. This agility proves indispensable in dynamic and fast-paced development environments.

How does Terraform simplify infrastructure provisioning?

Terraform makes infrastructure provisioning a breeze:

  1. Declarative Configuration: Specify what resources you want and their desired state.

  2. Resource Abstraction: Works with multiple cloud providers and on-premises environments.

  3. Dependency Resolution: Terraform manages resource dependencies intelligently.

  4. State Management: Keeps track of the current infrastructure state.

Important Terraform Terminologies (with examples):

  1. Provider: A plugin that offers resource types for a specific infrastructure platform (e.g., AWS, Azure).

     provider "aws" {
       region = "us-east-1"
     }
    
  2. Resource: A specific infrastructure object managed by Terraform.

     resource "aws_instance" "demo1" {
       ami           = "ami-0c55b159cbfafe1f0"
       instance_type = "t2.micro"
     }
    
  3. Variable: Parameters used to make configurations more dynamic and reusable.

     variable "aws_region" {
       description = "The AWS region where resources should be provisioned."
       default     = "us-east-1"
     }
    
  4. Output: Exposes specific values from your infrastructure.

     output "instance_public_ip" {
       value = aws_instance.example.public_ip
     }
    
  5. State: The state file (terraform.tfstate) tracks the current infrastructure state.

     terraform {
       backend "s3" {
         bucket = "my-terraform-state"
         key    = "terraform.tfstate"
         region = "us-east-1"
       }
     }
    

♻Terraform Lifecycle

The Terraform lifecycle encompasses four crucial phases: initialization, planning, application, and destruction.

  1. Initialization (Terraform init): This phase initializes the local Terraform environment. Typically, it's executed just once per session to set up the necessary configurations.

  2. Planning (Terraform plan): During this step, Terraform compares the current Terraform state with the existing state in the cloud infrastructure. It constructs and presents an execution plan, which serves as a read-only blueprint for potential changes to the deployment. Importantly, it doesn't alter the actual deployment.

  3. Application (Terraform apply): In this phase, Terraform executes the previously generated plan. This operation has the potential to modify the deployment, bringing it in line with the defined configuration.

  4. Destruction (Terraform destroy): The destruction phase is responsible for removing all resources managed by the specific Terraform environment. It effectively cleans up and deletes the infrastructure components according to the Terraform configuration.

⚓Set Up of Terraform on AWS

  1. To install Terraform and set up the environment for AWS, you can follow these steps:

Here, we will be setting up Terraform in an AWS EC2 instance(Ubuntu AMI)

    # Download HashiCorp GPG key and store it as /usr/share/keyrings/hashicorp-archive-keyring.gpg
    wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg

    # Add HashiCorp repository to the sources list
    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 package list and install Terraform
    sudo apt update && sudo apt install terraform

    #Verify the installation using 
    terraform -- version

These commands will download HashiCorp's GPG key, add their repository to your system's package sources, update the package list, and finally install Terraform. Make sure you have appropriate permissions (e.g., sudo) to execute these commands on your system.

📑Create Day 01 Simple Project

So on Day 01 , let's start with a simple project

  1. To start your Terraform project, create a new directory

  2. To store your Terraform configuration files, create a new directory.

  3. Enter this directory's path at the command prompt or terminal.

  4. Create a Terraform configuration file: In your project directory, create a new file called main.tf. This file will contain your Terraform configuration.

  5. In main.tf file add the below HCL commands to create a file with automation.

      resource "local_file" "terraform_dev" {
              filename = "/home/ubuntu/sample_pro/demo.txt"
              content = "It's Day-1 TerraWeek challenge"
      }
    
  6. The code snippet creates a resource of type "local_file" with the name "devops_terraform". Here's the breakdown of the code:

    • resource: This keyword is used to define a resource in Terraform.

    • "local_file": It specifies the resource type, which in this case is a local file resource.

    • "devops_terraform": It provides a name or identifier for the resource instance, allowing it to be referenced elsewhere in the configuration.

    • filename: It specifies the path and name of the file to be created. In this case, it is set to "/root/terraform/terraform-local/automate-file.txt".

    • content: It defines the content that will be written to the file. Here, it is set to "It's Day-1 Terraweek challenge".

  7. Initialization: Start your Terraform project with terraform init. This command sets up your environment by fetching necessary provider plugins and configuring the backend for state storage. It ensures your Terraform environment is ready for infrastructure management

      terraform init
    

  8. Deployment: Deploy your infrastructure using terraform apply. Terraform will ask for confirmation; type "yes" and hit Enter to proceed with deployment.

     terraform plan 
     terraform apply
    

  9. Verification: After Terraform finishes applying your configuration, verify the successful deployment in your cloud provider's console.

  10. To Check for your newly provisioned infrastructure components to confirm everything is as expected, check whether the file has created or not

    So, we have completed our day 01 of the terraWeek Challenge by creating simple project.

Get started with Terraform today and simplify your infrastructure management journey. Happy provisioning! 🔧🚀 #Terraform #InfrastructureAsCode #DevOps #CloudComputing

Hashnode: https://hashnode.com/@NileshSahare

LinkedIn: www.linkedin.com/in/nilesh-sahare-102325148

Happy Learning :)

If you find my blog valuable, I invite you to like, share, and join the discussion. Your feedback is immensely cherished as it fuels continuous improvement. Let's embark on this transformative DevOps adventure together! 🚀