Introduction to Terraform and Terraform Basics

Introduction to Terraform and Terraform Basics

TerraWeek Day 1

What is Terraform

HashiCorp Terraform is an infrastructure as code tool that lets you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share. You can then use a consistent workflow to provision and manage all of your infrastructure throughout its lifecycle. Terraform can manage low-level components like compute, storage, and networking resources, as well as high-level components like DNS entries and SaaS features.

Terraform allows you to describe your complete infrastructure in the form of code. Even if your servers come from different providers such as AWS or Azure, Terraform helps you build and manage these resources in parallel across providers. Think of Terraform as connective tissue and common language that you can utilize to manage your entire IT stack.

how can Terraform help you manage infrastructure as code?

  • One of the main functions of Terraform is for public cloud provisioning on one of the major providers. Providing an IaC for services such as AWS and Azure has -- and will continue to be -- the main focus of Terraform. Terraform enables the use of these public clouds via a provider, a plugin that wraps existing APIs and languages like Azure Bicep and creates Terraform syntax.

  • The second main use for Terraform is to facilitate multi-cloud deployments. One of the main draws of Terraform is that it performs across all cloud providers simultaneously, unlike some of its other IaC competitors. The capability to deploy resources into multiple cloud providers is critical because engineers can utilize the same syntax without familiarizing themselves with multiple tools and technologies.

  • The third most common use of Terraform is deploying, managing, and orchestrating resources with custom cloud providers. A provider is a way in Terraform to wrap an existing API and convert it to the Terraform declarative syntax, and this can be done even if you’re not using AWS or another one of the major cloud services. Providers can also be written for internal use cases where you may desire to convert existing tools or APIs into Terraform.

In short, Terraform helps manage your entire IT ecosystem via IaC, whether it’s a single cloud, multi-cloud, or custom deployment.

IaCvs Configuration Management

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

IaC replaces standard operating procedures and manual effort required for IT resource management with lines of code. Instead of manually configuring cloud nodes or physical hardware, IaC automates the process infrastructure management through source code.

Here are several of the major key benefits of using an IaC solution like Terraform:

  • Speed and Simplicity. IaC eliminates manual processes, thereby accelerating the delivery and management lifecycles. IaC makes it possible to spin up an entire infrastructure architecture by simply running a script.
  • Team Collaboration. Various team members can collaborate on IaC software in the same way they would with regular application code through tools like Github. Code can be easily linked to issue tracking systems for future use and reference.
  • Error Reduction. IaC minimizes the probability of errors or deviations when provisioning your infrastructure. The code completely standardizes your setup, allowing applications to run smoothly and error-free without the constant need for admin oversight.
  • Disaster Recovery. With IaC you can actually recover from disasters more rapidly. Because manually constructed infrastructure needs to be manually rebuilt. But with IaC, you can usually just re-run scripts and have the exact same software provisioned again.

  • Enhanced Security. IaC relies on automation that removes many security risks associated with human error. When an IaC-based solution is installed correctly, the overall security of your computing architecture and associated data improves massively.

  • Plan and Apply: Terraform's "plan and apply" workflow is a powerful feature that ensures predictability and control over infrastructure changes. By running the terraform plan command, you can preview the proposed changes before applying them. This allows you to review and validate the modifications, avoiding any surprises or unintended consequences. Once you're satisfied with the plan, you can execute the changes with the terraform apply command.

While there are many other benefits of employing IaC, things like speed, accuracy, data visibility, and security are key reasons that organizations choose to implement solutions like Terraform.

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

To install Terraform and set up the environment for AWS, you need to follow a few steps. Here's a guide for each cloud provider:-

Installing and Setting Up Terraform for AWS:-

Step 1:- 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

Step 2:- Install the HashiCorp GPG key.

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

Step 3:- Verify the key's fingerprint.

gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint

Step 4:- 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

Step 5:- Download the package information from HashiCorp.

sudo apt update

Step 6:- Install Terraform from the new repository.

sudo apt-get install terraform

you can follow below the official page to install Terraform

Terraform Installation

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:-

Init -> plan -> apply

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

Step2:- 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.

If we got to aws console we can see EC2 up and running

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

  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. Resources:- 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 TYPE>.<NAME>.<ATTRIBUTE>

    When you do a ‘Terraform apply’ you create the infrastructure/resources defined within your resource block while maintaining a local state file to identify what all resources are created. This state file is updated every time you create new or deleted/modify the existing resources.

     resource "aws_instance" "example" {
       ami           = "ami-053b0d53c279acc90"
       instance_type = "t2.micro"
    
       tags = {
         Name = "my-ec2-instance"
       }
     }
    
  3. Modules:- Modules help you organize and reuse Terraform code. They allow you to encapsulate a set of resources and configurations into a standalone component that can be used multiple times across different infrastructure deployments. Modules can be created locally or sourced from external repositories. Here's an example of using a module to create an AWS S3 bucket

     module "s3_TF" {
       source = "terraform-aws-modules/s3-TF/aws"
    
       bucket_name = "TF-bucket"
       acl         = "private"
       region      = "us-east-1"
     }
    
  4. Variable: Variables in Terraform are used to parameterize your configuration. They allow you to define dynamic values that can be customized based on the environment or user inputs. Variables can have default values or be set externally when running Terraform.

     variable "region" {
       type    = string
       default = "us-west-2"
     }
    
  5. Output:- Outputs in Terraform allow you to expose certain values from your configuration to be displayed or used by other parts of your infrastructure. Outputs are useful for obtaining information about the infrastructure created by Terraform, such as IP addresses or resource identifiers.

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

    Devops#devops,#TerraWeek Day 1

    Thank you for reading!! I hope you find this article helpful!!

    if any queries or corrections to be done to this blog please let me know.

    Happy Learning!!

    Saikat Mukherjee

Thank You Images - Free Download on Freepik

Did you find this article valuable?

Support Saikat Mukherjee by becoming a sponsor. Any amount is appreciated!