❄️Introduction to Terraform, Its basics and Setup AWS Environment.

❄️Introduction to Terraform, Its basics and Setup AWS Environment.

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

Terraform is an open-source infrastructure as code (IaC) software tool created by HashiCorp. It allows users to define and provision infrastructure using a high-level configuration language known as HashiCorp Configuration Language (HCL) or JSON.

Here's how Terraform can help you manage infrastructure as code:

  1. Declarative Configuration: With Terraform, you define the desired state of your infrastructure using a configuration file. Terraform then compares this desired state with the current state of your infrastructure and makes the necessary changes to achieve the desired state.

  2. Infrastructure Provisioning: Terraform can create, update, and delete infrastructure resources such as virtual machines, networks, storage, and more across various cloud providers (AWS, Azure, Google Cloud, etc.) and on-premises environments.

  3. Dependency Management: Terraform manages dependencies between infrastructure resources, ensuring that resources are created or updated in the correct order based on their dependencies.

  4. State Management: Terraform keeps track of the state of your infrastructure, which allows it to determine what changes need to be made to achieve the desired state. The state file is used to map your configuration to real-world resources.

  5. Execution Plans: Before making any changes to your infrastructure, Terraform generates an execution plan that shows what actions it will take. This helps you understand the impact of changes before applying them.

  6. Immutable Infrastructure: Terraform promotes the concept of immutable infrastructure, where infrastructure is treated as disposable and can be easily recreated from scratch using code. This helps in maintaining consistency and reliability of your infrastructure.

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

We need Terraform because managing infrastructure manually can be time-consuming, error-prone, and difficult to scale. Here's how Terraform simplifies infrastructure provisioning:

  1. Automation: Terraform automates the process of provisioning and managing infrastructure. Instead of manually configuring servers, networks, and other resources, you can define your infrastructure as code, which can be version-controlled, tested, and easily shared.

  2. Consistency: By using Terraform, you ensure that your infrastructure is consistent across all environments (development, staging, production). You define your infrastructure once and then apply the same configuration to different environments, reducing the chances of configuration drift.

  3. Scalability: Terraform helps you scale your infrastructure easily. You can define complex infrastructure configurations that include multiple servers, load balancers, databases, and other resources, and Terraform will provision them as needed.

  4. Change Management: Terraform provides a way to manage changes to your infrastructure. Instead of making ad-hoc changes manually, you can update your Terraform configuration and apply those changes in a controlled manner.

  5. Resource Dependency Management: Terraform automatically manages dependencies between resources. For example, if you define a load balancer that depends on a set of servers, Terraform will ensure that the servers are created before the load balancer.

  6. Infrastructure as Code (IaC): Terraform allows you to define your infrastructure as code, which means you can use programming constructs to define your infrastructure. This makes it easier to manage and maintain your infrastructure over time.

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

Sure, here are five important terminologies in Terraform along with examples:

  1. Provider: A provider is responsible for managing the lifecycle of a resource, such as creating, reading, updating, and deleting resources. Providers are responsible for translating the Terraform configuration into API calls for the underlying infrastructure. Example:

     provider "aws" {
       region = "us-west-2"
     }
    
  2. Resource: A resource is a piece of infrastructure that Terraform manages, such as an AWS EC2 instance, a Google Cloud Storage bucket, or a Kubernetes deployment. Resources are declared in Terraform configuration files using a resource block. Example:

     resource "aws_instance" "example" {
       ami           = "ami-0c55b159cbfafe1f0"
       instance_type = "t2.micro"
     }
    
  3. Variable: Variables allow you to parameterize your Terraform configurations. They can be used to define values that are reused across your configuration files or to pass values dynamically at runtime. Example:

     variable "aws_region" {
       default = "us-west-1"
     }
    
     provider "aws" {
       region = var.aws_region
     }
    
  4. Output: Outputs are used to extract information from your Terraform configuration. They can be used to display values such as IP addresses, endpoint URLs, or other information that you want to use outside of Terraform. Example:

     output "instance_ip" {
       value = aws_instance.example.public_ip
     }
    
  5. Module: Modules allow you to encapsulate reusable Terraform configurations. They can be used to define a set of resources that can be reused across multiple projects or environments. Modules can have input variables and output values. Example:

     module "web_server" {
       source = "./modules/web_server"
       instance_count = 2
     }
    

These are some of the crucial terminologies in Terraform that are essential to understanding and working with Terraform configurations.

❄️How can you install Terraform Step by Step?

☑️First you should install unzip package

sudo apt install unzip

☑️To install the AWS CLI, run the following commands.

$ curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

☑️Then 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.

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

❄️Setup the Environment for AWS.

☑️First, Create the IAM User

☑️Attach a policy to the IAM user - AdministratorAccess

☑️Review and create the IAM user

Click "Create user" to create the IAM user

☑️Access key and secret key

☑️Configure AWS CLI with the IAM user credentials.

aws configure

Enter the Access Key ID, Secret Access Key, default region name, and default output format when prompted.

☑️On creating New Instance you'll get AMI ID.

☑️Now create terraform file name asmain.tf

terraform {
 required_providers {
        aws = {
        source  = "hashicorp/aws"
        version = "5.41.0"
    }
  }
        required_version = ">= 1.2.0"
}

provider "aws" {
region = "us-west-1"        #Name of the region
}

resource "aws_instance" "launch-instance" {
        count = 2     #How much EC2 instance you want to create.
        ami = "ami-05c969369880fa2c2"    #AMI ID

        instance_type = "t2.micro"
        tags = {
     Name = "Terra-1"     #Name of the Instance
  }
}

output "instance_pub_ip" {
        value = aws_instance.launch-instance[*].public_ip
}

☑️Initialize the Terraform configuration

terraform init

☑️Creates an Execution plan

terraform plan

☑️Apply the Terraform configuration

terraform apply

☑️You'll get the Output

☑️Check in the Region, the EC2 instances is running

I think this blog will be quite valuable, offering unique viewpoints and introducing new and engaging ideas. 🙏

😊 Happy learning!

Did you find this article valuable?

Support PratikM's Blog by becoming a sponsor. Any amount is appreciated!