💠Familiarizing with HCL Syntax in Terraform & Tasks.

💠Familiarizing with HCL Syntax in Terraform & Tasks.

⚙️Task 1: Familiarize yourself with HCL syntax used in Terraform

In Terraform, HashiCorp Configuration Language (HCL) is used to define infrastructure as code. HCL is a declarative language that allows you to describe the desired state of your infrastructure. Here's an overview of HCL blocks, parameters, and arguments, as well as the types of resources and data sources available in Terraform:

📌HCL Blocks

  • HCL uses blocks to define different components of your infrastructure.

  • Blocks are defined using curly braces {} and contain key-value pairs to specify configuration settings.

  • Blocks are used to define resources, data sources, variables, outputs, providers, etc.

📌Parameters and Arguments

  • Parameters are the settings or configurations you define within a block.

  • Arguments are the actual values assigned to parameters.

📌Example of a Resource Block in Terraform

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}
  • resource is the block type.

  • aws_instance is the resource type.

  • example is the resource name (used for referencing this resource in other parts of the configuration).

  • ami and instance_type are parameters.

  • "ami-0c55b159cbfafe1f0" and "t2.micro" are arguments assigned to the parameters.

📌Types of Resources in Terraform

  1. Compute Resources: Examples include instances, autoscaling groups, and load balancers.

  2. Networking Resources: Examples include VPCs, subnets, route tables, and security groups.

  3. Storage Resources: Examples include EBS volumes and S3 buckets.

  4. Database Resources: Examples include RDS instances and DynamoDB tables.

  5. Provider Resources: Resources provided by the Terraform providers themselves, such as AWS, Azure, Google Cloud, etc.

📌Types of Data Sources in Terraform

  • AWS Data Sources: Examples include aws_ami, aws_subnet, aws_vpc, etc.

  • Azure Data Sources: Examples include azurerm_resource_group, azurerm_virtual_network, etc.

  • Google Cloud Data Sources: Examples include google_compute_instance, google_compute_network, etc.

  • Other Data Sources: Terraform supports data sources for various other systems, such as DNS records, Docker containers, GitHub repositories, etc.

Data sources allow you to fetch information from existing resources or systems to use in your Terraform configuration.

⚙️Task 2: Understand variables, data types, and expressions in HCL

In HashiCorp Configuration Language (HCL), variables are used to define values that can be reused throughout your configuration. HCL supports several data types, including string, number, bool, list, map, and object. You can also use expressions to manipulate and combine values.

📌Define a Variable in variables.tf

Create a file named variables.tf and define a variable. For example, let's define a variable named output_file_path of type string.

variable "output_file_path" {
  type    = string
  default = "output.txt"
}

📌Use the Variable in main.tf

Create a file named main.tf and use the variable in a local_file resource.

resource "local_file" "output_file" {
  content  = "Hello, world!"
  filename = var.output_file_path
}

📌Initialize and Apply the Configuration

Run terraform init to initialize Terraform, run terraform plan to Check the plan and then run terraform apply to apply the configuration.

📌Verify the Output

⚙️Task 3: Practice writing Terraform configurations using HCL syntax

To practice writing Terraform configurations using HCL syntax, you can create a simple configuration that uses the docker provider to create a Docker container. Here's a step-by-step guide:

📌Install the Docker

sudo apt-get install docker.io docker-compose -y

📌Add Current User to Docker group

sudo usermod -aG docker $USER

📌Create a terraform file main.tf & Define the Required Providers.

In your main.tf file, define the required providers, such as docker

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

provider "docker" {}

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

resource "docker_container" "nginx" {      #Create a Docker Container
  image = docker_image.nginx.image_id
  name  = "tutorial"

  ports {
    internal = 80
    external = 8000
  }
}

📌Initialize and Apply the Configuration

Run terraform init to initialize Terraform, run terraform plan to Check the plan and then run terraform apply to apply the configuration.

Verify the Container: Use the sudo docker ps command to verify that the container has been created and is running.

📌Then Access the Nginx through Docker

Open Security group -> Edit Inbound rules -> Add 8000 -> Save it.

then, to accessing the Nginx EC2-public-ip:8000

I believe this blog will be really helpful, giving you fresh perspectives and teaching you something new and interesting. 🙏

😊 Enjoy learning!

Did you find this article valuable?

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