Day02-TerraWeek Challenge

Day02-TerraWeek Challenge

👋🏻Introduction:

📚 HCL, HashiCorp Configuration Language🏗️

Imagine HCL (HashiCorp Configuration Language) as the storyteller of the Terraform world🌍. It's the special language that helps you describe and define your infrastructure, just like an author weaves a tale with 📖words.

Here's the magic: Instead of writing tedious scripts to create servers, databases, or networks, you use HCL to simply tell Terraform what you want.🧙‍♂️For instance, you might say, "I want two web servers, a database, and a load balancer." Terraform, armed with HCL, then goes off to make it happen. 🚀

But wait, there's more! HCL allows you to add extra details, like server sizes or network configurations, all in a readable, easy-to-understand format. So, think of HCL as the ✍️pen and paper that brings your infrastructure story to life, making it clear, organized, and 💫 powerful.

🎗️HCL Blocks, Parameters & Arguments:

  • 📜 HCL Syntax:

    • Imagine HCL as the architect's blueprint 🏢 for building your digital infrastructure. It's like the language your computer can understand to create and manage resources, just as an architect's plans guide construction workers.
  • 🧱 Blocks:

    • Think of HCL as building blocks. You define your infrastructure by stacking these blocks, each with its purpose, like Lego pieces forming a structure.

      In Terraform configuration, you'll typically have blocks for resources, providers, and variables that start with { and end with }. It's like saying, "Here's the plan for our server!"

    • Example: the "aws_instance" resource block

      •   #"resource" signifies the block type
          #"aws_instance" specific type of resource/service on which you want terraform to interact
          #"myec2" define the name of resource as a object for terraform  
          resource "aws_instance" "myec2" {
            ami           = "ami-0c55b159cbfafe1f0"
            instance_type = "t2.micro"
          }
        

📜 Parameters:

  • Parameters are defined within a block and serve as attributes that enable the fine-tuning of configurations for that particular block type. These parameters are expressed as key-value pairs enclosed within the block.

  • For instance, in the example provided, "ami" and "instance_type" are parameters in the "aws_instance" resource block.

    •   resource "aws_instance" "example" 
        {
        # Value before "=" is Parameter
          ami           = "ami-0c55b159cbfafe1f0"
          instance_type = "t2.micro"
        }
      
  • 🔍 Arguments:

    • Arguments are like the details you give to a recipe to make your dish just the way you want it. In simple words, arguments are used to provide specific values to parameters within a block

    • Example:

      • In the below example, the values assigned to parameters (ami & instance_type) after the "=" are arguments.

          resource "aws_instance" "example" 
          {
          # Value after  "=" is Argument
            ami           = "ami-0c55b159cbfafe1f0"
            instance_type = "t2.micro"
          }
        

👉🏻Variables, Data Types & Expressions in HCL

  • 🧩Variables:

    • In terraform, variables are defined by a variable block in HCL. By defining variables in HCL, you can reuse them throughout the configuration.

      • Example:

        • Create a new file with the name "variables.tf" and define variable resource block below

        •   # variable block
            variable "instance_count" {
            # we define description of instance_count variable 
               description = "The number of instances to launch"
            # define type of variable is it number or string   
                type        = number
            # define its default value   
                default     = 2
             }
          
  • 🤖Using variables in HCL:

    • Here is a demo example of using variables in Terraform configuration.

      • Example:

        •   resource "aws_instance" "example" 
            {
              ami           = "ami-0c55b159cbfafe1f0"
              instance_type = "t2.micro"
             # count will pick the default value of instance_count variable
              count         = var.instance_count
            }
          
  • 🧨HCL Data Types and Expression:

  • HCL (HashiCorp Configuration Language) data types and expressions are essential elements used in Terraform configurations to define and manipulate infrastructure resources. Let's explore them:

    • Example:

      • String Data Type:

        •   variable "message" {
              type    = string
              default = "Hello, World!"
            }
          
      • Number Data Type:

        •   variable "count" {
              type    = number
              default = 42
            }
          
      • Boolean Data Type:

        •   variable "enabled" {
              type    = bool
              default = true
            }
          
      • List Data Type:

        •   variable "fruits" {
              type    = list(string)
              default = ["apple", "banana", "cherry"]
            }
          
      • Map Data Type:

        •   variable "person" {
              type    = map(string)
              default = {name = "John", age = "30"}
            }
          
      • Interpolation:

        •   resource "aws_instance" "example" {
              ami           = "ami-0c55b159cbfafe1f0"
              instance_type = var.instance_type
            }
          
      • Arithmetic Expressions:

        •   variable "result" {
              type    = number
              default = 2 + 2
            }
          
      • Comparison Expressions:

        •   variable "is_greater" {
              type    = bool
              default = 5 > 3
            }
          
      • Conditional Expressions

        •   variable "instance_type" {
              type    = string
              default = var.environment == "production" ? "t2.large" : "t2.micro"
            }
          
      • Functions

        •   variable "selected_fruit" {
              type    = string
              default = element(var.fruits, 1)
            }
          

🪶 Practice Terraform Configuration:

Now that we have covered the basics of Terraform configurational language HCL. Let’s deploy a nginx docker container with Terraform.

  • Docker Provider:

    • The Docker provider allows you to manage Docker containers and related resources

      •   # terraform block to tells to install specific provider 
          terraform {
            required_providers {
              docker = {
                source  = "kreuzwerker/docker"
                version = "3.0.2" }
                               }    }
          # provider block to intialize docker
          provider "docker" {}
        
          # resource block for docker to get image
          resource "docker_image" "nginx" {
            name = "nginx:latest"
          }
          # reource block for docker to run container
          resource "docker_container" "nginx_container" {
            name  = "nginx_container"
            image = docker_image.nginx.name
        
            ports {
              internal = 80
              external = 8080
            }
          }
        
  • Useful Commands:

    • terraform plan: Command to preview changes before applying them.
        • terraform apply: Command to apply changes to deploy infrastructure.
        • terraform destroy: Command to destroy resources created by the terraform.
        • terraform state: Command to view the current state of your infrastructure.
        • terraform validate: Command to check your configuration file for syntax errors.
        • terraform fmt: Command to format your configuration file.
      

🌟Conclusion:

Terraform is a powerful Infrastructure as Code (IaC) tool that simplifies and automates infrastructure management. With its declarative approach and clear syntax, Terraform allows you to define, provision, and manage resources across various cloud providers and services.