TerraWeek Challenge - Day 1

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

Using tools for infrastructure as code (IaC), you can control infrastructure without a graphical user interface by using configuration files. IaC enables you to define resource configurations that you can version, reuse, and share, allowing you to develop, modify, and manage your infrastructure in a secure, dependable, and repeatable manner.

HashiCorp Configuration Language (HCL), or JSON, is a declarative configuration language that you use with Terraform to create your infrastructure. The resources, their properties, and their connections with one another are all described in this configuration, which also represents the desired state of your infrastructure. With the help of Terraform's provider ecosystem, which supports a wide range of cloud providers like AWS, Azure, Google Cloud, and many others, you can define infrastructure components such as virtual machines, networks, storage, load balancers, and more.

To deploy infrastructure with Terraform:

  • Scope: Identify the infrastructure for your project.

  • Author: Write the configuration for your infrastructure.

  • Initialize: Install the plugins Terraform needs to manage the infrastructure.

  • Plan: Preview the changes Terraform will make to match your configuration.

  • Apply: Make the planned changes.

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

  • Automation: Terraform lets you manage and provide infrastructure resources automatically. You can quickly create, edit, and remove infrastructure resources by using the Terraform CLI to run commands and scripts.

  • Reusability and modularity: The use of modules, which are reusable components of infrastructure setups, is encouraged by Terraform. Your code will become more modular and manageable as a result of the ability to abstract and encapsulate intricate infrastructure structures using modules.

  • Infrastructure as a Code: Enables automated testing of infrastructure improvements, collaboration, and version control. It also ensures infrastructure management that is dependable, repeatable, and auditable. Infrastructure is given a single truth source, simplifying administration and understanding.

  • Availability of Cross-Cloud: Across a wide range of platforms and situations, you can manage infrastructure resources. It gives you the flexibility and independence to select the best cloud service or provider for business requirements.

  • State management: Terraform keeps track of the state of your infrastructure in a state file. This file contains the mapping between the resources defined in your configuration and the real resources in your infrastructure. Terraform uses this state to plan and execute changes only where necessary, ensuring that your infrastructure matches the desired configuration.

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

The easiest way to install Terraform is to access the download documentation on the official Terraform website. Here, regardless of the OS (Operating System) we are using, we will find all the alternatives for installing Terraform.

links: developer.hashicorp.com/terraform/downloads

Prerequisites: Having an account on AWS or Azure or GCP

AWS: On Amazon Web Services (AWS), you can install Terraform on a virtual machine instance, such as an EC2 instance, running a compatible operating system. You would typically connect to the instance via SSH, update the package lists, download the latest release of Terraform from the official website, extract the downloaded package, move the Terraform binary to a directory included in the system's PATH variable (e.g., /usr/local/bin), and verify the installation by running the terraform version command.

GCP: For Google Cloud Platform (GCP), you can install Terraform on a Compute Engine instance. After launching the instance, you would connect to it via SSH, update the package lists, install any required packages like gnupg and software-properties-common, add the HashiCorp GPG key, add the HashiCorp repository to the system's package manager, update the package lists again, install Terraform using the package manager (e.g., apt-get), and verify the installation by running the terraform version command.

Azure: On Microsoft Azure, you can install Terraform on an Azure Virtual Machine (VM). Similar to other platforms, you would launch a VM with a compatible Linux-based operating system, connect to the VM via SSH, update the package lists, install any necessary packages like curl and unzip, download the latest release of Terraform from the official website, extract the downloaded package, move the Terraform binary to a directory in the system's PATH (e.g., /usr/local/bin), and verify the installation using the terraform version command.

Note: It varies from platform to platform-specific, so follow the documentation for the complete setup.

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

  1. Resource: They are the infrastructure components that you want to manage with Terraform. They include computing instances, virtual networks, databases, and more. Each resource has a provider-specific configuration that describes its properties.

     resource "aws_instance" "web" {
       ami           = "ami-a1b2c3d4"
       instance_type = "t2.micro"
     }
    
  2. Provider: They are the plugins that define and control the lifecycle of an individual cloud provider's or service's infrastructure resources.

  3. Modules: They are the reusable packages of Terraform code that allow you to define, configure, and share infrastructure resources as a single unit. They can be nested to form complex infrastructure architectures.

  4. State: It keeps track of the existing configuration and how resources are bounded to each other

  5. Plugins: They are the additions to the platform's fundamental features that let it communicate with other systems, services, and cloud providers.

How to setup Terraform in your local system and build your first configuration file:

Note: The access key and Secret Access key should not be shared and if lost can be regenerated for the same user.

Create our first configuration in VS Code using Terraform:

Nginx Docker Container Using Terraform:

Terraform file is always saved with the .tf extension, we can create different HCL configuration files defining resources, variables, and output depending upon our requirement following the clean code

For now let's create a main.tf

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 3.0.1"
    }
  }
}

provider "docker" {}

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}

resource "docker_container" "nginx" {
  image = docker_image.nginx.image_id
  name  = "tutorial"
  ports {
    internal = 80
    external = 8000
  }
}

Terraform workflow three steps :

Terraform init: Initializes a Terraform working directory, which downloads the necessary provider plugins and sets up the backend configuration.

Terraform plan: Create an execution plan that shows the changes Terraform will make to the infrastructure based on the current configuration. It helps identify additions, modifications, or deletions of resources.

Terraform apply: Applies the changes specified in the Terraform configuration to the infrastructure, creating, modifying, or deleting resources as necessary, based on the execution plan.

Nginx Server Running:

Thank you for reading the article and I appreciate the effort and time invested, do give me feedback for the same!