#TerraWeek Day 1

#TerraWeek Day 1

Β·

6 min read

Day 1: Introduction to Terraform and Terraform Basics

let's start...πŸš€πŸ˜ƒ

  • What is Terraform and how can it help you manage infrastructure as code?

    Ans:- Terraform is a tool that helps you manage your cloud (AWS, Azure, GCP etc.) resources and infrastructure in a simple, efficient way. It allows you to define your infrastructure as code, meaning you can write down exactly how you want your servers, databases, and other resources to be set up using HashiCorp Configuration Language (HCL). Then, Terraform takes care of creating and managing those resources for you, ensuring they match your specifications. This helps you automate and streamline the process of managing your infrastructure, saving time and reducing errors.

  • Why do we need Terraform and how does it simplify infrastructure provisioning?

    Ans:-

  • Infrastructure as Code (IaC): Terraform allows us to define our infrastructure in code using a declarative configuration language. This means we can specify the desired state of our infrastructure using simple, human-readable code rather than manually configuring resources through a web interface or command line.

  • Automation: With Terraform, we can automate the provisioning and management of infrastructure resources. Once we define our infrastructure configuration, Terraform handles the creation, updating, and deletion of resources automatically, saving time and reducing manual effort.

  • Consistency: Terraform ensures that our infrastructure remains consistent across different environments, such as development, staging, and production. By using the same Terraform configuration for each environment, we can avoid configuration drift and ensure that all environments are provisioned identically.

  • Scalability: Terraform makes it easy to scale our infrastructure up or down to meet changing demands. We can quickly add or remove resources as needed by updating our Terraform configuration and applying the changes, without having to manually provision or deprovision resources.

  • Multi-Cloud Support: Terraform supports multiple cloud providers, including AWS, Azure, GCP, and others, as well as on-premises infrastructure. This allows us to manage our infrastructure consistently across different environments and avoid vendor lock-in.

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

    Ans :-

    To install Terraform and set up the environment for AWS, you need to follow a few steps :-

    Installing and Setting Up Terraform for AWS:-

    --->official documentation for installing terraform for ubuntu machine<--- https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli

    --> Ensure that your system is up to date and you have installed the gnupg, software-properties-common, and curl packages installed. 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.

wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null

--> 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. The lsb_release -cs command finds the distribution release codename for your current system, such as buster, groovy, or sid.

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
  • After installing Set Up AWS Account:

    • If you don't already have an AWS account, sign up for one at https://aws.amazon.com/.

    • obtain your AWS Access Key ID and Secret Access Key from the AWS Management Console. Make sure you have appropriate permissions to create and manage AWS resources.

  • Configure AWS CLI:

    • Install AWS Command Line Interface (CLI) by following the instructions at aws.amazon.com/cli.

    • Run aws configure command and provide your AWS Access Key ID, Secret Access Key, default region, and output format (e.g., JSON).

Create AWS EC2 using Terraform:-

  • Firstly need to create a directory and then we need to create a file called main.tf

    Note:- the extension should be .tf

Now we need to run the below command:-

  • Step 1:- terraform init -This command will scan your .tf files in that folder and install all the required automation things

    • step-2 terraform plan :This command will create an execution plan for terraforming, the things that will be installed, the names, and the properties added.

Step 3:- terraform apply -The actual execution and automation happen in this command.

go your aws console and you can see EC2 up and running

if you are at learning stage, then after creating infrastructure successfully do terraform destroy .[Delete all the infrastructure resources that Terraform manages. It is a powerful command and should be used with caution because it permanently removes the resources, potentially leading to data loss or downtime if not used properly.] if you are forget to do terraform destroy or delete infra manually your resources will running and aws taking charges for that.

Explain the important terminologies of Terraform with the example at least (5 crucial terminologies).

  • Ans :-

  • 1. Provider: Terraform Providers are essentially plugins that Terraform installs to interact with the remote systems i.e. Azure/AWS/Google Cloud/ VMware and a lot of other vendors devices.

    Each Terraform Provider that is used in your Terraform script will allow for the creation of certain set of resources. Once a provider has been defined in your code, terraform will automatically go to Terraform public registry and install the required provider during initialization. If you haven’t used any Terraform provider within your script you won’t be able to manage or create any infrastructure. You can define more than one provider in your Terraform code.

 provider "aws" { 
    region = "us-east-1"
}
      • 2.Resource: Resources are usually defined within a construct called Resource Block. A resource block defines what type of resource we are going to create and the assigns it a unique name. A combination of resource type and resource name assigned can be later used to reference this resource somewhere else in the Terraform script.

        The parameters defined for the resource creation within a resource block are known as Resource Arguments. Every Terraform provider has its own documentation, describing its resource types and their arguments. In order to call a resource attribute somewhere else I the code you need to use the following syntax:

              resource "aws_instance" "example" {
                   ami = "ami-0c55b159cbfafe1f0"
                   instance_type = "t2.micro"
              }
        

        3.Module: A module is a container for multiple resources that can be managed as a group. Modules help organize and reuse Terraform configurations.

              module "s3_TF" {
                 source = "terraform-aws-modules/s3-TF/aws"
        
                 bucket_name = "TF-bucket"
                 acl         = "private"
                 region      = "us-east-1"
               }
        
        • 4.State: The state is a JSON file maintained by Terraform that keeps track of the current state of managed infrastructure resources. It stores metadata and attributes of resources created by Terraform.

        • Example: The state file may contain information about the resources created, such as their IDs, IP addresses, and configuration settings.

          5.Output: An output in Terraform is a way to extract and display information about resources after they have been created. Outputs are useful for sharing important information with other parts of your infrastructure or external systems.

            output "instance_public_ip" { 
          
                 value = aws_instance.example.public_ip 
          
            }
          
Β